28

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!

Community
  • 1
  • 1
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

5 Answers5

98

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.

Andy
  • 8,870
  • 1
  • 31
  • 39
  • 9
    +1 for being the only answer to explain *why* the OP's version was incorrect. – Greg Case Dec 12 '11 at 13:16
  • 6
    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. – IAmYourFaja Dec 12 '11 at 13:28
  • @Andy 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. – Rajat Khandelwal Jul 13 '18 at 10:36
  • @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. – Ganesh Jadhav Feb 12 '19 at 14:26
13

Try with

String imageUrl = "file:///C:/Users/MyUser/image.jpg";
adarshr
  • 61,315
  • 23
  • 138
  • 167
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());
}
korifey
  • 3,379
  • 17
  • 17
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.

Gilad Dahan
  • 508
  • 5
  • 19
-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);
Nunes
  • 1
  • 1
  • 1
    Please do some explanation in how your answer could help OP, and please use only english. Welcome to SO! – Piazzi Mar 04 '19 at 01:06