resource下的excel文件下载被损害
放在resource下面的文件,maven编译打包时会出现损害,具体的话,没细研究,所以只要打包的时候排除指定后缀文件即可了:
下载代码:
@ResponseBody
@RequestMapping(value = "/downloadExample", method = RequestMethod.GET)
@ApiOperation(value = "模板下载")
void downloadExample(@RequestParam("key") String fileName, HttpServletResponse response, HttpServletResponse request) throws Exception {
InputStream inStream = null;
try {
inStream = this.getClass().getResourceAsStream("/example/" + fileName);
} catch (Exception e) {
e.printStackTrace();
return;
}
if (inStream == null) {
response.setStatus(HttpStatus.NOT_FOUND.value());
return;
}
response.reset();
response.setContentType("bin");
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// �ȸ�
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setHeader("Access-control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
response.setStatus(HttpStatus.OK.value());
byte[] b = new byte[1024];
int len;
try {
while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
主要就是打包的时候主pom文件排除指定后缀即可:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>zip</nonFilteredFileExtension>
<nonFilteredFileExtension>cer</nonFilteredFileExtension>
<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
<nonFilteredFileExtension>py</nonFilteredFileExtension>
<nonFilteredFileExtension>keystore</nonFilteredFileExtension>
</nonFilteredFileExtensions>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>**/font/**</exclude>
<exclude>**/fonts/**</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/font/**</include>
<include>**/fonts/**</include>
</includes>
</resource>
</resources>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
世界上所有的不公平都是由于当事人能力不足造成的.