SpringMVC+JSP,实现文件下载功能(IE,Google均可用)
一、java代码
@RequestMapping(value = "download_new")
public void doPost(HttpServletRequest request, HttpServletResponse response,String files) throws Exception {
String filedownload = files;
Integer num = files.lastIndexOf("/");
String filedisplay = files.substring(num+1, files.length());
response.setContentType("application/x-download");
response.addHeader("Content-Disposition", "attachment;filename="+filedisplay);
try {
RequestDispatcher rd = request.getRequestDispatcher(filedownload);
if(rd != null) {
rd.forward(request,response);
}
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}
jsp
<form action="/download_new" method="post" style="float:left;" id="download_form">
<input type="hidden" value="${files }" name="files"/>
<input id="newindex_download" type="submit" value="相关附件下载"/>
</form>