转自:http://denger.iteye.com/blog/1014066

基于 Nginx XSendfile + SpringMVC 进行文件下载

PS:经过实际测试,通过 nginx 提供文件下载功能的时候,在 Application Server(Java/RoR/Go...) 端不设置 Content-Length 也是可以的

在平常我们实现文件下载通常是通过普通 read-write方式,如下代码所示。

  1. @RequestMapping("/courseware/{id}")
  2. public void download(@PathVariable("id") String courseID, HttpServletResponse response) throws Exception {
  3. ResourceFile file = coursewareService.downCoursewareFile(courseID);
  4. response.setContentType(file.getType());
  5. response.setContentLength(file.contentLength());
  6. response.setHeader("Content-Disposition","attachment; filename=\"" + file.getFilename() +"\"");
  7. //Reade File - > Write To response
  8. FileCopyUtils.copy(file.getFile(), response.getOutputStream());
  9. }

由于程序的IO都是调用系统底层IO进行文件操作,于是这种方式在read和write时系统都会进行两次内存拷贝(共四次)。linux 中引入的 sendfile 的实际就为了更好的解决这个问题,从而实现"零拷贝",大大提升文件下载速度。

    使用 sendfile() 提升网络文件发送性能

    RoR网站如何利用lighttpd的X-sendfile功能提升文件下载性能

  



    在apache,nginx,lighttpd等web服务器当中,都有sendfile feature。下面就对 nginx 上的XSendfile与SpringMVC文件下载及访问控制进行说明。我们这里的大体流程为:

     1.用户发起下载课件请求; (http://dl.mydomain.com/download/courseware/1)

     2.nginx截获到该(dl.mydomain.com)域名的请求;

     3.将其proxy_pass至应用服务器;

     4.应用服务器根据课件id获取文件存储路径等其它一些业务逻辑(如增加下载次数等);

     5.如果允许下载,则应用服务器通过setHeader -> X-Accel-Redirect 将需要下载的文件转发至nginx中);

     6.Nginx获取到header以sendfile方式从NFS读取文件并进行下载。



     其nginx中的配置为:

     在location中加入以下配置

  1. server {
  2. listen 80;
  3. server_name dl.mydomain.com;
  4. location / {
  5. proxy_pass  http://127.0.0.1:8080/;  #首先pass到应用服务器
  6. proxy_redirect     off;
  7. proxy_set_header   Host             $host;
  8. proxy_set_header   X-Real-IP        $remote_addr;
  9. proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
  10. client_max_body_size       10m;
  11. client_body_buffer_size    128k;
  12. proxy_connect_timeout      90;
  13. proxy_send_timeout         90;
  14. proxy_read_timeout         90;
  15. proxy_buffer_size          4k;
  16. proxy_buffers              4 32k;
  17. proxy_busy_buffers_size    64k;
  18. proxy_temp_file_write_size 64k;
  19. }
  20. location /course/ {
  21. charset utf-8;
  22. alias       /nfs/files/; #文件的根目录(允许使用本地磁盘,NFS,NAS,NBD等)
  23. internal;
  24. }
  25. }

