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>)。

相关推荐
许彰午3 小时前
95_Python内存管理与垃圾回收
开发语言·python
csdn2015_3 小时前
springboot读取配置的方法
java·spring boot·spring
骄阳如火4 小时前
Python 性能深度剖析:从“被诟病的慢”到“Rust 重塑”的拐点
python
AOwhisky4 小时前
下一代容器来了?Docker 宣布原生支持 WebAssembly
java·运维·docker·容器·rust·wasm
满怀冰雪4 小时前
03-第一个 Paddle 程序:Tensor 创建、计算与设备管理
人工智能·python·paddle
CClaris4 小时前
大模型量化从0到1(九):用 llama.cpp 把模型转成 GGUF 并跑本地推理
人工智能·pytorch·python·深度学习·llama
学编程的小虎5 小时前
SenseVoice微调
人工智能·python·自然语言处理
诸葛说抛光5 小时前
国内大型汽车改装展览会定展 佛山改装 佛山汽车赛事
python·汽车
chouchuang5 小时前
day-030-综合练习-笔记管理器
开发语言·笔记·python
乖巧的妹子5 小时前
Python基础核心知识点详解:内置函数、运算符、字符串方法、数据结构与类型转换
python