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();
}
}
}
}