其Spring代码为:

  1. package com.xxxx.portal.web;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import com.xxxx.core.io.ResourceFile;
  10. import com.xxxx.portal.services.CoursewareService;
  11. /**
  12. * File download controller, provide courseware download or other files. <br>
  13. * <br>
  14. * <i> download a course URL e.g:<br>
  15. * http://dl.mydomain.com/download/courseware/1 </i>
  16. *
  17. * @author denger
  18. */
  19. @Controller
  20. @RequestMapping("/download/*")
  21. public class DownloadController {
  22. private CoursewareService coursewareService;
  23. protected static final String DEFAULT_FILE_ENCODING = "ISO-8859-1";
  24. /**
  25. * Under the courseware id to download the file.
  26. *
  27. * @param courseID The course id.
  28. * @throws IOException
  29. */
  30. @RequestMapping("/courseware/{id}")
  31. public void downCourseware(@PathVariable("id") String courseID, final HttpServletResponse response) throws IOException {
  32. ResourceFile file = coursewareService.downCoursewareFile(courseID);
  33. if (file != null && file.exists()){
  34. // redirect file to x-accel-Redirect
  35. xAccelRedirectFile(file, response);
  36. } else { // If not found resource file, send the 404 code
  37. response.sendError(404);
  38. }
  39. }
  40. protected void xAccelRedirectFile(ResourceFile file, HttpServletResponse response)
  41. throws IOException {
  42. String encoding = response.getCharacterEncoding();
  43. response.setHeader("Content-Type", "application/octet-stream");
  44. //这里获取到文件的相对路径。其中 /course/ 为虚拟路径,主要用于nginx中进行拦截包含了/course/ 的URL, 并进行文件下载。
  45. //在以上nginx配置的第二个location 中同样也设置了 /course/,实际的文件下载路径并不会包含 /course/
  46. //当然,如果希望包含的话可以将以上的 alias 改为 root 即可。
  47. response.setHeader("X-Accel-Redirect", "/course/"
  48. + toPathEncoding(encoding, file.getRelativePath()));
  49. response.setHeader("X-Accel-Charset", "utf-8");
  50. response.setHeader("Content-Disposition", "attachment; filename="
  51. + toPathEncoding(encoding, file.getFilename()));
  52. // response.setContentLength((int) file.contentLength());  // 经过实际测试,这里不设置 Content-Length 也是可以的
  53. }
  54. //如果存在中文文件名或中文路径需要对其进行编码成 iSO-8859-1
  55. //否则会导致 nginx无法找到文件及弹出的文件下载框也会乱码
  56. private String toPathEncoding(String origEncoding, String fileName) throws UnsupportedEncodingException{
  57. return new String(fileName.getBytes(origEncoding), DEFAULT_FILE_ENCODING);
  58. }
  59. @Autowired
  60. public void setCoursewareService(CoursewareService coursewareService) {
  61. this.coursewareService = coursewareService;
  62. }
  63. }

最新文章

  1. linux命令总结
  2. yii和wp做博客
  3. flex mx组件和s组件的字体兼容性不一致
  4. JSP SQL注入--破法
  5. [转]Linux关机命令详解
  6. CXF(2.7.10) - A simple JAX-WS service
  7. ASOP源码下载
  8. Effective C++ 第二版 40)分层 41)继承和模板 42)私有继承
  9. JavaScript模块化编程之require.js与sea.js
  10. 九度OJ 1016 火星A + B 未AC版,整型存储不下
  11. Nginx的upstream反向代理、负载均衡详解
  12. 原生js实现一个简单的轮播图
  13. zabbix3.0.4安装部署与SendEmail报警配置
  14. Selenium--调用js,对话框处理 (python)
  15. Spring 注解 hibernate 实体方法 &lt;property name=&quot;packagesToScan&quot; value=&quot;com.sise.domain&quot;/&gt;
  16. centos7 添加用户并授权root权限
  17. JavaScript中eval()函数
  18. loadrunner场景中按scenario和group执行的区别
  19. (转)Sql Server 快速查看表结构(表描述及字段说明)
  20. 监控 Linux 性能的 18 个命令行工具(转)

热门文章

  1. A java code
  2. volatile非原子性的示例
  3. 欧几里得 &amp;amp; 拓展欧几里得算法 解说 (Euclid &amp;amp; Extend- Euclid Algorithm)
  4. STL_算法_填充新值(fill、fill_n、generate、generate_n)
  5. SetWindowPos函数详解
  6. Codeforces--633D--Fibonacci-ish(暴力搜索+去重)(map)
  7. python对XML 操作
  8. leetcode二分查找相关
  9. ubuntu 软件桌面图标创建
  10. Oracle 数据导入导出(imp/exp)