简体   繁体   English

Spring boot,无法读取文件,启动时使用jar包

[英]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.

相关问题 无法读取spring boot包装jar文件 - Can not read spring boot packaging jar file 无法从 spring boot .jar 读取文件 - Unable to read file from spring boot .jar 在IntelliJ中启动Spring Boot jar - Starting Spring Boot jar in IntelliJ Java spring boot jar file error when running maven package command - Java spring boot jar file error when running maven package command Spring 在 docker 上启动无法从资源中读取属性文件,但是可以在没有 Docker 的情况下使用 Z93F725A4jar423D21C28 - Spring boot on docker not able to read a property file from resources, but the can without Docker with java -jar Spring Boot rest controller not working when package in a jar - Spring Boot rest controller not working when package in a jar Pom 文件配置到 package 外部/自定义 jar 文件中的胖 spring 启动可执行文件 Z68904480F43A29DAC41 - Pom file configuration to package external/custom jar file in the fat spring boot executable jar file 为什么在运行 Spring 启动应用程序的 .jar 版本时无法检索此文件? - Why I can't retrieve this file when I run the .jar version of my Spring Boot application? 当我用Jenkins用Maven构建Spring Boot应用程序并执行JAR文件时,找不到类定义? - Can not find class define, when I build a Spring Boot application with Maven by Jenkins and execute JAR file? Spring Boot:如何使用@PropertySource指向jar外部的文本文件? - Spring Boot : how to use @PropertySource to point to a text file outside the jar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM