2016-06-01 11 views
0

STS、TomcatのgetClass().getResource('...').getPath()を使ってディレクトリやファイルを読み込み、埋め込まれたTomcatを使って端末からJAR/WARファイルを実行するという、とても簡単なプロジェクトを作成しました。Spring Resource Inside JAR/WAR

プロジェクトはシンプルですが、私が言ったように、ここでのコードは次のとおりです。

package org.example 

import org.springframework.beans.factory.annotation.Autowired 
import org.springframework.boot.CommandLineRunner 
import org.springframework.boot.SpringApplication 
import org.springframework.boot.autoconfigure.SpringBootApplication 

@SpringBootApplication 
class ResourceDemoApplication implements CommandLineRunner { 

    static void main(String[] args) { 
     SpringApplication.run ResourceDemoApplication, args 
    } 

    @Override 
    void run(String... arg0) throws Exception { 
     retrieveDirectory() 
    } 

    void retrieveDirectory() { 
     /*new File(getClass().getResource('/private/folders').getPath()).eachDirRecurse() { dir -> 
      dir.eachFileMatch(~/.*.txt/) { file -> 
       println(file.getPath()) 
      } 
     }*/ 
     println new File(getClass().getResource('/private/folders/').getPath()).isDirectory() 
    } 
} 

このコードは、STSで実行または私は実行中のTomcatインスタンスにドロップした場合、それはtrueを印刷した場合。 java -jar...として実行すると、端末にfalseが返されます。私は数え切れないほどの例を見てきましたが、これを正しく、あるいは期待通りに動作させる方法をまだ理解していません。私は、JARの内部からファイルを読むことはファイルシステムにアクセスすることとは異なることを知っていますが、どのように展開されているかに関係なく、これを動作させる方法はわかりません。

ありがとうございました!

+0

すでにjarファイルはファイルではありません、あなたがそのように読むことができません述べたように。それは、戦争が解凍されてから再びファイルリソースになるため、コンテナ内で動作します。 –

+0

jarファイルからファイル/ディレクトリを読み取ることはできませんか? –

+0

もちろん、ファイルとしてはできませんが、ファイルがどこに格納されているかにかかわらず動作する 'InputStream'を使用してください。 –

答えて

1

はかなり研究のビットとコードに掘り後、私はこの解決策になってしまった:グルーヴィーで

package org.example 

import org.springframework.boot.CommandLineRunner 
import org.springframework.boot.SpringApplication 
import org.springframework.boot.autoconfigure.SpringBootApplication 
import org.springframework.core.io.FileSystemResource 
import org.springframework.core.io.support.PathMatchingResourcePatternResolver 

@SpringBootApplication 
class ResourceDemoApplication implements CommandLineRunner { 

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver() 

    static void main(String[] args) { 
     SpringApplication.run ResourceDemoApplication, args 
    } 

    @Override 
    void run(String... arg0) throws Exception { 
     retrieveDirectory() 
    } 

    void retrieveDirectory() { 
     List<FileSystemResource> files = resolver.findPathMatchingResources('private/folders/**/example.txt') 

     files.each { file -> 
      println file.getInputStream().text 
     } 
    } 
} 

あなたがなどの型を宣言する必要はありません...私はそれをやっていますコード内で何が起きているのかを示すためにここに書類を用意しています。あなたはJavaでこれを行う場合は、println file.getInputStream().textを置き換えるために、このようなものが必要になります。

InputStream is 
BufferedReader br 
String fileContents 
files.each { file -> 
    is = file.getInputStream() 
    br = new BufferedReader(new InputStreamReader(is)) 

    String line 
    fileContents = "" 
    while((line = br.readLine()) != null) { 
     fileContents += line 
    } 
    println fileContents 
    println "************************" 
    br.close() 
} 
+0

これでいい仕事です。 –