基于 Nginx XSendfile + SpringMVC 进行文件下载
转自:http://denger.iteye.com/blog/1014066
基于 Nginx XSendfile + SpringMVC 进行文件下载
PS:经过实际测试,通过 nginx 提供文件下载功能的时候,在 Application Server(Java/RoR/Go...) 端不设置 Content-Length 也是可以的
在平常我们实现文件下载通常是通过普通 read-write方式,如下代码所示。
- @RequestMapping("/courseware/{id}")
- public void download(@PathVariable("id") String courseID, HttpServletResponse response) throws Exception {
- ResourceFile file = coursewareService.downCoursewareFile(courseID);
- response.setContentType(file.getType());
- response.setContentLength(file.contentLength());
- response.setHeader("Content-Disposition","attachment; filename=\"" + file.getFilename() +"\"");
- //Reade File - > Write To response
- FileCopyUtils.copy(file.getFile(), response.getOutputStream());
- }
由于程序的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中加入以下配置
- server {
- listen 80;
- server_name dl.mydomain.com;
- location / {
- proxy_pass http://127.0.0.1:8080/; #首先pass到应用服务器
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size 10m;
- client_body_buffer_size 128k;
- proxy_connect_timeout 90;
- proxy_send_timeout 90;
- proxy_read_timeout 90;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- }
- location /course/ {
- charset utf-8;
- alias /nfs/files/; #文件的根目录(允许使用本地磁盘,NFS,NAS,NBD等)
- internal;
- }
- }
其Spring代码为:
- package com.xxxx.portal.web;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.xxxx.core.io.ResourceFile;
- import com.xxxx.portal.services.CoursewareService;
- /**
- * File download controller, provide courseware download or other files. <br>
- * <br>
- * <i> download a course URL e.g:<br>
- * http://dl.mydomain.com/download/courseware/1 </i>
- *
- * @author denger
- */
- @Controller
- @RequestMapping("/download/*")
- public class DownloadController {
- private CoursewareService coursewareService;
- protected static final String DEFAULT_FILE_ENCODING = "ISO-8859-1";
- /**
- * Under the courseware id to download the file.
- *
- * @param courseID The course id.
- * @throws IOException
- */
- @RequestMapping("/courseware/{id}")
- public void downCourseware(@PathVariable("id") String courseID, final HttpServletResponse response) throws IOException {
- ResourceFile file = coursewareService.downCoursewareFile(courseID);
- if (file != null && file.exists()){
- // redirect file to x-accel-Redirect
- xAccelRedirectFile(file, response);
- } else { // If not found resource file, send the 404 code
- response.sendError(404);
- }
- }
- protected void xAccelRedirectFile(ResourceFile file, HttpServletResponse response)
- throws IOException {
- String encoding = response.getCharacterEncoding();
- response.setHeader("Content-Type", "application/octet-stream");
- //这里获取到文件的相对路径。其中 /course/ 为虚拟路径,主要用于nginx中进行拦截包含了/course/ 的URL, 并进行文件下载。
- //在以上nginx配置的第二个location 中同样也设置了 /course/,实际的文件下载路径并不会包含 /course/
- //当然,如果希望包含的话可以将以上的 alias 改为 root 即可。
- response.setHeader("X-Accel-Redirect", "/course/"
- + toPathEncoding(encoding, file.getRelativePath()));
- response.setHeader("X-Accel-Charset", "utf-8");
- response.setHeader("Content-Disposition", "attachment; filename="
- + toPathEncoding(encoding, file.getFilename()));
- // response.setContentLength((int) file.contentLength()); // 经过实际测试,这里不设置 Content-Length 也是可以的
- }
- //如果存在中文文件名或中文路径需要对其进行编码成 iSO-8859-1
- //否则会导致 nginx无法找到文件及弹出的文件下载框也会乱码
- private String toPathEncoding(String origEncoding, String fileName) throws UnsupportedEncodingException{
- return new String(fileName.getBytes(origEncoding), DEFAULT_FILE_ENCODING);
- }
- @Autowired
- public void setCoursewareService(CoursewareService coursewareService) {
- this.coursewareService = coursewareService;
- }
- }
基于 Nginx XSendfile + SpringMVC 进行文件下载的更多相关文章
- 【转载】【JAVA秒会技术之图片上传】基于Nginx及FastDFS,完成图片的上传及展示
基于Nginx及FastDFS,完成商品图片的上传及展示 一.传统图片存储及展示方式 存在问题: 1)大并发量上传访问图片时,需要对web应用做负载均衡,但是会存在图片共享问题 2)web应用服务器的 ...
- 基于Nginx dyups模块的站点动态上下线并实现简单服务治理
简介 今天主要讨论一下,对于分布式服务,站点如何平滑的上下线问题. 分布式服务 在分布式服务下,我们会用nginx做负载均衡, 业务站点访问某服务站点的时候, 统一走nginx, 然后nginx根据一 ...
- 基于nginx tomcat redis分布式web应用的session共享配置
一.前言 nginx 作为目前最流行的开源反向代理HTTP Server,用于实现资源缓存.web server负载均衡等功能,由于其轻量级.高性能.高可靠等特点在互联网项目中有着非常普遍的应用,相关 ...
- 基于nginx的tomcat负载均衡和集群
要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务器即可共享SESSION了. 你可以自己写tomcat的扩展来保存SESSI ...
- 转: 基于nginx的hls直播系统
转自:http://blog.csdn.net/cjsafty/article/details/9108587 看点: 1. 详细解解答了 nginx rtmp配置过程. 前写了一篇基于nginx的h ...
- Windows 环境下基于 nginx 的本地 PyPI 源
Windows 环境下基于 nginx 的本地 PyPI 源的搭建: 1.登录 nginx 官网,下载安装包
- 7个基于Linux命令行的文件下载和网站浏览工具
7个基于Linux命令行的文件下载和网站浏览工具 时间:2015-06-01 09:36来源:linux.cn 编辑:linux.cn 点击: 2282 次 Linux命令行是GNU/Linux中最神 ...
- 基于nginx+lua+redis高性能api应用实践
基于nginx+lua+redis高性能api应用实践 前言 比较传统的服务端程序(PHP.FAST CGI等),大多都是通过每产生一个请求,都会有一个进程与之相对应,请求处理完毕后相关进程自动释放. ...
- Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)
Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 最近因为项目关系,收朋友之托,想制作秀场网站,但是因为之前一直没有涉及到这 ...
随机推荐
- spring mvc源码-》MultipartReques类-》主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multipart/form-data"格式,以二进制形式提交数据,提交方式为post方式。
spring mvc源码->MultipartReques类-> MultipartReques类主要是对文件上传进行的处理,在上传文件时,编码格式为enctype="multi ...
- android.os.TransactionTooLargeException
android.os.TransactionTooLargeException 今天开发过程共遇到问题,后台要反回一些表格,不是单纯的数据.就是有一些html标签的东西.错误的思路: 我得到数据后通过 ...
- VC问题 IntelliSense:“没有可用的附加信息”,[请參见“C++项目 IntelliSense 疑难解答”,获得进一步的帮助]
在XP上安装VS2010 后发现 IntelliSense不能使用,但在Windows7上是能够正常使用这功能的.关于IntelliSense不能使用的问题已有网友提出了是由于KB2876217这个补 ...
- cocos2dx编译安卓版本号查看C++错误
首先,在Mac以下相关软件路径,打开"终端",然后输入 pico .bash_profile 回车 export COCOS2DX_ROOT=/Users/bpmacmini0 ...
- AWS OpsWorks新增Amazon RDS支持
AWS OpsWorks是一个应用管理服务. 你可以通过它把你的应用在一个 堆栈中定义成为不同层的集合.每一个堆栈提供了须要安装和配置的软件包信息,同一时候也能部署不论什么在OpsWorks层中定义的 ...
- Linux -- 内存控制之oom killer机制及代码分析
近期,线上一些内存占用比較敏感的应用.在訪问峰值的时候,偶尔会被kill掉,导致服务重新启动.发现是Linux的out-of-memory kiiler的机制触发的. http://linux-mm. ...
- 欣喜若狂!今天最终成功把音频导入到iphone了,大半年的努力,靠的毅力和方法
研究IOS 的助手也有大半年时间了,一直没有实现导入音视频文件的功能,主要是过程太复杂,而且基本上没有资料能够查询.经过不懈的努力,今天最终成功导入了一个mp3 文件到ipod,一切功能正常,期间经历 ...
- U4704 函数
U4704 函数 题目背景 设gcd(a,b)为a和b的最大公约数,xor(a,b)为a异或b的结果. 最大公约数 异或 题目描述 kkk总是把gcd写成xor.今天数学考试恰好出到了gcd(a,b) ...
- spring:使用<prop>标签为Java持久属性集注入值
spring:使用<prop>标签为Java持久属性集注入值 使用 spring 提供的<prop>为Java持久属性集注入值,也就是向 java.util.Propertie ...
- CentOS7 iso ks