jsp页面:
页面列表点击下载,根据下载文件名称后缀名,传参数,下载文件的ID,调用js,到struts中.do处理类。
java类:
@RequestMapping(params = "method=downloadFile")
public void downloadFile(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
// 解决中文乱码问题
String id = new String(request.getParameter("id").getBytes("iso-8859-1"), "UTF-8");
Long idL = Long.parseLong(id);
DownloadFile downloadFile = downloadFileService.get(idL);
String name = downloadFile.getName();//下载文件名称
byte[] content= downloadFile.getContent();//下载文件内容
response.setContentType("application/x-msdownload ");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(name.getBytes("gbk"), "iso-8859-1"));
response.getOutputStream().write(content); // 写入文件
response.getOutputStream().flush();
response.getOutputStream().close();
}