nginx.conf

location / {
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;
proxy_pass http://backend;
} location /file/ {
internal;
alias /usr/local/;
}

设置成 "internal" 属性是用来禁止浏览器直接访问的,只信任后台返回的 "X-Accel-Redirect"。

getDownFile.jsp

<%
String filename = request.getParameter("filename");
response.setHeader("Content-Disposition", "attachment;filename="+filename);
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("X-Accel-Redirect", "/file/"+filename);
%>

http://10.10.3.205/test/getDownFile.jsp?filename=5.zip

请求到nginx后会发给Tomcat,先判断是否可以下载,若可以下载设置X-Accel-Redirect回给nginx,nginx重新定位到物理文件进行下载。

下面是JFinal的用法

html

<a href="<%=path%>/redis_test/testResponse?id=1&filename=5.zip">下载</a>

后台

String filename = getPara("filename");
String id = getPara("id");
if(id.equals(""))
{
getResponse().setHeader("Content-Type", "application/octet-stream");
getResponse().setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("中文名.zip", "UTF-8"));
getResponse().setHeader("X-Accel-Redirect", "/file/"+filename); }
renderNull();

Nginx 利用 X-Accel-Redirect response.setHeader 控制文件下载的更多相关文章

  1. JSP禁用缓存的方式 response.setHeader( "Pragma", "no-cache" ); setDateHeader("Expires", 0);

    JSP禁用缓存的方式    使用服务器端控制AJAX页面缓存:        response.setHeader( "Pragma", "no-cache" ...

  2. response.setHeader("Cache-Control","no-cache");的作用

      1. response.setHeader("Cache-Control","no-cache"); This is used to prevent the ...

  3. response.setHeader()的用法

    一秒刷新页面一次 response.setHeader("refresh","1"); 二秒跳到其他页面 response.setHeader("re ...

  4. nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录

    geo指令使用ngx_http_geo_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_geo_module.ngx_http_geo_modu ...

  5. Servlet实现定时刷新到另外一个页面response.setHeader("refresh", "3;url=/...")

    想要实现,访问Responsedemo11的时候,3秒钟后,跳转到ResponseDemo10 用   response.setHeader("refresh", "3; ...

  6. nginx利用limit模块设置IP并发防CC攻击

    nginx利用limit模块设置IP并发防CC攻击 分类: 系统2013-01-21 09:02 759人阅读 评论(0) 收藏 举报 来源:http://blog.xencdn.net/nginx- ...

  7. response.setHeader各种使用方法

    一秒刷新页面一次 response.setHeader("refresh","1"); 二秒跳到其它页面 response.setHeader("re ...

  8. response.setHeader("Content-disposition","attachment;filename="+fileName) 下载时文件名中存在空格错误

    最近在进行文件下载时发现一个问题,就是下面语句运行时,下载某些文件正常,下载某些文件异常,后来发现文件名中有空格的文件火狐浏览器是默认将文件名截断了的 response.setHeader(" ...

  9. 解决response.setHeader("Content-disposition" 中文乱码问题

    Content-Disposition中文乱码 Response.setHeader(”Content-Disposition”, “attachment; filename=” + fileName ...

随机推荐

  1. AIX性能监控

    http://www.ibm.com/developerworks/cn/aix/library/au-aix7memoryoptimize2/ http://www.aixchina.net/Art ...

  2. Feature hashing相关 - 1

    考虑典型的文本分类,一个经典的方法就是     分词,扫描所有特征,建立特征词典 重新扫描所有特征,利用特征词典将特征映射到特征空间编号 得到特征向量 学习参数 w 存储学习参数 w , 存储特征映射 ...

  3. ASP.NET MVC 使用带有短横线的html Attributes(转载)

    转载地址:http://www.nmtree.net/2013/10/25/asp-net-mvc-use-dash-in-html-attributes.html 情景再现 我们常常需要一个文本框来 ...

  4. [LeetCode] Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. Microsoft SQL Server 博客目录

    基础概念篇 SQL Server排序规则 SQL SERVER 统计信息概述(Statistics) SQL SERVER 索引之聚集索引和非聚集索引的描述 Sql Server 索引之唯一索引和筛选 ...

  6. android studio常见错误

    1.Failed to import new Gradle project: Could not install Gradle distribution from'http://services.gr ...

  7. C++ 中 char 与 int 转换问题

    itoa 功  能:把一整数转换为字符串 函  数:char *itoa(int value, char *string, int radix); 解  释:itoa 是英文integer to ar ...

  8. git warning: LF will be replaced by CRLF in...

    如果你有git项目,在提交代码的过程中可能会碰到上面的警告,特别是的项目中包含序列化对象的时候,你可能要小心!! 警告的含义是说换行符的,不同的操作系统的换行符是不一致的,如果你不清楚,真得看看这个 ...

  9. Linux crontab 定时任务详解

    1.每小时执行一次脚本 * */1 * * * /etc/init.d/smb restart #不是所有的系统都支持“*/1”这种写法可以试试: 0 * * * * /etc/init.d/smb  ...

  10. indeterminateDrawable

    Android原生控件只有横向进度条一种,而且没法变换样式,比如原生rom的样子很丑是吧,当伟大的产品设计要求更换前背景,甚至纵向,甚至圆弧状的,咋办,比如ok,我们开始吧: 一)变换前背景 先来看看 ...