在平常我们实现文件下载通常是通过普通 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());
}
//如果存在中文文件名或中文路径需要对其进行编码成 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;
}
}
分享到:
相关推荐
大型电商项目实战1:Redis+Rest+Linux+Nginx+Spring+SpringMVC实现JAVA高并发秒杀系统,baidu链接,谢谢
Spring+Springmvc+Mybatis+Oracle+Redis搭建完整的框架,实测可运行。
1、基于vue+python+flask+uwsgi+nginx+mysql的外包项目网站项目源码.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为...
开发工具 1.Eclipse IDE:采用Maven项目管理,模块化。 2.代码生成:通过界面方式简单配置,自动生成相应代码,目前包括三种生成方式(增...负载均衡:Nginx 分布式文件:FastDFS 数据库连接池:Alibaba Druid 1.0
spring4.3+hibernate4.3+springmvc+nginx+Redis实现session共享demo
Nginx++Keepalived+Tomcat负载 均衡 &动 静 分 离。配置
Nginx+Uwsgi+Django(python3)+Vue部署,一步步实现。网上找了N多篇文章都没成功,特意记录,以免其他同学踩坑
Nginx+tomcat+redis软负载均衡需要的一整套资源,还包括了JDK,依赖包等文件
nginx配置+https
本文档详细介绍了分布式企业架构技术文档,分布式服务,j2ee核心技术,前端后端技术类型。分布式云服务,以及分布式架构在企业当中的运用
nginx+lua+redis通过匹配客户端ip进行灰度发布
自己做的一个nginx+tomcat+redis完成session共享小例子,包括所需jar,适合初学者,希望大家一起学习交流
示例:使用Docker搭建基于Nginx+Tomcat的分布式部署架构。
centos7.2 Nginx1.12 + PHP7.1 + Mysql5.6 安装教程,希望对大家有用
Nginx+upload+lua实现简单文件上传服务
校园匿名问答网站-项目引导:【管理员PC端web项目】基于Vue+Element开发:【用户移动端web项目】基于Vue+Element+Cube开发:【服务器后台项目】基于SpringBoot+SpringMVC+MyBatis+Nginx+Redis开发:
基于nginx+lua+dyups实现服务自定义调度资源整理,自定义服务调度主要是指根据不同客户端请求进行判断,转发到不同的服务集群。
详细说明了windows服务器nginx+tomcat+mysql部署及配置(配置阿里云后台安全组,配置域名)很适合新手学习 附件中包含: 1.操作说明文档 2.操作录屏 3.安装所用到的软件安装包 1)Windows Server 2019 数据中心版 ...
nginx1.9.7+keepalive1.3.2,可搭建Nginx可高用集群和自动故障转移