public void downloadExcel(HttpServletResponse response) {
InputStream bis = null;
BufferedOutputStream out = null;
try {
String fileName = "批量新增模板.xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("multipart/form-data");
bis = new ClassPathResource("/template/template.xlsx").getInputStream();
out = new BufferedOutputStream(response.getOutputStream());
int len;
while ((len = bis.read()) != -1) {
out.write(len);
out.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
解决:pom文件中加入如下这个插件可以避免xlsx文件在resource目录下被自动压缩
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.6</version>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>