当前位置:   article > 正文

springboot类路径下excel、word文件下载为空和打不开记录_springboot templates 读取的xlsx 是空的

springboot templates 读取的xlsx 是空的

下载为空问题原因

下载为空这个原因很简单,新同事写的下载是这样写的,这种只能在本地用下,打包上线后就g了…

	  String fileName = "test.xlsx";
      String filePath = System.getProperty("user.dir")+"\\src\\main\\resource\\staitc\\"+fileName;
      File file = new File(filePath);
      FileInputStream fileInputStream = new FileInputStream(file);

      IOUtils.copy(fileInputStream, response.getOutputStream());
      response.setContentType("application/vnd.ms-excel;charset=UTF-8");
      response.setHeader("Content-disposition","attachment:fileName="+ URLEncoder.encode(fileName,"UTF-8"));
      response.setContentLength(fileInputStream.getChannel().size());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这个问题不大,只要换种方式读取类路径下文件即可,

InputStream resourceAsStream = this.getClass().getResourceAsStream("/static/" + fileName);
  • 1

重点是下面这个打不开的问题

下载打不开问题现象和原因

在springboot工程的类路径下 /static/ 目录下创建了一个模板excel文件,比如 template.xlsx,写的文件下载功能将这个文件下下来的时候无法打开,提示内容已损坏。

一开始我以为是下载的写法有问题,比如响应头的content-type设置不对,content-length没有设置等问题导致的,最终发现是类路径下的excel编译后,会变成原来的大

出现原因是因为同事在 pom.xml 中配置了 配置了

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
        </profile>
		// 省略几个 profile
        <profile>
            <id>prod</id>
            <properties>
                <profileActive>prod</profileActive>
            </properties>
        </profile>

    </profiles>


    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

项目中按照不同的环境写了多个环境的配置文件

-application.yml
-application-dev.yml
-application-test.yml
-application-prod.yml
  • 1
  • 2
  • 3
  • 4

在 application.yml 中

spring:
	profiles:
		active: @profileActive@
  • 1
  • 2
  • 3

此时因为pom配置的true,所以会导致其上配置的directory配置的路径下的资源都会经过maven一次处理,会将其内的@@包含的变量使用其定义的变量替换。

        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
        </profile>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

我这里配置的默认激活的是dev的配置,所以默认会使用 dev这个变量去覆盖所有类路径下的 @profileActive@,也即打包后的application.yml如下

spring:
	profiles:
		active: dev
  • 1
  • 2
  • 3

而在处理excel、word等文件时,处理会其会变大且无法打开…不清楚怎么造成变大。

解决方案

在maven的 maven-resources-plugin 插件中添加配置 xlsx,表示不对xlsx为后缀的文件处理,点击该标签可以看到其默认已经对于图片文件进行处理,所以图片文件大小不会变。

Additional file extensions to not apply filtering (already defined are : jpg, jpeg, gif, bmp, png)
  • 1
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
    <encoding>utf-8</encoding>
    <useDefaultDelimiters>true</useDefaultDelimiters>
    <nonFilteredFileExtensions>xlsx</nonFilteredFileExtensions>
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

该配置项是个集合类型,可以以逗号分隔配置多个:比如 xlsx,docx

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/815969
推荐阅读
相关标签
  

闽ICP备14008679号