简体   繁体   English

java.net.MalformedURLException:未知协议:f

[英]java.net.MalformedURLException: unknown protocol: f

I am trying to get histogram of an image using this code Displaying a histogram of image data . 我试图使用此代码获取图像的直方图显示图像数据的直方图 Normally it works fine when input image given by url. 通常它在url给出的输入图像时工作正常。 But while I give image from local directory 但是我从本地目录中提供图像

private BufferedImage getImage() {
    try {
        return ImageIO.read(new URL(
            "F:/test.jpg"));
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    return null;
}

it gives exception, 它给出了例外,

 java.net.MalformedURLException: unknown protocol: f 

How to resolve this exception and get Histogram of an image 如何解决此异常并获取图像的直方图

F:/test.jpg is not a valid URL. F:/test.jpg不是有效的URL。 For files the URL is file://F:/test.jpg where file is the protocol 对于文件,URL为file://F:/test.jpg其中file是协议

The protocol is not valid. 该协议无效。

If you need to load a file from the filesystem you need to use the file URI scheme 如果需要从文件系统加载文件,则需要使用文件URI方案

A file URI takes the form of file://host/path 文件URI采用file://host/path

where host is the fully qualified domain name of the system on which the path is accessible, and path is a hierarchical directory path of the form directory/directory/.../name . 其中host是可以访问路径的系统的完全限定域名,path是表单directory/directory/.../name的分层目录路径。 If host is omitted, it is taken to be " localhost ", the machine from which the URL is being interpreted. 如果省略host ,则将其视为“ localhost ”,即从中解释URL的机器。

So the url should be: 所以网址应该是:

file://F:/test.jpg

While the other answers will technically solve your problem, you shouldn't be using a URL for this. 虽然其他答案将在技术上解决您的问题,但您不应该使用此URL There are other signatures for the read function, one which takes a File and one which takes an InputStream instead , so you can use either of the following: read函数有其他签名, 一个采用File ,另一个采用InputStream ,因此您可以使用以下任一方法:

return ImageIO.read(new File("F:/test.jpg"));
// or
return ImageIO.read(new FileInputStream("F:/test.jpg"));

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

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