Java file conversion: // URL to a file (...) independent of the platform, including UNC paths

I am developing a platform independent application. I get the file url *. In windows it is:

  • file:///Z:/folder%20to%20file/file.txt

  • file://host/folder%20to%20file/file.txt (UNC path)

I use new File(URI(urlOfDocument).getPath()) , which works fine with the first one, as well as on Unix, Linux, OS X, but does not work with UNC paths.

What is the standard way to convert files: URLs to files (..) compatible with Java 6?

......

* Note. I get URL abstracts from OpenOffice / LibreOffice (XModel.getURL ()).

+22
java windows url
Aug 29 '13 at 21:05
source share
5 answers

Based on the hint and link provided in the answer of Simone Jannis, this is my hack to fix this.

I am testing uri.getAuthority () because the UNC path will report privileges. This is a mistake - so I rely on the existence of an error that is evil, but it seems like it will stay forever (since Java 7 solves the problem in java.nio.Paths).

Note. In my context, I get absolute paths. I tested this on Windows and OS X.

(Still looking for a better way to do this)

 package com.christianfries.test; import java.io.File; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; public class UNCPathTest { public static void main(String[] args) throws MalformedURLException, URISyntaxException { UNCPathTest upt = new UNCPathTest(); upt.testURL("file://server/dir/file.txt"); // Windows UNC Path upt.testURL("file:///Z:/dir/file.txt"); // Windows drive letter path upt.testURL("file:///dir/file.txt"); // Unix (absolute) path } private void testURL(String urlString) throws MalformedURLException, URISyntaxException { URL url = new URL(urlString); System.out.println("URL is: " + url.toString()); URI uri = url.toURI(); System.out.println("URI is: " + uri.toString()); if(uri.getAuthority() != null && uri.getAuthority().length() > 0) { // Hack for UNC Path uri = (new URL("file://" + urlString.substring("file:".length()))).toURI(); } File file = new File(uri); System.out.println("File is: " + file.toString()); String parent = file.getParent(); System.out.println("Parent is: " + parent); System.out.println("____________________________________________________________"); } } 
+9
Aug 30 '13 at 8:50
source share

Based on @SotiriosDelimanolis comment, here is a method for working with URLs (such as file: ...) and non-URLs (such as C: ...) using Spring FileSystemResource:

 public FileSystemResource get(String file) { try { // First try to resolve as URL (file:...) Path path = Paths.get(new URL(file).toURI()); FileSystemResource resource = new FileSystemResource(path.toFile()); return resource; } catch (URISyntaxException | MalformedURLException e) { // If given file string isn't an URL, fall back to using a normal file return new FileSystemResource(file); } } 
+3
Oct 17 '17 at 15:06 on
source share

Java (at least 5 and 6, java 7 Paths most) has a problem with UNC and URI. The Eclipse team wrapped it here: http://wiki.eclipse.org/Eclipse/UNC_Paths

From java.io.File javadocs the UNC prefix is ​​"////", and java.net.URI processes the file: //// host / path (four slashes).

More information about why this happens and about possible problems that occur in other URI and URL methods can be found in the error list at the end of the above link.

Using this information, the Eclipse team developed the org.eclipse.core.runtime.URIUtil class, whose source code can probably help when working with UNC paths.

+2
Aug 29 '13 at 23:55
source share

I hope (definitely not verified) that the newer java has brought the nio and Path package. I hope he fixed: String s="C:\\some\\ile.txt"; System.out.println(new File(s).toPath().toUri()); String s="C:\\some\\ile.txt"; System.out.println(new File(s).toPath().toUri());

-one
Apr 25 '17 at 12:10
source share

For Java 8, the following method works:

  1. Generate URIs from a URI File Line
  2. Create file from URI (not directly from URI string, absolute URI strings are not paths)

See code snippet below.

  String fileURiString="file:///D:/etc/MySQL.txt"; URI fileURI=new URI(fileURiString); File file=new File(fileURI);//File file=new File(fileURiString) - will generate exception FileInputStream fis=new FileInputStream(file); fis.close(); 
-one
Jan 10 '19 at 7:01
source share



All Articles