@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. ThreadPoolExecutor的运转机制

    最近发现几起对ThreadPoolExecutor的误用,其中包括自己,发现都是因为没有仔细看注释和内部运转机制,想当然的揣测参数导致,先看一下新建一个ThreadPoolExecutor的构建参数: ...

  2. JAVA基础经典面试

    [前言] 整理好久,这篇总结写的超级好,很全面.要全部背下来,至于框架应用那块,一定要有针对的,多写项目,照着慕课的对应项目. 链接:http://www.importnew.com/22083.ht ...

  3. Mybatis 系列7

    上篇系列6中 简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介绍mapper映射文件的配置. 这是mybatis的核心之一 一定要学好 在mapper文件中,以mapper作为根 ...

  4. 使用Python自动提取内容摘要

    https://www.biaodianfu.com/automatic-text-summarizer.html 利用计算机将大量的文本进行处理,产生简洁.精炼内容的过程就是文本摘要,人们可通过阅读 ...

  5. 关于Apple开发者的D-U-N-S Number

    企业开发者需要这个信息,中文译名叫邓白氏编码,很多攻略给的那个申请地址已经失效,这个组织官方也有地址可以提交申请资料,不过得注册,苹果目前可用的地址是:https://developer.apple. ...

  6. log4j2.xml全配置文件

    可以参考如下配置 <?xml version="1.0" encoding="UTF-8"?> <!--日志级别以及优先级排序: OFF &g ...

  7. Ocelot中文文档-入门

    Ocelot只能用于.NET Core,目前是为netcoreapp2.0构建的,这个文档可能会帮你了解Ocelot是否适合你. .NET Core 2.0 安装NuGet包 使用nuget安装Oce ...

  8. java使用Myeclipse创建Hibernate项目碰到的诸多问题总结

    这两天一直在搞Myeclipse创建Hibernate的1对多映射. 由于缺乏经验,可算是把我坑惨了.控制台是不停地报错啊~~~~我差点就崩溃了. 1.看的是慕课网的Hibernate一对多映射教程, ...

  9. Pygame常用方法

    '''import pygame# 初始化pygame库,让计算机硬件准备pygame.init()# ----------窗口相关操作-----------# 创建窗口window = pygame ...

  10. IntelliJ IDEA maven 构建简单springmvc项目

    环境: apache-tomcat-8.5.15 jdk1.8.0_172 IDEA 建立一个maven-webapp项目:Create New Project 后点击next 然后next 可以选择 ...