找不到资源文件

时间:2015-04-15 19:27:33

标签: java spring

正如https://stackoverflow.com/a/22632251/379235所述,我将代码添加为

  @Value("classpath:lpLogCompressFileLocations")
  private Resource logLocations;

并将其用作

  Compressor getCompressor() throws IOException {
    System.out.println("logLocationsFileExists:" + logLocations.getFile().exists());
    return new Compressor(".", logLocations.getFile(), ZIP_NAME_PREFIX);
  }

在我的jar文件中我也可以找到这个文件

$ jar -tvf target/original-Processor.jar  | grep lpLog
    60 Wed Apr 15 12:19:02 PDT 2015 lpLogCompressFileLocations

但是当我部署此代码并尝试访问时,我得到了

15 Apr 2015 12:17:56,530 [DEBUG] [http-bio-8443-exec-3] ReportSender             | Error in starting compression: class path resource [lpLogCompressFileLocations] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/shn/lp/original-LogProcessor.jar!/lpLogCompressFileLocations

我也尝试将代码更改为

@Value("classpath:/lpLogCompressFileLocations")
private Resource logLocations;

但错误相同

15 Apr 2015 12:19:30,984 [DEBUG] [http-bio-8443-exec-1] ReportSender             | Error in starting compression: class path resource [lpLogCompressFileLocations] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/shn/lp/original-LogProcessor.jar!/lpLogCompressFileLocations

我错过了什么?

1 个答案:

答案 0 :(得分:0)

您无法获取jar文件中存在的资源的文件处理程序。这里解释一下。 Java Jar file: use resource errors: URI is not hierarchical

我不确定这是否能解决您的目的,但您可以访问以下内容。

@Value("classpath:lpLogCompressFileLocations")
  private Resource logLocations;

在您的方法中(示例代码)

BufferedReader reader = new BufferedReader(new InputStreamReader(this.logLocations.getInputStream()));
        StringBuilder out = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
        System.out.println(out.toString()); // Prints the string content read from input stream
        reader.close();