无法使用Java Desktop在网络上启动文件?
(我有一个问题,我在this问题中说明,但没有正确的答案。我提炼我的问题,并试图编辑最初的问题,以反映,但我猜是因为SO显示未解决的问题,它失去了势头的方式,没有办法恢复它,所以我再次发布我的正确问题)。无法使用Java Desktop在网络上启动文件?
我有驻留在共享的网络位置的文件:
"\\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());)但抛出这个异常! :
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的错误数据库中的this entry。
由于该错误已经一年多了,我认为你不会很快得到修复。尝试最新的VM。如果这没有帮助,请尝试获取WDesktopPeer
的来源。不要编码路径,请尽量保持原样(带反斜杠和全部),并在其周围加引号。这可能会起作用。
[编辑]具体而言,不更换\
与/
,不预置file://
和离开的空间,因为它们是(而不是用%20
代替)
你的意思是在WDesktopPeer源? – 2009-09-01 16:35:26
是的。修复WDesktopPeer的源代码,让其独立并通过它,而不需要对OS本机代码进行任何修改。 – 2009-09-16 07:23:47
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;
}
这个答案是关于错误报告的,但我已经编辑它来修复有散列的情况。
作品很有魅力,谢谢Mr.Zammbi。 – 2013-04-05 15:15:32
与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());
}
TL;的ZAMMBI's answer 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 <shakes fist>
这项工作,围绕工作
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());
这种变通的作品,似乎是最普遍的形式:
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://")));
并执行相同的文件路径(即与空间)当文件是本地到您的计算机(即在C :)上工作?问题与网络文件系统有关吗? – 2009-09-01 15:14:27
如果我将这个“\\\\ KUROSAVVAS-PC \\ Users \\ kuroSAVVAS \\ Desktop \\ New Folder \ Warsaw Panorama.jpg”粘贴到“运行”对话框中,文件将正常打开。在这个例子中,共享也驻留在我的本地磁盘上,但通常它应该可以在任何网络位置上工作。 – 2009-09-01 15:17:48
(我之前的评论会自动删除这些空格,他们在那里......)我也想说我对该份额进行了认证 – 2009-09-01 15:18:38