Spring mvc 下载文件处理

摘要:
=null){fis.close();}if(bos!=null){bos.close();}}catch(IOExceptione){//TODOAuto generatedcatchblocke.printStackTrace()}}longend=System.currentTimeMillis();System.out.println(结束-开始);}/***@paramresponse*@paramname*@paramurl*@paramfile*/privatevoiddownFileWidthData(HttpServletResponseresponse,Stringname,Stringurl,Filefile){longstart=System.currentTimeMillis();System.out.println(开始);FileInputStreamfis=null;try{fis=newFileInputStream(文件);}catch(FileNotFoundException1){//TODOAuto-generatedcatchblocke1.printStackTrace();}DataOutputStreamdos=null;OutputStreamos=空;try{url=URLEncoder.encode(url,“utf-8”);//response.addHeader(“Context Disposition”,//“附件:filename=”+URLEncoder.cencode(name,“utf-8”));response.addHeader(“Content Length”,“”+file.Length());response.addHeader(”Content Disposition“,”Attachment;filename=“+newString(name.getBytes(”gbk“),”iso-8859-1“)));response.setContentType(”application/ost流;charset=utf-8“);os=response.getOutputStream() ; dos=新数据输出流(os);byte[]b=新字节[1024];intlen;而((len=fis.read(b))!=-1) {dos.write(b,0,len);}dos.flush();os.flush();}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}最后{try{if(os!=null){os.close();{if(dos!=null{dos.close()}if(fis!

@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 下载文件处理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇苹果cms测试Spring Security 实战(使用Spring Boot项目演示)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

IIS 中 "另一个程序正在使用此文件,进程无法访问!"

然而,自从昨晚重新启动机器后,发现iis无法启动。手动启动并提示:“另一个程序正在使用此文件,进程无法访问它!”百度得知这是由港口冲突造成的。什么软件使用端口80?同时,我更改了iis的默认端口80,没问题。接下来,我想知道是哪一方秘密占用了端口80。但是,在执行上述命令后,我没有找到占用端口80的程序。我惊讶地发现没有人占用端口80。...

用arduino做一个智能垃圾桶

这些天我几乎很忙。我有一些时间继续打扰我的arduino。上一次我从TB购买arduino套件时,有一个人体热能感应模块,用于感应人体接近信号。今天我们用这个做一个简单的智能垃圾桶。要实现的功能是:当有人靠近时,垃圾可以自动打开盖子,当人离开时,盖子可以自动关闭。1、 所需材料和工具:1 Arduino SCM我使用Arduino Nano 2人体热能传感模...

WinSCP命令行操作

WinSCP命令行操作WinSCP是一个在Windows环境下使用SSH的开源图形SFTP客户端。它还支持SCP协议。它的主要功能是在本地和远程计算机之间安全地复制文件。在cmd下直接输入winscp,进入winscp操作界面。查看帮助。直接在下面输入帮助以查看所有可用命令。当第一个参数为“both”时,一个参数与另一个参数同步。未指定目录时,同步当前工作目...

Ubuntu安装时怎样分区

应该首先放置启动分区。并将引导设置为主分区。如果是双系统或多系统安装,通常可以选择逻辑分区。首先,Grub可以在1024柱面后面引导Linux内核;第二,即使有多个Linux安装,/boot也可以完全不共享。此外,非独立/引导分区仅占用根文件夹下约20MB的空间。所以决定是否启动。...

Navicat数据存放位置和备份数据库路径设置

navicat数据库存储在哪里?有了这样的问题,让我们来解决这个问题。默认情况下安装Navicat,默认情况下也安装MySQL,数据库存储在默认用户的目录中。选择安装目录时,还可以选择数据的位置。很多人此时只是设置了MySQL的安装位置。...

Vmware安装Linux打开一直黑屏的四个解决方法

方法1:以管理员身份打开cmd,输入netshwinsocksreset,按enter键,然后重新启动计算机方法2:关闭虚拟机,编辑虚拟机设置,选择硬件中的虚拟机设置、删除加速3D图形前面的复选框,然后再次启动虚拟机。最终的解决方案:我一开始使用的是14版,但上述方法都不能解决黑屏问题。然后我使用版本12来解决这个问题。...