Java 使用InputStream加载图像
在我的IDE中,我可以获取资源文件夹中图像的路径,并通过执行以下操作使该路径成为新的文件对象:Java 使用InputStream加载图像,java,jar,Java,Jar,在我的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 (URISyntaxExcept
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。但我不知道如何使这项工作的形象。我只需要能够从我的资源文件夹中获取图像的路径。即使它是一个jar,我该怎么做呢?不要将
URL
转换为文件
引用,这就违背了拥有嵌入式资源的意义,嵌入式资源只是zip文件中的条目,因此它们不能被视为文件
相反,使用类似于ImageIO.read(imagePath)
有关更多详细信息,请参见不要将
URL
转换为文件
引用,这会破坏嵌入资源的意义,而嵌入资源只是zip文件中的条目,因此不能将其视为文件
相反,使用类似于ImageIO.read(imagePath)
有关更多详细信息,请参见我认为在这种情况下最好的解决方案是直接向
类加载器
请求输入流
(使用)并将其传递给
这是一个完整的例子
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public final class Main {
public static void main(final String[] args) {
final ClassLoader clsldr = Main.class.getClassLoader();
for (final String path : args) {
try {
InputStream is = null;
BufferedImage image = null;
try {
is = clsldr.getResourceAsStream(path);
if (is != null) {
image = ImageIO.read(is);
if (image != null) {
// Do something with the image.
System.out.printf("%s: %d x %d%n",
path,
image.getWidth(),
image.getHeight());
} else {
System.err.printf("error: %s: %s%n",
path,
"not a valid image file");
}
} else {
System.err.printf("error: %s: %s%n",
path,
"no such resource");
}
} finally {
if (is != null) {
is.close();
}
}
} catch (final IOException e) {
System.err.printf("error: %s: %s%n", path, e.getMessage());
}
}
}
}
假设我有一个图片文件photo.jpg
,然后编译上面的文件并创建一个像这样的JAR文件
$javac*.java
$jar-cfe example.jar Main*.class photo.jpg
然后我可以像这样运行程序并得到以下输出
$java-jar示例NoSuchThing Main.class photo.jpg
错误:NoSuchThing:没有这样的资源
错误:Main.class:不是有效的图像文件
photo.jpg:328 x 328
我认为在这种情况下最好的解决方案是直接向类加载器
请求输入流
(使用)并将其传递给
这是一个完整的例子
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public final class Main {
public static void main(final String[] args) {
final ClassLoader clsldr = Main.class.getClassLoader();
for (final String path : args) {
try {
InputStream is = null;
BufferedImage image = null;
try {
is = clsldr.getResourceAsStream(path);
if (is != null) {
image = ImageIO.read(is);
if (image != null) {
// Do something with the image.
System.out.printf("%s: %d x %d%n",
path,
image.getWidth(),
image.getHeight());
} else {
System.err.printf("error: %s: %s%n",
path,
"not a valid image file");
}
} else {
System.err.printf("error: %s: %s%n",
path,
"no such resource");
}
} finally {
if (is != null) {
is.close();
}
}
} catch (final IOException e) {
System.err.printf("error: %s: %s%n", path, e.getMessage());
}
}
}
}
假设我有一个图片文件photo.jpg
,然后编译上面的文件并创建一个像这样的JAR文件
$javac*.java
$jar-cfe example.jar Main*.class photo.jpg
然后我可以像这样运行程序并得到以下输出
$java-jar示例NoSuchThing Main.class photo.jpg
错误:NoSuchThing:没有这样的资源
错误:Main.class:不是有效的图像文件
photo.jpg:328 x 328
如果我使用InputStream,您的答案非常好。但程序员指出,我不必使用InputStream,我遇到了一个错误,因为我将URL转换为URI,然后再转换为文件。所以在我的例子中,他的答案更简单。如果我使用InputStream,你的答案是很好的。但程序员指出,我不必使用InputStream,我遇到了一个错误,因为我将URL转换为URI,然后再转换为文件。所以在我的情况下,他的答案更简单。