办公设备维修网
资讯中心 您所在的位置:网站首页 资讯中心 【JAVA下载无法打开】JAVA下载Excel文件之后无法打开,提示损坏

【JAVA下载无法打开】JAVA下载Excel文件之后无法打开,提示损坏

2024-06-22 18:52:49| 来源: 网络整理

resources 目录下放模板 excel 文件,通过接口下载后,可以正常下载,但打不开。

问题: springboot 项目简单的下载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(); }

代码挺简单,一运行,也挺顺利,很快就把文件下好了。点开看看,提示我可能是内存不足,文件无法打开,而且下载的文件比templates里的文件要大。

看了很多帖子,试了很多方法,最后发现,pom文件里加个东西就行了

org.apache.maven.pluginsmaven-resources-plugin2.7xlsx

解决:maven 构建时对该 excel 模板进行了过滤,导致文件损坏,解决办法,在过滤的时候把 xlsx 排除掉(xlsx)。



【本文地址】 转载请注明 

最新文章

推荐文章

CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备16040606号-1