简体   繁体   English

如何从战争包装的罐子中读取PNG

[英]How to Read PNG from a jar packaged in a war

I am trying to write some code that allows me to access a file (specifically EMailBanner.png) that is wrapped as a jar and then included in a war. 我正在尝试编写一些代码,以使我能够访问文件(特别是EMailBanner.png),该文件包装为jar,然后包含在战争中。

The code I have cobbled together is as follows; 我整理的代码如下:

public static File getFile(String imagePath){

    if(StringUtilities.stringEmptyOrNull(imagePath)){
        throw new IllegalArgumentException("Invalid image path");
    }

    File tempFile = null;
    InputStream is = null;
    FileOutputStream fos = null;
    try{
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        is = classLoader.getResourceAsStream(imagePath);
        tempFile = File.createTempFile("EMailBanner", ".png"); 
        tempFile.deleteOnExit();  
        fos = new FileOutputStream(tempFile); 
        byte[] buf = new byte[1024];  
        int len;  
        while ((len = is.read(buf)) != -1) {  
            fos.write(buf, 0, len);  
        }
    }catch(IOException e ){
        LOGGER.error("Unable to load image", e);
    }catch(Exception e){
        LOGGER.error("Unable to load image", e);
    }finally{
        try {   
            fos.close();
            is.close();
        } catch (IOException e) {
            LOGGER.warn("Unable to close the file input / file output streams", e);
        }
    }
    return tempFile;
}

The issue I am facing is that when deployed on to the development box as a war file - the application cannot find the png file. 我面临的问题是,当作为战争文件部署到开发箱时,应用程序找不到png文件。 If I run locally in eclipse it isn't a problem. 如果我在Eclipse中本地运行,这不是问题。

Whats strange is I have a number of properties files in the resources folder as you can see from the image below; 奇怪的是,从下图中可以看到,资源文件夹中有许多属性文件;

在此处输入图片说明

I have no problems loading those from within the jar file - loaded like this; 我从jar文件中加载这些文件没有问题-这样加载;

public static Properties getDatabaseConnectionProps(ApplicationName appName) throws IOException{

    if(appName == null){
        throw new IllegalArgumentException("Path to proeprties file was null or empty");
    }

    Properties props = null;

    try(InputStream resourceStream = DatabaseUtilities.class.getResourceAsStream("/vimba.properties")) {
        if(resourceStream != null){
            props = new Properties();
            props.load(resourceStream);
            return props;
        }else{
            throw new IllegalArgumentException("In invalid properties file path was provided");
        }
    } catch (IOException e) {
        throw e;
    }
}

So why would one approach work and potentially not the other? 那么,为什么一种方法可行,而另一种可能无效? I am completely out of alternative options so really hope someone can save the day 我完全没有其他选择,所以我真的希望有人可以度过美好的一天

Thanks 谢谢

I have just tested something similar on my local machine using your code. 我刚刚使用您的代码在本地计算机上测试了类似的东西。 It seems to work fine from what I can see. 从我所看到的看来,它工作正常。

The only other issue I can see is - if you check the JAR (you can de-compile it), make sure the image you are trying to retrieve is in there and that the filename matches. 我能看到的唯一另一个问题是-如果检查JAR(可以反编译),请确保要尝试检索的图像位于其中,并且文件名匹配。

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

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