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 ...
随机推荐
- ORACLE复杂查询之连接查询
一.传统的连接查询 1.交叉连接:返回笛卡尔积 WHERE中限定查询条件,可以预先过滤掉掉不符合条件的记录,返回的只是两个表中剩余记录(符合条件的记录)的笛卡尔积. 2.内连接:参与连接的表地位平等, ...
- Day6_正则表达式
用作匹配数据的方法: print(re.findall('\w','yangshuai 123 asd \n \t')) #w:匹配字母数字下划线 print(re.findall('\W','yan ...
- arcEngine开发之根据点坐标创建Shp图层
思路 根据点坐标创建Shapefile文件大致思路是这样的: (1)创建表的工作空间,通过 IField.IFieldsEdit.IField 等接口创建属性字段,添加到要素集中. (2)根据获取点的 ...
- cw2vec理论及其实现
导读 本文对AAAI 2018(Association for the Advancement of Artificial Intelligence 2018)高分录用的一篇中文词向量论文(cw2ve ...
- 夜神模拟器链接Android studoid
在cmd 窗口输入:adb.exe connect 127.0.0.1:62001然后as就自动匹配了夜神经常忘记,特此提醒
- Junit-4.1.2 @Test 使用
学习使用Junit-4.1.2 @Test来做单元测试 1.下载jar包 下载junit-4.12.jar 下载hamcrest-core-1.3.jar 2.在External Libraries中 ...
- 【转】浏览器输入URL后发生了什么
转自:http://www.cnblogs.com/webdeve/p/7865520.html本文摘要: 输入网址 当我们在浏览器输入网址并回车后,一切从这里开始. 一.DNS域名解析 我们在浏览器 ...
- 关于Linux虚拟化技术KVM的科普
虚拟化技术应用越来越广泛,虚拟化技术需求越来越强劲.KVM.XEN.Docker等比较热门,尤其是KVM技术越来越受欢迎. 基于此背景,了解一下KVM+QEMU就有点必要了. 从网上收集了一些资料进行 ...
- maven入门 (二)_私服安装与上传下载
本篇文章主要介绍maven的私服安装和 jar包的上传与下载.毕竟大家还是在公司需要上传jar包到自己公司私服的. 1.安装私服 下载链接: https://pan.baidu.com/s/17dbQ ...
- 错误 C2280 Union : 尝试引用已删除的函数 以及 警告 C4624 “Grade”: 已将析构函数隐式定义为“已删除”的一种解决方法
Union 是C/C++语言中的一种结构类型,用于定义可共享内存的数据变量的一种方式,初次使用Union联合体时可能会遇到以下问题: 错误 C2280 Union : 尝试引用已删除的函数 警告 C4 ...