public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String fileName = request.getParameter("file");
String rootPath = this.servlet.getServletContext().getRealPath("files");
File f = new File(rootPath);
if (!f.exists())
f.mkdirs(); // 如果文件夹不存在,则创建
File toFile = new File(rootPath, fileName);
FileInputStream fis = new FileInputStream(toFile);
// 初始化response
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes(), "ISO-8859-1"));
OutputStream os = response.getOutputStream();
byte[] b = new byte[1024];
int real = fis.read(b);
while (real > 0) {
os.write(b, 0, real);
real = fis.read(b);
}
os.close();
fis.close();
return null;
}
以下是action 代码