将Java file:// URL转换为File(...)路径,独立于平台,包括UNC路径 [英] Converting Java file:// URL to File(...) path, platform independent, including UNC paths

查看:762
本文介绍了将Java file:// URL转换为File(...)路径,独立于平台,包括UNC路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个独立于平台的应用程序。我收到一个文件URL *。
在Windows上,这些是:

I am developing a platform independent application. I am receiving a file URL*. On windows these are:


  • 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路径。

I am using new File(URI(urlOfDocument).getPath())which works fine with the first one and also on Unix, Linux, OS X, but does not work with UNC paths.

将文件转换为File(..)路径,与Java 6兼容的标准方法是什么?

What is the standard way to convert file: URLs to File(..) paths, being compatible with Java 6?

。 .....

*
注意:我从OpenOffice / LibreOffice(XModel.getURL())收到这些URL。

* Note: I am receiving theses URLs from OpenOffice / LibreOffice (XModel.getURL()).

推荐答案

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

Based on the hint and link provided in Simone Giannis answer, this is my hack to fix this.

我正在测试uri.getAuthority(),因为UNC路径将报告一个权威机构。这是一个错误 - 所以我依赖于一个错误的存在,这是邪恶的,但它似乎会永远存在(因为Java 7解决了java.nio.Paths中的问题)。

I am testing on uri.getAuthority(), because UNC path will report an Authority. This is a bug - so I rely on the existence of a bug, which is evil, but it apears as if this will stay forever (since Java 7 solves the problem in java.nio.Paths).

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

Note: In my context I will receive absolute paths. I have tested this on Windows and OS X.

(仍在寻找更好的方法)

(Still looking for a better way to do it)

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

}

这篇关于将Java file:// URL转换为File(...)路径,独立于平台,包括UNC路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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