首页 > 编程语言 > 详细

java 下载文件

时间:2018-12-14 16:57:10      阅读:115      评论:0      收藏:0      [点我收藏+]
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 下载文件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!