2013-08-29 44 views
18

我正在开发独立于平台的应用程序。我收到一个文件URL *。 在Windows上它们是:将Java文件:// URL转换为文件(...)路径,与平台无关,包括UNC路径

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

  • file://host/folder%20to%20file/file.txt(一个UNC路径)

我使用new File(URI(urlOfDocument).getPath())这也是在Unix,Linux中的第一个正常工作, OS X,但不适用于UNC路径。

什么是标准的方式来转换文件:URL到文件(..)路径,与Java 6兼容?

......

* 注:我收到的OpenOffice/LibreOffice的论文网址(XModel.getURL())。

+27

尝试的Java 7'Paths.get(新网址(URL).toURI());' –

+0

感谢(+1)为指向java.nio.Paths。但是,我需要与Java 6兼容(我编辑问题) - 对不起。 –

+5

标准的方法是'new File(url.toURI())'。即使使用Java 5也是如此。 – Holger

回答

1

Java(至少5和6,java 7 Paths解决得最多)对UNC和URI有问题。 Eclipse团队将其包装在此处:http://wiki.eclipse.org/Eclipse/UNC_Paths

从java.io.File javadocs开始,UNC前缀为“////”,而java.net.URI处理file://// host/path(四个斜杠)。

关于为什么会发生这种情况以及其他URI和URL方法可能导致的问题的更多详细信息,可以在上面给出的链接末尾的错误列表中找到。

使用这些信息,Eclipse团队开发了org.eclipse.core.runtime.URIUtil类,在处理UNC路径时可能会帮助哪些源代码。

7

根据Simone Giannis提供的提示和链接回答,这是我的破解来解决这个问题。

我在uri.getAuthority()上测试,因为UNC路径会报告一个权限。这是一个错误 - 所以我依赖于一个错误的存在,这是一个邪恶的东西,但是它会像永远一样保持下去(因为Java 7解决了java.nio.Paths中的问题)。

注意:在我的上下文中,我将收到绝对路径。我已经在Windows和OS X上测试了这个

(还在寻找更好的方式来做到这一点)

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("____________________________________________________________"); 
    } 

} 
-1

我希望(不正确地验证),其新的Java NIO带来的包装和路径。希望它有修复: String s="C:\\some\\ile.txt"; System.out.println(new File(s).toPath().toUri());

+0

所以我终于得到了一台windows机器,而上面的工作并不成功,而java仍然不够聪明。但是,Windows能够理解linux斜杠,所以把s改成C:/some/ile.txt就行了 – judovana

1

建立在@ SotiriosDelimanolis的评论,在这里处理URL(如文件:...)和非URL(如C:...),使用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); 
    } 
}