黑马程序员技术交流社区
标题:
【上海校区】文件下载例子
[打印本页]
作者:
不二晨
时间:
2019-3-22 09:19
标题:
【上海校区】文件下载例子
/**
* 检索列表中下载附件
* 传入参数:@param uploadFile
* 传入参数:@param request
* 返回类型:void
*/
@RequestMapping(value = "/downloadFile")
public void downloadFile(UploadFile uploadFile,HttpServletRequest request, HttpServletResponse response) {
BufferedInputStream in = null;
BufferedOutputStream out = null;
FileInputStream fis=null;
try {
/*根据id单条查询*/
uploadFile = allSearchService.queryFileInformationById(uploadFile);
response.setContentType("text/html;charset=UTF-8");
//String rootpath = request.getSession().getServletContext().getRealPath("/");
String rootpath=SysParamInits.ARRANGE_FILE_PATH;//配置文件中指定下载文件路径。例D:\files
File f = new File(rootpath + File.separator + uploadFile.getFilepath());
response.reset();
response.setContentType("application/x-excel");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(uploadFile.getFilename().getBytes("gbk"),"iso-8859-1"));
response.setHeader("Content-Length",String.valueOf(f.length()));
fis=new FileInputStream(f);
in = new BufferedInputStream(fis);
out = new BufferedOutputStream(response.getOutputStream());
byte[] data = new byte[1024];
int len = 0;
while (-1 != (len=in.read(data, 0, data.length))) {
out.write(data, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
}
---------------------
【转载,仅作分享,侵删】
作者:小志的博客
原文:
https://blog.csdn.net/li1325169021/article/details/84936964
版权声明:本文为博主原创文章,转载请附上博文链接!
作者:
不二晨
时间:
2019-3-25 17:13
奈斯,感谢分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2