佛山企业网站建设技术,1 网站建设的目标是什么,移动互联网开发的前景,企业网站建设应该序言
随着打包部署的方式的改变#xff0c;原本正常运行的代码可能带来一些新的问题#xff0c;比如我们现在使用SpringBoot 的方式生成Jar包直接运行#xff0c;就会对我们再在Resource下的Excel文件产生影响#xff0c;导入与预期不符的情况发生cuiyaonan2000163.com
比…序言
随着打包部署的方式的改变原本正常运行的代码可能带来一些新的问题比如我们现在使用SpringBoot 的方式生成Jar包直接运行就会对我们再在Resource下的Excel文件产生影响导入与预期不符的情况发生cuiyaonan2000163.com
比如我们会在工程中提供一些模板Excel文件然后供前端调用下载但是下载后内容时乱码或者不能正常的打开该文件
打包问题
我们在通过编译后发现 放置在target目录下的excel文件打不开了。因为原本文件就打不开了所以你在后期下载的时候肯定就有问题。
原因SpringBoot会对resources下文件进行压缩导致wordexcel格式异常cuiyaonan2000163.com
增加如下的配置告诉Springboot 相关的文件不要压缩 plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-resources-plugin/artifactIdconfigurationnonFilteredFileExtensionsnonFilteredFileExtensionxlsx/nonFilteredFileExtensionnonFilteredFileExtensiondocx/nonFilteredFileExtension/nonFilteredFileExtensions/configuration/plugin关于Excel等特殊文件的以流的方式下载的问题
看如下我们经常使用的文件下载或者复制的代码
public static void main(String[] args) {String inputFileName C:\\cuiyaonan2000163.com\\123.xlsx; // 输入文件名String outputFileName C:\\cuiyaonan2000163.com\\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这种文件,内容就会是乱码或者显示文件已经损坏 那我们再换一种方式来考本文件 public static void main(String[] args) throws IOException {try {FileOutputStream os new FileOutputStream(new File(C:\\\\cuiyaonan2000163.com\\\\123.xlsx));FileInputStream resource new FileInputStream(C:\\\\cuiyaonan2000163.com\\\\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);