问题下载excel文件打开时提示有不可识别内容
下载Excel老是提示说有不可识别内容!然后去class文件查看文件并打开发现打开也提示有不可识别内容,而且文件大小也变了,说明变异的时候把excel文件也编译进去了。
代码:
下载格式的在线文档
/**
* 合作伙伴批量导入模板下载
*
* @param response
* @return
*/
@GetMapping("/downLoadExcelFiles")
public void downLoadExcelFiles(HttpServletResponse response, HttpServletRequest request) throws IOException {
try (InputStream resourceAsStream = new ClassPathResource("/static/moban/test.xlsx").getInputStream()) {
response.setContentType("application/force-download");
response.setHeader("Content-disposition", "attachment;filename=" + "151515.xlsx");
OutputStream outputStream = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024 * 1024];
while ((len = resourceAsStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
} catch (Exception e) {
log.error("下载失败:{}", e.getMessage());
}
}
解决办法:
不编译excel,在pom的过滤中加不编译excel文件
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering> //意识是不编译resources文件下的所有文件
<includes>
<include>**/*.xlsx</include>
</includes>
</resource>
</resources>
<build>
<finalName>kj-manager</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/*.xlsx</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.2.RELEASE</version> <!-- 默认版本2.1.1打包失败,缺失某个类 -->
<configuration>
<mainClass>com.fcar.kj.manager.AppKjManager</mainClass>
</configuration>
<!-- 执行了这个插件之后,在 target 目录下生成两个Jar包,original是源码,jar是可执行的 -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>