0

I have literally searched the whole internet for this question but I have not found an answer. I have a file, in the network and I want to create an Itext image with it and for that, I have to convert its path to URL. The problem is when I use path.toURI().toURL() it appends my project path to the URL such that my URL ends up starting with C:/ which will not work. Is there a way to just convert a string to file URL in java? I have tried this:

        String paths = "‪\\\\DESKTOP-A11F076\\Users\\Benson Korir\\Desktop\\walgotech\\passport.jpg";
        String first = "file:" + paths.replaceAll("\\\\", "//").replaceAll("////", "//");
        String second = "file://desktop-a11f076//Users//Benson Korir//Desktop//walgotech//passport.jpg";
        System.out.println(first);
        System.out.println(second);

The second string I have copied directly from the browser and it works fine. Funny this is these two strings output the same thing but the first string brings an error when I use it here:

            Image image1 = Image.getInstance(second);

I am getting the error below:

java.io.FileNotFoundException: ‪\DESKTOP-A11F076\Users\Benson Korir\Desktop\walgotech\passport.jpg (The system cannot find the path specified)
3
  • 1
    unrelated to javafx - please keep the tags focused :) Commented Jan 2, 2021 at 13:35
  • Most likely, the problem is because of the case-sensitivity. Use String paths = "‪\\\\desktop-a11f076\\Users\\Benson Korir\\Desktop\\walgotech\\passport.jpg";. Commented Jan 2, 2021 at 13:45
  • Why are you converting single backslashes to double forward slashes? You need to replace "\\\\\\\\" by "//", and "\\\\" by "/". Commented Jan 3, 2021 at 2:58

1 Answer 1

1

If I got your requirement correctly, your path is a UNC file name, and that is the short form of an SMB path, with DESKTOP-A11F076 being the remote machine, and \Users\Benson Korir\Desktop\walgotech\passport.jpg being the path to the file on that machine.

If I am correct with that assumption, my understanding is that your URL have to look like this: smb://‪DESKTOP-A11F076/Users/Benson Korir/Desktop/walgotech/passport.jpg.

As far I remember is a Java java.io.File object capable to handle a UNC file name (this article implies that, too), but when translating it to a URI, it tries to make it absolute first, and there it fails in your case.

I usually avoid working on Windows whenever possible, therefore I have no environment to verify that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.