resources
?目錄下放模板 excel 文件,通過(guò)接口下載后,可以正常下載,但打不開(kāi)。
問(wèn)題:?springboot
?項(xiàng)目簡(jiǎn)單的下載excel
?模板功能,模板放在resources/template/
目錄中
public void downloadItemBatch(HttpServletResponse response) throws IOException {
String fileName = "商品信息.xlsx";
String path = "templates/" + fileName;
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
ServletOutputStream outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
outputStream.close();
inputStream.close();
}
代碼挺簡(jiǎn)單,一運(yùn)行,也挺順利,很快就把文件下好了。點(diǎn)開(kāi)看看,提示我可能是內(nèi)存不足,文件無(wú)法打開(kāi),而且下載的文件比templates里的文件要大。
看了很多帖子,試了很多方法,最后發(fā)現(xiàn),pom文件里加個(gè)東西就行了文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-657225.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<nonFilteredFileExtensions>
<!--不加這一行,xlsx文件會(huì)被過(guò)濾,然后在maven build的時(shí)候,去target下看對(duì)應(yīng)的xlsx就是損壞的-->
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
解決:maven 構(gòu)建時(shí)對(duì)該 excel 模板進(jìn)行了過(guò)濾,導(dǎo)致文件損壞,解決辦法,在過(guò)濾的時(shí)候把 xlsx 排除掉(<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-657225.html
到了這里,關(guān)于JAVA下載Excel文件之后無(wú)法打開(kāi),提示損壞的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!