Get all images within directory - within jar file

前端 未结 2 500
挽巷
挽巷 2021-01-25 14:07

I have a folder inside my project that has 238 images. I want to be able to find all images within the directory.

I\'m currently accessing all these images like this:

相关标签:
2条回答
  • 2021-01-25 15:01

    You can use the PathMatchingResourcePatternResolver provided by Spring.

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

    I didn't do anything fancy with the returned Resource but you get the picture.

    Add this to your maven dependency (if using maven):

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

    This will work directly from within Eclipse/NetBeans/IntelliJ and in the jar that's deployed.

    Running from within IntelliJ gives me the following output:

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

    Running from command line with executable jar gives me the following output:

    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
    
    0 讨论(0)
  • This isn't really possible in any nice and clean way.

    It would be nice to be able to do .getClass().getResources("/pictures/*.jpg") (or something), but we can't.

    The best you can do is cheat a little. If you know the name of the jar file where the images are stored, you can use either the JarFile or ZipFile APIs to get a listing:

    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) {
        }
    
    }
    

    A better solution would be not to embedded these images in your jar, if you don't know their names in advance.

    Embedded resources really should be known by name to your application in advance.

    As suggested by AndrewThompson, you could generate a list of resources, add this to your Jar file. At runtime, you would load this file and have access to all the resources.

    0 讨论(0)
提交回复
热议问题