简体   繁体   English

从打包为jar文件的java程序中的资源中提取图像

[英]Extract image from resources within a java program packaged as a jar file

I have a java program that reads image resources from disk.我有一个从磁盘读取图像资源的 java 程序。

This works well while testing with mvn exec:java .这在使用mvn exec:java进行测试时效果很好。

However, when program is packaged with mvn packaged , it throws the following error:但是,当程序使用mvn packaged ,会抛出以下错误:

java.nio.file.FileSystemNotFoundException java.nio.file.FileSystemNotFoundException


Project structure项目结构

├── java
│   └── com
│       ├── aaa
│       │   ├── bbb
│       │   │   ├── AssemblingTags.java
│       │   │   ├── BatchLifeCycle.java
│       │   │   ├── config
│       │   │   │   ├── AssemblingTagsConfig.java
│       │   │   │   ├── Configuration.java
│       │   │   │   ├── DatabaseConfig.java
│       │   │   │   ├── ExecutionContext.java
│       │   │   │   ├── InjectionTagsConfig.java
│       │   │   │   ├── QualityConfig.java
│       │   │   │   └── TraceabilityContext.java
│       │   │   ├── Database.java
│       │   │   ├── DeviceLifeCycle.java
│       │   │   ├── Devices.java
│       │   │   ├── InjectionTags.java
│       │   │   ├── QRCode.java
│       │   │   ├── Quality.java
│       │   │   ├── Simulator.java
│       │   │   ├── ThreadManager.java
│       │   │   ├── TimeManager.java
│       │   │   └── Utils.java
│       │   └── traceability
│       │       └── Tracer.java
│       └── example
│           ├── AdminAPI.java
│           ├── ConfigAPI.java
│           ├── CORSFilter.java
│           ├── DeviceAPI.java
│           ├── Main.java
│           ├── QrCodeAPI.java
│           ├── ToggleAPI.java
│           └── TracerAPI.java
└── resources
    └── images
        └── strioscopy
            ├── v1-2.png
            ├── v1-3.png
            ├── v2-1.png
            └── v2-2.png

The piece of code that reads the image读取图像的一段代码

Note the prints.注意打印。

try {
            System.out.println("images/" + type + "/" + name); <-- PRINT 1
            URL res = getClass().getClassLoader().getResource("images/" + type + "/" + name);
            System.out.println(res);                           <-- PRINT 2
            File file = Paths.get(res.toURI()).toFile();
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[(int) file.length()];
            fis.read(data);
            fis.close();
            return Response.ok(data).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.serverError().entity(e).build();
        }

When using mvn exec:java , it works and print:使用mvn exec:java ,它可以工作并打印:

images/strioscopy/v1-3.png 
file:/home/hduser/program/target/classes/images/strioscopy/v1-3.png

When executing with the jar ( java -jar myprogram-jar-with-dependencies.jar ), it fails and prints:使用 jar ( java -jar myprogram-jar-with-dependencies.jar ) 执行时,它失败并打印:

images/strioscopy/v1-3.png

jar:file:/home/hduser/program/target/myprogram-jar-with-dependencies.jar!/images/strioscopy/v1-3.png

From my understanding, it is not able to read the images from the jar (as it is "jared").根据我的理解,它无法从罐子中读取图像(因为它是“罐子”)。 But how to fix this problem ?但是如何解决这个问题?

Moreover, notice the exclamation mark here: (between chevrons) jar:file:/home/hduser/program/target/myprogram-jar-with-dependencies.jar>>!<</images/strioscopy/v1-3.png此外,请注意此处的感叹号:(人字形之间) jar:file:/home/hduser/program/target/myprogram-jar-with-dependencies.jar>>!<</images/strioscopy/v1-3.png

Is it normal ?正常吗?

For whatever reason, it gives expected result with the following code:无论出于何种原因,它都会通过以下代码给出预期结果:

try {
            System.out.println("images/" + type + "/" + name);
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("images/" + type + "/" + name);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[1024];
            while ((nRead = is.read(data,0,data.length)) != -1) {
                baos.write(data, 0, nRead);
            }
            baos.flush();
            byte[] ba = baos.toByteArray();
            return Response.ok(ba).build();
        } catch (Exception e) {
            e.printStackTrace();
            return Response.serverError().entity(e).build();
        }

However, I am not able to explain why.但是,我无法解释原因。 (If someone can bring light on this, welcome !) (如果有人可以对此有所了解,欢迎!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM