【问题标题】:Error reading xml file from resources in Heroku [duplicate]从 Heroku 中的资源读取 xml 文件时出错 [重复]
【发布时间】:2018-09-10 13:48:56
【问题描述】:

错误:

java.io.FileNotFoundException: class path resource [xml/ruleSet.xml] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/app/target/******-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/xml/ruleSet.xml

代码:

File ruleSet = new ClassPathResource("xml/ruleSet.xml").getFile();
pmdConfiguration.setReportFormat("text");
pmdConfiguration.setRuleSets(ruleSet.getAbsolutePath());

我需要将完整文件插入到 setRuleSets 方法中,FileInputStream 将如何帮助我?

FileInputStream ruleSet = new FileInputStream(ClassLoader.getSystemResource
                    ("xml/ruleSet.xml").getPath());

我是否应该通过读取文件输入流来重新创建一个临时文件并将该文件路径传递给setRuleSets 方法?

【问题讨论】:

    标签: java heroku fileinputstream


    【解决方案1】:

    根本问题是在文件系统命名空间中没有路径JAR 文件中的资源。这就是ClassPathResource::getFile 抛出异常的原因,也是URL::getPath 的原因。

    还有一个选择。使用 InputStream,而不是 FileInputStream,并使用类加载器 API 方法将资源作为流打开。

    变化:

        FileInputStream ruleSet = new FileInputStream(ClassLoader.getSystemResource
                        ("xml/ruleSet.xml").getPath());
    

        InputStream ruleSet = ClassLoader.getSystemResourceAsStream("xml/ruleSet.xml");
    

    在这种情况下,这是行不通的,因为PMDConfiguration::setRuleSets 不接受流参数。

    但是,javadoc 声明:

    public void setRuleSets(String ruleSets)
    

    设置命令1 分隔的 RuleSet URI 列表。

    由于getResource 方法返回一个 URL,您应该能够做到这一点:

        pmdConfiguration.setRuleSets(
            ClassLoader.getSystemResource("xml/ruleSet.xml").toString();
    

    更新

    如果getSystemResourcegetSystemResourceAsStream 失败,则资源不存在(在运行时类路径上的JAR 等中),路径不正确,或者您应该使用getResourcegetResourceAsStream


    1 - “命令”这个词是一个错字。它应该是“逗号”。

    【讨论】:

    • 将 toString 添加到 "ClassLoader.getSystemResource("xml/ruleSet.xml").toString()" 失败,我也尝试过 pmdConfiguration.setRuleSets( ClassLoader.getSystemResource("xml/ruleSet.xml").getPath()); 它在本地通过但在 heroku 中失败。 System.out.println(ClassLoader.getSystemResource("xml/ruleSet.xml")); System.out.println(ClassLoader.getSystemResourceAsStream("xml/ruleSet.xml"));两者都返回 null。
    【解决方案2】:

    好的,所以我想出了在这里做什么,

    我们已经有了输入流,因此我将输入流转换为一个临时文件,并使用了那个 file.getPath。

    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream resourceAsStream = classLoader.getResourceAsStream("xml/ruleSet.xml");
    String ruleSetFilePath = "";
    if(resourceAsStream != null){
        File file = stream2file(resourceAsStream);
        ruleSetFilePath = file.getPath();
    
    }
    pmdConfiguration.setRuleSets(ruleSetFilePath);
    
    public static File stream2file (InputStream in) throws IOException {
    final File tempFile = File.createTempFile("ruleSet", ".xml");
    tempFile.deleteOnExit();
    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(in, out);
    }
    return tempFile;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 2023-03-24
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多