1

プロジェクト内に238枚の画像を含むフォルダーがあります。ディレクトリ内のすべての画像を検索できるようにしたい。

私は現在、次のようにこれらすべての画像にアクセスしています。

File directory = new File(FileNameGuesser.class.getResource(DIRECTORY).getPath());
for (File file : directory.listFiles()) {
    // filter, process, etc.
}

これはEclipse内で正常に機能します。ただし、jarファイルにエクスポートすると、(zip形式であるため)FileNameGuesser.class.getResource(DIRECTORY)戻り、メソッドが機能しなくなります。C:\Users\...\file.jar!\org\...

どうすればこれを達成できますか?

編集:可能であれば、Eclipseデプロイされたjarの両方で機能するソリューションを見つけたいと思います。

4

2 に答える 2

2

これは、きれいできれいな方法では実際には不可能です。

.getClass().getResources("/pictures/*.jpg")(または何か)ができればいいのですが、できません。

あなたができる最善のことは、少しごまかすことです。画像が保存されている jar ファイルの名前がわかっている場合は、JarFileまたはZipFileAPI を使用してリストを取得できます。

ZipFile zipFile = null;
try {

    zipFile = new ZipFile(new File("...")); // Path to your Jar
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {

        ZipEntry entry = entries.nextElement();

        // Here, I've basically looked for the "pictures" folder in the Zip
        // You will need to provide the appropriate value
        if (!entry.isDirectory() && entry.getName().startsWith("pictures")) {

            // Basically, from here you should have the full name of the
            // image.  You should be able to then construct a resource path
            // to the image to load it...

            // URL url = getClass().getResource("/" + entry.getName());
            System.out.println(entry.getName());

        }

    }

} catch (Exception exp) {

    exp.printStackTrace();

} finally {


    try {
        zipFile.close();
    } catch (Exception e) {
    }

}

事前に名前がわからない場合は、これらの画像を jar に埋め込まないことをお勧めします。

組み込みリソースは、事前にアプリケーションに名前で認識されている必要があります。

AndrewThompson が提案したように、リソースのリストを生成し、これを Jar ファイルに追加できます。実行時にこのファイルをロードし、すべてのリソースにアクセスできます。

于 2012-08-18T04:22:58.200 に答える
0

PathMatchingResourcePatternResolverSpringが提供するものを使用できます。

public class SpringResourceLoader {

    public static void main(String[] args) throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        // Ant-style path matching
        Resource[] resources = resolver.getResources("/pictures/**");

        for (Resource resource : resources) {
            System.out.println("resource = " + resource);
            InputStream is = resource.getInputStream();
            BufferedImage img =  ImageIO.read(is);
            System.out.println("img.getHeight() = " + img.getHeight());
            System.out.println("img.getWidth() = " + img.getWidth());
        }
    }
}

私は戻ってきたものに特別なことは何もしませんでしたResourceが、あなたは写真を手に入れました。

これをMaven依存関係に追加します(Mavenを使用している場合):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>

これは、Eclipse / NetBeans / IntelliJ内から、およびデプロイされたjar内から直接機能します。

IntelliJ内から実行すると、次の出力が得られます。

resource = file [C:\Users\maba\Development\stackoverflow\Q12016222\target\classes\pictures\BMW-R1100S-2004-03.jpg]
img.getHeight() = 768
img.getWidth() = 1024

実行可能jarを使用してコマンドラインから実行すると、次の出力が得られます。

C:\Users\maba\Development\stackoverflow\Q12016222\target>java -jar Q12016222-1.0-SNAPSHOT.jar
resource = class path resource [pictures/BMW-R1100S-2004-03.jpg]
img.getHeight() = 768
img.getWidth() = 1024
于 2012-08-18T05:55:26.610 に答える