代码之家  ›  专栏  ›  技术社区  ›  Savvas Dalkitsis

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

  •  6
  • Savvas Dalkitsis  · 技术社区  · 17 年前

    (我有一个问题,我在 this


    "\\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到之后对其进行解码的所有方法。..什么都没用。

    4 回复  |  直到 9 年前
        1
  •  4
  •   Aaron Digulla    17 年前

    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());
    }
    
        2
  •  9
  •   mikeyreilly    14 年前

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

    这个答案在错误报告中,但我已经编辑了它,以便在出现哈希时进行修复。

        3
  •  8
  •   Daniel Ryan    13 年前

    ZAMMBI's answer (+1 BTW)。(使用Java 6)

    正如预期的那样,这是可行的

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

    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://")));
    
        4
  •  7
  •   Community Mohan Dere    9 年前

    当您尝试访问路径中有空格的网络驱动器上的资源时,似乎出现了一个错误。看 this entry 在Sun的bug数据库中。

    由于这个bug已经存在一年了,我认为你不会很快得到修复。尝试最新的VM。如果这没有帮助,试着找到来源 WDesktopPeer

    \ 随着 / file:// 并保持空格不变(而不是用 %20 )