无法使用Java Desktop在网络上启动文件?

(我有一个问题,我在这个问题中说明但没有正确的答案。我改进了我的问题,并试图编辑最初的问题来反映,但我想因为SO显示未解决的问题的方式失去了动力,没有办法恢复它。所以我再次发布我的正确问题)。


我有一个驻留在共享网络位置的文件:

"\\KUROSAVVAS-PC\Users\kuroSAVVAS\Desktop\New Folder\Warsaw Panorama.JPG" 

(这些空间是有意的)

以下代码:

 import java.awt.Desktop; import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) { try { String s = "\\\\KUROSAVVAS-PC\\Users\\kuroSAVVAS\\Desktop\\New Folder\\Warsaw Panorama.jpg"; File f = new File(s); System.out.println(f.exists()); Desktop.getDesktop().open(f); } catch (IOException e) { e.printStackTrace(); } } } 

向控制台打印文件存在( System.out.println(f.exists()); )但抛出此exception! :

 java.io.IOException: Failed to open file:////KUROSAVVAS-PC/Users/kuroSAVVAS/Desktop/New%20%20%20%20%20Folder/Warsaw%20%20%20%20Panorama.jpg. Error message: The system cannot find the file specified. at sun.awt.windows.WDesktopPeer.ShellExecute(WDesktopPeer.java:59) at sun.awt.windows.WDesktopPeer.open(WDesktopPeer.java:36) at java.awt.Desktop.open(Desktop.java:254) at Test.main(Test.java:13) 

有谁知道为什么会发生这样的事情? 我已经尝试了从创建URI到后来解码它们的所有内容……没有任何作用。

当您尝试访问路径中包含空格的网络驱动器上的资源时,似乎存在错误。 请参阅Sun的错误数据库中的此条目 。

由于这个bug已经有一年了,我认为你不会很快得到修复。 尝试最新的VM。 如果这没有帮助,请尝试获取WDesktopPeer的源WDesktopPeer 。 而不是编码路径,尝试保持原样(使用反斜杠和所有)并在其周围加上引号。 那可能有用。

[编辑]具体来说,不要用\替换\ ,不要预先添加file://并保留空格(而不是用%20替换它们)

使用java 7,您可以执行此操作

 public static void main(String[] args) throws IOException { String s = "\\\\KUROSAVVAS-PC\\Users\\kuroSAVVAS\\Desktop\\New Folder\\Warsaw Panorama.jpg"; Path p = Paths.get(s); Desktop.getDesktop().browse(p.toUri()); } 

Java 6解决方案:

 public static void launchFile(File file) { if (!Desktop.isDesktopSupported()) return; Desktop dt = Desktop.getDesktop(); try { dt.open(file); } catch (IOException ex) { // this is sometimes necessary with files on other servers ie // \\xxx\xxx.xls launchFile(file.getPath()); } } // this can launch both local and remote files public static void launchFile(String filePath) { if (filePath == null || filePath.trim().length() == 0) return; if (!Desktop.isDesktopSupported()) return; Desktop dt = Desktop.getDesktop(); try { dt.browse(getFileURI(filePath)); } catch (Exception ex) { ex.printStackTrace(); } } // generate uri according to the filePath private static URI getFileURI(String filePath) { URI uri = null; filePath = filePath.trim(); if (filePath.indexOf("http") == 0 || filePath.indexOf("\\") == 0) { if (filePath.indexOf("\\") == 0){ filePath = "file:" + filePath; filePath = filePath.replaceAll("#", "%23"); } try { filePath = filePath.replaceAll(" ", "%20"); URL url = new URL(filePath); uri = url.toURI(); } catch (MalformedURLException ex) { ex.printStackTrace(); } catch (URISyntaxException ex) { ex.printStackTrace(); } } else { File file = new File(filePath); uri = file.toURI(); } return uri; } 

这个答案是关于错误报告的,但我已经编辑了它以便在有哈希时进行修复。

TL; ZAMMBI答案的 DR(+1 BTW)。 (使用Java 6)

这正如预期的那样有效

 Desktop.getDesktop().open(new File("\\\\host\\path_without\\spaces.txt")); //works 

由于已知的Java错误,这会失败:

 Desktop.getDesktop().open(new File("\\\\host\\path with\\spaces.txt")); //fails  

这种解决方法是有效的

 Desktop.getDesktop().browse(new URI("file://host/path%20with/spaces.txt")) //works (note slash direction and escape sequences) 

这种解决方案似乎应该可行,但不会:

 Desktop.getDesktop().browse((new File("\\\\host\\path with\\spaces.txt")).toURI()); 

这种解决方法有效,似乎是最常见的forms:

 File curFile = new File("\\\\host\\path with\\or_without\\spaces\\local or network.txt"); Desktop.getDesktop().browse(new URI(curFile .toURI().toString().replace("file:////","file://")));