Spring mvc 下载文件处理
@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 下载文件处理的更多相关文章
- Spring MVC -- 下载文件
像图片或者HTML文件这样的静态资源,在浏览器中打开正确的URL即可下载,只要该资源是放在应用程序的目录下,或者放在应用程序目录的子目录下,而不是放在WEB-INF下,tomcat服务器就会将该资源发 ...
- spring MVC 下载文件(转)
springle MVC中如何下载文件呢? 比struts2 下载文件简单得多 先看例子: @ResponseBody @RequestMapping(value = "/download& ...
- spring mvc 下载文件链接
http://www.blogjava.net/paulwong/archive/2014/10/29/419177.html http://www.iteye.com/topic/1125784 h ...
- Spring mvc下载文件java代码
/** * 下载模板文件 * @author cq */ @RequestMapping("/downloadExcel.do") public ResponseEntity< ...
- Spring MVC的文件上传和下载
简介: Spring MVC为文件上传提供了直接的支持,这种支持使用即插即用的MultipartResolver实现的.Spring MVC 使用Apache Commons FileUpload技术 ...
- Spring MVC 实现文件的上传和下载
前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”.我回复他说:“使用Spring MVC框架可以做到这一点,因为Spri ...
- spring mvc ajaxfileupload文件上传返回json下载问题
问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...
- 0062 Spring MVC的文件上传与下载--MultipartFile--ResponseEntity
文件上传功能在网页中见的太多了,比如上传照片作为头像.上传Excel文档导入数据等 先写个上传文件的html <!DOCTYPE html> <html> <head&g ...
- Java Web 学习(8) —— Spring MVC 之文件上传与下载
Spring MVC 之文件上传与下载 上传文件 表单: <form action="upload" enctype="multipart/form-data&qu ...
随机推荐
- ansible常见模块
模块的使用 查看模块帮助 ansible-doc -l 查看所有模块 ansible-doc -s MODULE_NAME 查看指定模块的详细帮助 ansible命令应用基础 语法: ansible ...
- 关于windows修改远程登录端口的问题
windows远程桌面默认使用的是3389,为了避免被别用用心的扫描从而暴力破解远程服务器或者vps的账户信息.可以修改默认端口3389到其它端口,如8000,10000等.最好修改为10000以后的 ...
- 关于.h .lib .dll的总结
对VC工程中的调用过程有些迷糊,所以就理清一下: 1.#include "...h"为头文件预编译命令,如果这些代码被修改,则需要重新编译生成预编译头文件. 预编译头的概念(转载) ...
- LCA最近公共祖先(倍增版)
倍增版LCA lac即最近公共祖先,u和v最近公共祖先就是两节点公用的祖先中深度最大的 比如 其中 lca(1,2)=4, lca(2,3)=4, lca(3,5)=1, lca(2,5)=4; 如何 ...
- 神奇的namespace使用
一大波概念正在来袭: 作用域与命名空间 相关概念 与命名空间相关的概念有: 声明域(declaration region)—— 声明标识符的区域.如在函数外面声明的全局变量,它的声明域为 ...
- 鼠标拖拽定位和DOM各种尺寸详解
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- CentOS-Minimal版本下安装telnet服务和xinetd服务
默认在CentOS-Minimal版本下没有安装telnet和xinetd服务. 1.安装telnet [root@localhost ~]# rpm -qa | grep telnet --检查是 ...
- Vimium快捷键记录
作为一个Geek必备的技能 从今天起在这里仅记录下使用过的快捷键和心得(翻译自 ?) version: 1.59 版本不同,快捷键有所不同 1. 下载安装地址(自备梯子) Chrome商店:https ...
- 基于elk 实现nginx日志收集与数据分析。
一.背景 前端web服务器为nginx,采用filebeat + logstash + elasticsearch + granfa 进行数据采集与展示,对客户端ip进行地域统计,监控服务器响应时间等 ...
- 跨域cors方法(jsonp,document.domain,document.name)及iframe性质
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...