使用response实现文件下载注意点
创建web工程,使用response实现文件的下载.
在webRoot下创建download文件,里面包含要下载的文件,现在把源码贴上来,然后再说我遇到的问题
public class DownLoadDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath("/download/倒影.jpg");
System.out.println("路径是==="+path);
//lastIndexOf返回的是最后一次出现的索引,截取的话必须要+1,从/的后面开始截取
String filename = path.substring(path.lastIndexOf("/")+1);
//注意:在mac中就是/,但是在windows中,文件路径是\,\是转译符,要再加上一个\,\\才是\:
// String filename = path.substring(path.lastIndexOf("\\")+1);这是在windows下的文件路径截取
System.out.println("文件名是==="+filename);
//如果下载的文件是中文的话,文件需要经过URL编码
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(path);
int len = 0;
byte bys[] = new byte[1024];
out = response.getOutputStream();
while((len = in.read(bys)) > 0){
out.write(bys, 0, len);
}
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
两个需要注意的点:
1.通过文件路径获取文件名,在代码中已经说过,windows和mac的路径是不一样的,这个一定要注意,不然下载就会出错
2.当文件名是中文时,文件要通过URL编码
使用response实现文件下载注意点的更多相关文章
- response与文件下载
参考博客: http://www.cnblogs.com/lcpholdon/p/4380980.html http://www.cnblogs.com/mingforyou/p/3281945.ht ...
- Http响应response(文件下载、验证码)
Http响应response response:响应 作用: 往浏览器写东西 组成部分: 响应行 响应头 响应体 操作响应行 格式: 协议/版本 状态码 状态码说明 状态码: 1xx:已发送请求 2x ...
- response实现文件下载
package cn.itcast.response; import java.io.FileInputStream; import java.io.IOException; import java. ...
- 使用response实现文件下载功能
response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("u ...
- java web response提供文件下载功能
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- response 输出中文数据 文件下载
使用OutputStream或者PrintWriter向客户端浏览器输出中文数据 package com.xc.response; import java.io.IOException; import ...
- spring4 文件下载功能
需要准备的工具和框架 Spring 4.2.0.RELEASE Bootstrap v3.3.2 Maven 3 JDK 1.7 Tomcat 8.0.21 Eclipse JUNO Service ...
- JavaWeb基础:Servlet Response
ServletResponse简介 ServletResponse代表浏览器输出,它提供所有HttpResponse的设置接口,可以设置HttpResponse的响应状态,响应头和响应内容. 生命周期 ...
- ASP.NET 实现PDF文件下载
本文介绍了一种在ASP.NET中下载文件的方法. 方法一:可能是最简单的.最短的方式: Response.ContentType = "application/pdf"; Resp ...
随机推荐
- OpenCV中Mat的列向量归一化
OpenCV中Mat的列向量归一化 http://blog.csdn.net/shaoxiaohu1/article/details/8287528 OpenCV中Mat的列向量归一化 标签: Ope ...
- ntity Framework技巧系列之四 - Tip 13 – 15
提示13. 附加一个实体的简单方式 问题: 在早先的一些提示中,我们讨论了使用Attach来加载一个处于未改变(unchanged)状态的东西到ObjectContext从而避免进行查询的开销. 如果 ...
- LeetCode OJ 61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【Machine Learning in Action --2】K-近邻算法改进约会网站的配对效果
摘自:<机器学习实战>,用python编写的(需要matplotlib和numpy库) 海伦一直使用在线约会网站寻找合适自己的约会对象.尽管约会网站会推荐不同的人选,但她没有从中找到喜欢的 ...
- 修改一个Label上字体的大小(富文本)
假如修改上面的Label上价格30000的大小,那么需要用到富文本 上代码 // 字符串30000 NSString * priceNumber = @"30000"; [pric ...
- c#高级编程
1..net才程序编译经过2步.首先把源代码编译成IL,这个是在visual studio中编译,然后是IL编译成机器语言,这个是在程序执行的时候进行的.
- postgres 数据库备份恢复
postgre 数据库备份恢复命令 备份:pg_dump -U postgres -v -F c -Z 4 -f ***.backup dbname 9压缩率最狠恢复:pg_restore -U p ...
- 数据库NULL和 ‘’ 区别
NULL判断时 : IS NOT NULL ''判断时: !=''
- 关于PHP执行超时的问题
PHP配置文件的参数max_execution_time表示脚本执行超时时间 max_execution_time=0表示不限制 max_execution_time=2表示执行两秒后终止,同时报错F ...
- Linux添加用户user到用户组group
添加用户:useradd niot 添加到组:usermod -a -G root niot 改密码:passwd niot 切换:su - niot 查看用户组:groups 将一个用户添加到用户组 ...