///直接下载文件 会下载到电脑的默认路径中一般为C:\Users\xxxxxx\Downloads/
@RequestMapping("/download")
public void download(HttpServletRequest req,HttpServletResponse response) throws IOException{
String filepath=pdfDownloadUrl; //文件所在路径 服务器用相对路径,本地开发可用绝对路径
String fileName=req.getParameter("fileName").toString();//文件名称(全名包括文件类型如11.pdf)
File file = new File(filepath,fileName);
if (file.exists()) {
response.setCharacterEncoding("utf-8");
response.setHeader("Pragma", "No-Cache");
response.setHeader("Cache-Control", "No-Cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/msexcel; charset=UTF-8");
response.setHeader("Content-disposition","attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));// 设定输出文件头
ServletOutputStream out = null;
FileInputStream in = new FileInputStream(filepath+fileName); // 读入文件
out = response.getOutputStream();
out.flush();
int aRead = 0;
while ((aRead = in.read()) != -1 & in != null) {
out.write(aRead);
}
out.flush();
in.close();
out.close();
return;
}
}