SpringBoot 下的Excel文件损坏与内容乱码问题

序言

随着打包部署的方式的改变,原本正常运行的代码可能带来一些新的问题,比如我们现在使用SpringBoot 的方式生成Jar包直接运行,就会对我们再在Resource下的Excel文件产生影响,导入与预期不符的情况发生[email protected]

比如:我们会在工程中提供一些模板(Excel文件),然后供前端调用下载,但是下载后内容时乱码,或者不能正常的打开该文件

打包问题

我们在通过编译后发现 放置在target目录下的excel文件打不开了。因为原本文件就打不开了,所以你在后期下载的时候肯定就有问题。

原因:SpringBoot会对resources下文件进行压缩,导致word,excel格式异常[email protected]

增加如下的配置,告诉Springboot 相关的文件不要压缩

XML 复制代码
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <configuration>
         <nonFilteredFileExtensions>
             <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
             <nonFilteredFileExtension>docx</nonFilteredFileExtension>
         </nonFilteredFileExtensions>
     </configuration>
 </plugin>

关于Excel等特殊文件的以流的方式下载的问题

看如下我们经常使用的文件下载或者复制的代码

java 复制代码
public static void main(String[] args) {
            String inputFileName = "C:\\[email protected]\\123.xlsx"; // 输入文件名
            String outputFileName = "C:\\[email protected]\\copy-123.xlsx"; // 输出文件名

            try (FileReader fr = new FileReader(inputFileName);
                 FileWriter fw = new FileWriter(outputFileName)) {
                char[] buffer = new char[1024]; // 缓冲数组
                int length;
                while ((length = fr.read(buffer)) != -1) {
                    fw.write(buffer, 0, length); // 将读取的内容写入输出文件
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

如果是文本文件txt,csv.什么的完全没问题,但是如果是Excel这种文件,内容就会是乱码或者显示文件已经损坏

那我们再换一种方式来考本文件

java 复制代码
  public static void main(String[] args) throws IOException {
        try {
            FileOutputStream os = new FileOutputStream(new File("C:\\\\[email protected]\\\\123.xlsx"));
            FileInputStream resource = new FileInputStream("C:\\\\[email protected]\\\\copy-123.xlsx");
            FileCopyUtils.copy(resource, os);
            System.out.print("SUCEESS");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

如上的拷贝就没有任何问题,那我们看看FileCopyUtils.copy(resource.getInputStream(), os);的源码给你我们的byte[] 字节数组拷贝的区别是什么.

因为InputStream 或者OutputStream 本身就是字节流不涉及什么编码格式.,像FileInput ,FileOut就会设计编码格式,但是我们在创建他们的时候还不能直接设置编码格式,需要经过一圈的包装转换才能设置

复制代码
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (filePath,true),"UTF-8"));

FileWriter writer = new FileWriter(filePath,true);
相关推荐
李白的粉3 小时前
基于springboot的在线教育系统
java·spring boot·毕业设计·课程设计·在线教育系统·源代码
小马爱打代码4 小时前
SpringBoot原生实现分布式MapReduce计算
spring boot·分布式·mapreduce
iuyou️4 小时前
Spring Boot知识点详解
java·spring boot·后端
一弓虽4 小时前
SpringBoot 学习
java·spring boot·后端·学习
姑苏洛言5 小时前
扫码小程序实现仓库进销存管理中遇到的问题 setStorageSync 存储大小限制错误解决方案
前端·后端
光而不耀@lgy5 小时前
C++初登门槛
linux·开发语言·网络·c++·后端
方圆想当图灵5 小时前
由 Mybatis 源码畅谈软件设计(七):SQL “染色” 拦截器实战
后端·mybatis·代码规范
毅航6 小时前
MyBatis 事务管理:一文掌握Mybatis事务管理核心逻辑
java·后端·mybatis
我的golang之路果然有问题6 小时前
速成GO访问sql,个人笔记
经验分享·笔记·后端·sql·golang·go·database
柏油6 小时前
MySql InnoDB 事务实现之 undo log 日志
数据库·后端·mysql