2015-04-15 121 views
0

正如所讨论的https://stackoverflow.com/a/22632251/379235,我说我的代码资源文件无法找到

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

,并在我的jar文件使用它作为

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

我可以找到这个文件,以及

$ 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 

我缺少什么?

回答

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();