【发布时间】:2015-10-25 06:43:10
【问题描述】:
在我的 IDE 中,我可以获取资源文件夹中图像的路径,并通过执行以下操作使该路径成为新的文件对象:
URL imagePath = getClass().getResource("/image.png");
try
{
//Convert the URLs into URIs and make a file object with that path
File image = new File(imagePath.toURI());
}
catch (URISyntaxException e)
{
e.printStackTrace();
}
但是当我为我的程序制作一个 jar 文件时,我得到错误 URI 不是分层的。我做了一些研究,发现我必须使用 getResourceAsStream() 方法创建一个 InputStream。但我不知道如何使它适用于图像。我只需要能够从我的资源文件夹中获取图像的路径。即使它是一个罐子,我该怎么做。
【问题讨论】: