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下载excel文件 2021-09-15
- java实现下载hdfs文件及文件夹 2021-11-30
- JAVA SFTP文件上传、下载及批量下载 2021-11-13
- JAVA代码实现下载单个文件,和下载打包文件 2021-08-07
- java文件下载 - 我俩绝配 2021-12-13
- 【Java】Java批量文件打包下载zip 2021-11-04
- java文件下载文件名中文乱码问题 2021-11-24
- 文件下载——下载Excel 2021-09-15