Java URL: Unknown Protocol "C"
Asked Answered
U

6

30

I know there are similar questions to this one on SO (like this one), however, after reading through the list of "Questions with similar titles", I still feel strongly that this is unique.

I am working with the iText library to generate PDFs from inside a Swing application. iText's Jpeg class requires a URL in its constructor to locate an image/jpg that you want to add to the PDF file.

When I set this URL to the absolute file path of my JPG file, I get a MalformedURLException claiming unknown protocol: c ("c" being the C:\ drive on my local disk).

Is there any hack/circumvention to this, or do I have to host this JPG somewhere and have the URL find it over the net? Here is the code that is failing:

try {
    String imageUrl = "C:\Users\MyUser\image.jpg";
    Jpeg image = new Jpeg(new URL(imageUrl));
} catch(Exception exc) {
    System.out.println(exc.getMessage());
}

Please note: The URL does properly escape the string (thus "\" are converted to "\ \", etc.).

Thanks in advance!

Unrestrained answered 12/12, 2011 at 12:59 Comment(3)
Shouldn't you be prefixing the file path with file:// ?Fundamentalism
It thinks the C: its the protocol. BTW: You can't use plain \ you need \\ or /Kourtneykovac
And again you are posting uncompilable code snippets! What is it about "String imageUrl = "C:\Users\MyUser\image.jpg"; will not compile", do you not understand?!?Brumfield
H
101

You need to turn the path to the image.jpg file into a file:// URL, like this:

String imageUrl = "file:///C:/Users/MyUser/image.jpg";

Otherwise it interprets the C as the URL protocol.

Harmful answered 12/12, 2011 at 13:1 Comment(4)
+1 for being the only answer to explain why the OP's version was incorrect.Pictograph
Yes - thank you very much. I like to learn, so its discouraging when other users just want to chastise you instead of help you. Thanks again.Unrestrained
@Harmful Your solution works for me but i want to know the reason behind it. Usually i read a file in java without "file://" and it always works.Boyse
@RajatKhandelwal: I have observed that this error is thrown if the local file path is not appended with "file:///" and the file path actually does not exist on your local disk. If you append "file:///" and then if the file path does not exists, java will plainly show error "Path does not exist". Seems like when the file does not exist and the path is not specified as a file path, java by default considers it as a URL.Selfabsorption
M
13

Try with

String imageUrl = "file:///C:/Users/MyUser/image.jpg";
Mihrab answered 12/12, 2011 at 13:1 Comment(0)
P
6

Try this

try {
    String imageUrl = "file:///C:/Users/MyUser/image.jpg";
    Jpeg image = new Jpeg(new URL(imageUrl));
} catch(Exception exc) {
    System.out.println(exc.getMessage());
}
Pinto answered 12/12, 2011 at 13:1 Comment(0)
H
0

In my case the issue was that I was having "%" in my file name. Once I changed it, the file was loaded successfully. So I guess special characters are not allowed in file names, at least in windows.

Hackney answered 20/10, 2021 at 10:19 Comment(0)
A
0

For me adding

file:///

before was necessary but not enough.

Maybe I am using badly the Saxon but I also had to set the baseUri to an empty String. Otherwise it tries to concat the baseUri to the src :

FopFactoryBuilder builder = new FopFactoryBuilder(xconf.toURI());
builder.setStrictFOValidation(false);
builder.setBaseURI(new URI(""));            
FopFactory fopFactory = builder.build();

The code from saxon-he InternalResourceResolver that does the concat :

public URI resolveFromBase(URI uri) {
        try {
            return new URL(baseUri.toURL(), uri.toString()).toURI();
        } catch (MalformedURLException | URISyntaxException | IllegalArgumentException e) {
            return baseUri.resolve(uri);
        }
    }
Accost answered 15/2 at 13:58 Comment(0)
K
-1

Searching the file with its directory and adding in the image to assign to the ImageView

File file = new File("F:/a.jpg");
Image image = new Image(arquivo.toURI().toString()); //here is magic happens
imageView.setImage(image);
Kef answered 3/3, 2019 at 23:18 Comment(1)
Please do some explanation in how your answer could help OP, and please use only english. Welcome to SO!Variscite

© 2022 - 2024 — McMap. All rights reserved.