网上找了,百度了几种方式, 总结一下,下面这种,我认为是比较好的。前端
固然能够 使用springMVC 的 那种简洁方式,但是不够原生,看起来不容易明白。nginx
因此下面这样的方式 就比较 简洁,而又容易理解,解决原生。spring
//下载绩效操做手册
@RequestMapping("/down/pfmceplan.do")
public String downPfmceplan(HttpServletRequest request,HttpServletResponse response) throws Exception{
String fileName="绩效操做shouceV1.0.zip";
//解决中文丢失问题
String fileNameHeader=new String(fileName.getBytes(), "ISO-8859-1");
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileNameHeader);
String path = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
// 求出 根项目的路径
String pathRealroot=path.substring(0, path.lastIndexOf("WEB-INF"))+"assets/"+"download/";
File downPath=new File(pathRealroot
+File.separator + fileName);
System.out.println("=================>>>>>>>>>>>>>>>"+downPath);
OutputStream os = response.getOutputStream();apache
// FileUtils 来自 apache 的开源 common包tomcat
// org.apache.commons.io.FileUtils 引入 该包调该方法便可服务器
os.write(FileUtils.readFileToByteArray(downPath));
os.flush();
os.close();
return null;
}app
这里的文件是固定死了, 同理 若是是 前端 传过来也是同样的道理。 图片
要求不高的 文件下载,能够 在springMVC 中,将其设置为 静态文件,相似 image 图片,js的同样,过滤权限,能够直接访问,便可实现下载了,这里不对其说明。ip
可是这样的方式,访问的时候该文件名就不能带有中文的。utf-8
若是须要带中文 能够在 服务器里面 tomcat 或者 nginx 配置一下 请求的字符集,
好比 tomcat的
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="utf-8"
redirectPort="8443" />
这样 就能够 支持 包含中文名称的文件名称 下载了 。