[英]Spring boot, The file can not be read, Use jar package when starting
Spring boot, The file can not be read, Use jar package when starting. 春季启动,无法读取文件,启动时使用jar包。 When I use the .war start the way everything is normal
当我使用.war启动时,一切正常
This is war maven pom.xml 这是战争行家pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>p12</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
But use java -jar
但是使用
java -jar
I want to start with java -jar, Now what do I need to do? 我想从java -jar开始,现在我需要做什么? spring boot version 1.4.4
春季启动版本1.4.4
public void generater(HttpServletResponse response, boolean testMode) {
ExcelTemplateEnum excelTemplate = this.exportTemplate();
FileInputStream is = null;
Workbook workbook = null;
try {
String templateFilePath = this.getTemplateFilePath(excelTemplate);
URL url = ExeclExportHandler.class.getClassLoader().getResource(templateFilePath);
is = new FileInputStream(url.getFile());
ExcelTransformer transformer = this.getExcelTransformer();
// 渲染导出数据
if (isMultipleBeans()) {
List<Map<String, Object>> data = this.renderDataList();
this.checkExportParam(data);
workbook = transformer.transform(is, getTemplateSheetNamesList(), getSheetNamesList(), data);
} else {
Map<String, Object> data = this.renderData();
this.checkExportParam(data);
workbook = transformer.transform(is, data);
}
if (testMode) {
...
} else {
ExcelHelper.setExcelHeader(response, excelTemplate.outname());
workbook.write(response.getOutputStream());
response.flushBuffer();
}
} catch (Exception e) {
...
} finally {
IOUtils.closeQuietly(is, workbook);
}
}
error line 75
错误线75
is = new FileInputStream(url.getFile());
You have to access the resource by Class.getResourceAsStream()
, since you can't access files packed in a jar: 您必须通过
Class.getResourceAsStream()
访问资源,因为您无法访问打包在jar中的文件:
is = this.getClass().getResourceAsStream(templateFilePath)
If it works with a war it is just luck, since your servlet/J2EE-container seems to unpack your war... which is not the case with all containers (due to the spec it is up to the container whether it will unpack the the war or not). 如果与战争兼容,那只是运气,因为您的servlet / J2EE-container似乎可以解开战争的包装……并非所有容器都这样(由于规范,取决于是否对容器进行包装)战争与否)。 Means with a different container you will get also get this Exception with the war.
意味着使用不同的容器,您还将在战争中得到此Exception。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.