Java 获取资源路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3140001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:47:30  来源:igfitidea点击:

get resource path

javapath

提问by Christian

created a new folder "resources" with all my images in eclipse. I added this folder to the class path.

在 Eclipse 中创建了一个包含我所有图像的新文件夹“资源”。我将此文件夹添加到类路径中。

Now i tried to access this images

现在我尝试访问此图像

URL url = new URL("imageA");

I also tried

我也试过

URL url = new URL("resources/imageA");

Both doesnt't work. What is wrong?

两者都不起作用。怎么了?

Sincerely Christian

虔诚的基督徒

回答by Chuk Lee

If you are loading from your classpath you should do something like this

如果你是从你的类路径加载你应该做这样的事情

InputSteam is =  this.getClass().getClassLoader().getResourceAsStream("resources/myimage.png")

回答by kgiannakakis

See herefor directions. You can't load an image directly with URL (actually you can, but it is complicated see this question)

请参阅此处了解路线。您不能直接使用 URL 加载图像(实际上可以,但是很复杂,请参阅此问题

Actually you need to do something like this:

其实你需要做这样的事情:

ClassLoader loader = ClassLoader.getSystemClassLoader();

if(loader != null) {
   URL url = loader.getResource(name);
}

回答by Sarath Kumar Sivan

You can use something like this if you are trying to load an image from a local location.

如果您尝试从本地位置加载图像,则可以使用类似的方法。

File file = new File("D:/project/resources/imageA.jpg");
Image image = ImageIO.read(file);

If you are planning to read the image from a URL, use the below code:

如果您打算从 URL 读取图像,请使用以下代码:

URL url = new URL("http://www.google.co.in/images/srpr/logo3w.png");
Image image = ImageIO.read(url);

Please have a look at the below code for better understanding:

请查看以下代码以更好地理解:

package com.stack.overflow.works.main;

import java.awt.Image;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * @author sarath_sivan
 */

public class ImageLoader {

    private ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    private Image image = null;
    private URL url = null;

    /**
     * Loading image from a URL with the help of java.net.URL class.
     */
    public void loadFromURL() {
        image = null;
        try {
            url = new URL("http://www.google.co.in/images/srpr/logo3w.png");
            if (url != null) {
                image = ImageIO.read(url);
            }

        } catch(Exception exception) {
            exception.printStackTrace();
        }
        display(image, "Loading image from URL...");

    }

    /**
     * Loading image from your local hard-drive with getResource().
     * Make sure that your resource folder which contains the image
     * is available in your class path.
     */
    public void loadFromLocalFile() {
        image = null;
        try {
            url = classLoader.getResource("images.jpg");
            if (url != null) {
                image = ImageIO.read(url);
            }

        } catch(Exception exception) {
            exception.printStackTrace();
        }
        display(image, "Loading image from Local File...");
    }



    private void display(Image image, String message) {
        JFrame frame = new JFrame(message);
        frame.setSize(300, 300);
        JLabel label = new JLabel(new ImageIcon(image));
        frame.add(label);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.loadFromURL();
        imageLoader.loadFromLocalFile();
    }

}

Output

输出

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明