码迷,mamicode.com
首页 > 编程语言 > 详细

java 下载文件

时间:2018-12-14 17:17:00      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:osi   name   res   read   files   下载文件   row   str   except   

public class TemplateUtil {

public void downloadTemplate(HttpServletResponse response) throws Exception{

InputStream inputStream =null ;
OutputStream outputStream = null;
inputStream = this.getClass().getResourceAsStream("/files/template.xlsx");
if (inputStream == null){
throw new Exception("未找到模板文件");
}

response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("content-disposition","attachment;filename=" + "模板.xlsx");

try{
outputStream = response.getOutputStream();
byte[] readBytes = new byte[1024];
int read = 0;
while ((read = inputStream.read(readBytes)) != -1){
outputStream.write(readBytes,0,read);
outputStream.flush();
}
}
catch (Exception e){
// do something
}
finally {
if (inputStream != null){
inputStream.close();
}
if (outputStream != null){
outputStream.close();
}
}
}
}

java 下载文件

标签:osi   name   res   read   files   下载文件   row   str   except   

原文地址:https://www.cnblogs.com/phoenix-wings/p/10119593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!