@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. Qt与FFmpeg联合开发指南(三)——编码(1):代码流程演示

    前两讲演示了基本的解码流程和简单功能封装,今天我们开始学习编码.编码就是封装音视频流的过程,在整个编码教程中,我会首先在一个函数中演示完成的编码流程,再解释其中存在的问题.下一讲我们会将编码功能进行封 ...

  2. 【转载】tomcat+nginx+redis实现均衡负载、session共享(二)

    今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html. 1.red ...

  3. go Mutex (互斥锁)和RWMutex(读写锁)

    转载自: https://blog.csdn.net/skh2015java/article/details/60334437 golang中sync包实现了两种锁Mutex (互斥锁)和RWMute ...

  4. Android开发之深入理解Android 7.0系统权限更改相关文档

    http://www.cnblogs.com/dazhao/p/6547811.html 摘要: Android 6.0之后的版本增加了运行时权限,应用程序在执行每个需要系统权限的功能时,需要添加权限 ...

  5. Windows 10创意者更新ISO发布!官方下载

    http://news.mydrivers.com/1/526/526719.htm 去年7月份,微软面向Windows 10推出了“周年更新”,让系统变得更加稳定好用.今天,Windows 10迎来 ...

  6. 数据库导入Excel数据的简易方法

    当然,最糙猛的方式就是自己写程序读取Excel程序然后插进数据库,但那种方式要求太高.说个简单方法,主流数据库的管理工具支持CSV文件格式数据向表导入,而Excel可以另存外CSV文件,这种导入就手工 ...

  7. JavaScript 中的undefined and null 学习

    JavaScript 中的undefined and null learn record from the definitive guide to html5 JavaScript 中有两个特殊值:u ...

  8. Linux基础命令归纳大全

    Linux发行版本:基于linux内核提供桌面环境及办公套件的操作系统 (Linux内核只有一个)   1. 启动终端的快捷键: ctr + alt + t 2. 终端字体放大: ctr+shift+ ...

  9. 分析DuxCms之AdminController

    /** * 后台模板显示 调用内置的模板引擎显示方法, * @access protected * @param string $templateFile 指定要调用的模板文件 * @return v ...

  10. -bash: ./switch.sh: /bin/bash^M: bad interpreter: No such file or directory

    问题: 偶然使用 windows 进行编写脚本.使用 wsl (windows subsystem for linux) 进行运行的时候,什么事情没有.但是当把脚本移植到远程服务器进行运行的时候,发现 ...