博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring mvc 下载文件处理
阅读量:4956 次
发布时间:2019-06-12

本文共 3069 字,大约阅读时间需要 10 分钟。

@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);

}

转载于:https://www.cnblogs.com/guoke-jsp/p/4339616.html

你可能感兴趣的文章
JavaWeb项目笔记包括jsp的用法selevt,HTML5
查看>>
大话数据结构之三(栈和队列)
查看>>
简单代码实现网页弹出遮罩层
查看>>
KBEngine.executeRawDatabaseCommand使用
查看>>
获取TrustedInstaller
查看>>
图论全解(二版)
查看>>
DFS之城堡问题
查看>>
Poco Reactor Demo
查看>>
Linux:安装Zookeeper
查看>>
怎样去写线程安全的代码(Java)
查看>>
C++函数默认参数
查看>>
GIT之分支管理
查看>>
C# Socket学习笔记一
查看>>
关闭Debut.Log
查看>>
Spring中bean的scope详解
查看>>
Django实战(2):创建第一个模型类
查看>>
mysql -- 基础语句
查看>>
RabbitMQ 实现远程过程调用RPC
查看>>
文件的打开方式
查看>>
swift开发网络篇—NSURLConnection基本使用
查看>>