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

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

问题: springboot 项目简单的下载excel 模板功能,模板放在resources/template/目录中

java 复制代码
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文件里加个东西就行了

XML 复制代码
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>2.7</version>
	<configuration>
		<nonFilteredFileExtensions>
			<!--不加这一行,xlsx文件会被过滤,然后在maven build的时候,去target下看对应的xlsx就是损坏的-->
			<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
		</nonFilteredFileExtensions>
	</configuration>            
</plugin>

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

相关推荐
Hi李耶1 分钟前
【LeetCode】15.三数之和
java·算法·leetcode
DogDaoDao16 分钟前
【第10篇】Python 异常处理与调试入门
python·深度学习·ai·程序员·大模型·异常处理与调试
蔬菜_28 分钟前
前端转全栈-day6(构造函数与继承)
java
程序员雷欧29 分钟前
Java 反射深度解析:从原理到源码的全面剖析
java·开发语言·python
登登登__38 分钟前
春秋招笔试题总结
java·开发语言
rannn_1111 小时前
【力扣hot100】链表专题|21、2、19、24、92、25
java·数据结构·算法·leetcode·链表·开发
我的xiaodoujiao1 小时前
快速学习Python基础知识详细图文教程18--多线程
开发语言·python·学习·测试工具
汉拓3D数字化1 小时前
设计标准化怎么落地?企业设计标准化的5阶段实施路径
科技·excel·系统·软件
Rabitebla1 小时前
C++11 新特性详解(一):列表初始化、initializer_list 与右值引用
java·开发语言·数据结构·c++·算法·leetcode·list
洋不写bug2 小时前
JavaComparable、Comparator、Cloneable接口解析,深拷贝浅拷贝详细解析
android·java·开发语言