核心代码
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.ljg.struts.action;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ljg.domain.UploadBean;
import com.ljg.service.UploadService;
public class DownloadFileAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String fileName=request.getParameter("fileName");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+fileName);
String filePath=this.getServlet().getServletContext().getRealPath("/file");
System.out.println(filePath);
String fileAllPath=filePath+"\\"+fileName;
System.out.println(fileAllPath);
FileInputStream fis=null;
OutputStream os=null;
byte[] buffer=new byte[1024];
int length=0;
try
{
fis=new FileInputStream(fileAllPath);
os=response.getOutputStream();
while((length=fis.read(buffer))>0)
{
os.write(buffer,0,length);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally
{
try
{
os.close();
fis.close();
} catch (Exception e2)
{
// TODO: handle exception
e2.printStackTrace();
}
}
return mapping.findForward("goBack");
}
}