无法使用Java Desktop在网络上启动文件? [英] Not possible to launch a file on a network using Java Desktop?

查看:663
本文介绍了无法使用Java Desktop在网络上启动文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我有一个问题,我在这个问题,但没有正确的答案,我提出了我的问题,并试图编辑最初的问题,以反映,但我想因为这样的方式显示未解决的问题,它失去了势头,没有办法恢复它。正确的问题再次)。




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

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

(空格有意)

以下代码:

  import java.awt.Desktop; 
import java.io.File;
import java.io.IOException;
$ b $ public class Test {

public static void main(String [] args){
try {
String s =\\\\ \ KUROSAVVAS-PC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'
文件f =新文件;
System.out.println(f.exists());
Desktop.getDesktop()。open(f);
} catch(IOException e){
e.printStackTrace();



$ b $ / code $ / pre
$ b $ p打印到该文件存在的控制台( System.out.println(f.exists()); ),但会抛出此异常! :

  java.io.IOException:打开文件失败://// KUROSAVVAS-PC / Users / kuroSAVVAS / Desktop /新建%20%20%20%20%20Folder /华沙%20%20%20%20Panorama.jpg。错误消息:系统找不到指定的文件。 

at sun.awt.windows.WDesktopPeer.ShellExecute(WDesktopPeer.java:59)
at sun.awt.windows.WDesktopPeer.open(WDesktopPeer.java:36)
在Test.main(Test.java:13)

$ b(java.awt.Desktop.open(Desktop.java:254)
$ b

有没有人知道为什么这样的事情会发生?我已经尝试了从创建URI到解码它们之后的所有事情......没什么可行的。

解决方案

您尝试访问路径中具有空格的网络驱动器上的资源。请参阅Sun的错误数据库中的此条目



由于bug已经一年了,我不认为你会很快得到修复。尝试最新的虚拟机。如果这没有帮助,请尝试获取 WDesktopPeer 的源代码。不要编码路径,而应该保持原样(反斜杠和全部),并在其周围加引号。这可能会起作用。
$ b

具体来说,不要用替换 \ / ,不要在前面加上 file:// 并保留空格(而不是用%20


(I have a problem that I illustrated in this question but had no correct answers. I refined my problem and tried to edit the initial question to reflect that but I guess because of the way SO displays unanswered questions it lost momentum and there is no way to revive it. So I am posting my correct question again).


I have a file that resides on a shared network location :

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

(The spaces are there intentionally)

The following code :

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

}

Prints to the console that the file exists (System.out.println(f.exists());) but throws this 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)

Has anyone any idea why something like this may happen? I have tried everything from creating URIs to decoding them afterwards... Nothing works.

解决方案

It seems that there is a bug when you try to access a resource on a network drive with spaces in the path. See this entry in Sun's bug database.

Since the bug is already a year old, I don't think you'll get a fix anytime soon. Try the latest VM. If that doesn't help, try to get the source for WDesktopPeer. Instead of encoding the path, try to keep it as it was (with backslashes and all) and put quotes around it. That might work.

[EDIT] Specifically, don't replace \ with /, do not prepend file:// and leave the spaces as they are (instead of replacing them with %20)

这篇关于无法使用Java Desktop在网络上启动文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