@RequestMapping(value = "downFile")
public void downFile(HttpServletResponse response, String name,
HttpServletRequest request) {
ServletContext sc = request.getSession().getServletContext();
String url = sc.getRealPath("/upload/" + name);
File file = new File(url);

//以下两种文件的下载流的处理方式,第二个方法感觉比较好
downFileWidthData(response, name, url, file);// 用

// downFileWidthBuffer(response, name, file);//运用buffer

}

/**
* @param response
* @param name
* @param file
*/
private void downFileWidthBuffer(HttpServletResponse response, String name,
File file) {
Date date = new Date();
long start = System.currentTimeMillis();
System.out.println(start);
BufferedOutputStream bos = null;
FileInputStream fis = null;
try {

response.addHeader("Content-Length", "" + file.length());
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(name.getBytes("gbk"), "iso-8859-1"));

response.setContentType("application/octet-stream;charset=UTF-8");
response.setContentType("application/octet-stream;charset=UTF-8");

} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);

bos = new BufferedOutputStream(response.getOutputStream());

bos.write(buffer);
bos.flush();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
if (fis != null) {
fis.close();
}
if (bos != null) {
bos.close();

}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end = System.currentTimeMillis();

System.out.println(end - start);
}

/**
* @param response
* @param name
* @param url
* @param file
*/
private void downFileWidthData(HttpServletResponse response, String name,
String url, File file) {
long start = System.currentTimeMillis();
System.out.println(start);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DataOutputStream dos = null;
OutputStream os = null;
try {
url = URLEncoder.encode(url, "utf-8");
// response.addHeader("Context-Disposion",
// "Attachment:filename="+URLEncoder.encode(name, "utf-8"));
response.addHeader("Content-Length", "" + file.length());
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(name.getBytes("gbk"), "iso-8859-1"));
response.setContentType("application/octet-stream;charset=UTF-8");
os = response.getOutputStream();
dos = new DataOutputStream(os);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
dos.write(b, 0, len);
}
dos.flush();
os.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
if (os != null) {
os.close();
}
if (dos != null) {
dos.close();
}
if (fis != null) {

fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);

}

Spring mvc 下载文件处理的更多相关文章

  1. Spring MVC -- 下载文件

    像图片或者HTML文件这样的静态资源,在浏览器中打开正确的URL即可下载,只要该资源是放在应用程序的目录下,或者放在应用程序目录的子目录下,而不是放在WEB-INF下,tomcat服务器就会将该资源发 ...

  2. spring MVC 下载文件(转)

    springle MVC中如何下载文件呢? 比struts2 下载文件简单得多 先看例子: @ResponseBody @RequestMapping(value = "/download& ...

  3. spring mvc 下载文件链接

    http://www.blogjava.net/paulwong/archive/2014/10/29/419177.html http://www.iteye.com/topic/1125784 h ...

  4. Spring mvc下载文件java代码

    /** * 下载模板文件 * @author cq */ @RequestMapping("/downloadExcel.do") public ResponseEntity< ...

  5. Spring MVC的文件上传和下载

    简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...

  6. Spring MVC 实现文件的上传和下载

    前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...

  7. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

  8. 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity

    文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...

  9. Java Web 学习(8) —— Spring MVC 之文件上传与下载

    Spring MVC 之文件上传与下载 上传文件 表单: <form action="upload" enctype="multipart/form-data&qu ...

随机推荐

  1. 阅读spring源码

    读Spring源码之前,你要先清楚,为什么你要用Spring... Spring最基本的功能是做为管理bean的容器,所以我以为应该先从org.springframework.context包了解咯, ...

  2. 修改was数据源

    本机的RAD运行的工程可以通过修改jpa中的persistence中的jni修改数据源: 对于通过was控制台部署的ear需要在was控制台:资源--jdbc 修改数据源

  3. Strom topology 设计的演进

    场景:采集日志数据,日志数据有多个字段组成,需求是根据日志数据中的N个字段(维度),去统计指标数据(个数.平均值)等.

  4. .net Entity Framework初识1

    利用EF可以直接操纵数据库,在一些简单的项目里甚至完全不用写sql. 一 code first 1.在web.config中设置连接字符串 这一步可以省略.如果跳过这一步,程序会默认生成一个可用的连接 ...

  5. LindDotNetCore~框架介绍及特色功能(有点springboot的意思)

    LindDotNetCore模块介绍 大叔博客 LindDotNetCore相关模块介绍 [x] 全局都是依赖DI [x] 消息队列 [x] NoSql [x] Caching [x] 仓储 [x] ...

  6. MySQL/MariaDB中的事务和事务隔离级别

    本文目录:1.事务特性2.事务分类 2.1 扁平事务 2.2 带保存点的扁平事务 2.3 链式事务 2.4 嵌套事务 2.5 分布式事务3.事务控制语句4.显式事务的次数统计5.一致性非锁定读(快照查 ...

  7. MFC半透明对话框

    int CTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDialog::OnCreate(lpCreateStruct) == -1) ...

  8. JavaScript教程大纲

    因为考虑到Python的接受难度,改为推广较为简单和流行的JavaScript.先列主要参考资料:         JavaScript权威指南(第6版):http://book.douban.com ...

  9. NewLife.Net——网络压测单机1.88亿tps

    NewLife.Net压力测试,峰值4.2Gbps,50万pps,消息大小24字节,消息处理速度1.88亿tps! 共集合20台高配ECS参与测试,主服务器带宽6Gbps.100万pps,16核心64 ...

  10. SEO优化:浅析关键词出现在网站哪些地方更有利?

    关键词出现在网站哪些地方符合SEO?进行网站的SEO时,关键词需要出现在整个网站的适当地方.下面列出几个重要的关键词摆放的地方.以下列出的10个地方希望能够帮助到大家. 1.网站Title部分. 2. ...