Java:从 FilePath 获取 URI

发布于 2024-12-18 20:54:44 字数 435 浏览 1 评论 0原文

我对Java知之甚少。我需要在 Windows 上从 FilePath(String) 构造 URI 的字符串表示形式。有时我得到的 inputFilePath 是:file:/C:/a.txt,有时是:C:/a.txt。现在,我正在做的是:

new File(inputFilePath).toURI().toURL().toExternalForm()

上面的方法适用于不以 file:/ 为前缀的路径,但对于以 file:/ 为前缀的路径, .toURI 方法通过附加当前目录的值将其转换为无效的 URI,因此路径变得无效。

请帮助我建议一种正确的方法来获取这两种路径的正确 URI。

I've little knowledge of Java. I need to construct a string representation of an URI from FilePath(String) on windows. Sometimes the inputFilePath I get is: file:/C:/a.txt and sometimes it is: C:/a.txt. Right now, what I'm doing is:

new File(inputFilePath).toURI().toURL().toExternalForm()

The above works fine for paths, which are not prefixed with file:/, but for paths prefixed with file:/, the .toURI method is converting it to a invalid URI, by appending value of current dir, and hence the path becomes invalid.

Please help me out by suggesting a correct way to get the proper URI for both kind of paths.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烏雲後面有陽光 2024-12-25 20:54:44

这些是有效的文件 uri:

file:/C:/a.txt            <- On Windows
file:///C:/a.txt          <- On Windows
file:///home/user/a.txt   <- On Linux

因此您需要删除 file:/file:///(Windows)和 file://对于Linux。

These are the valid file uri:

file:/C:/a.txt            <- On Windows
file:///C:/a.txt          <- On Windows
file:///home/user/a.txt   <- On Linux

So you will need to remove file:/ or file:/// for Windows and file:// for Linux.

残龙傲雪 2024-12-25 20:54:44

只需使用 Normalize();

示例:

path = Paths.get("/", input).normalize();

这一行将标准化所有路径。

Just use Normalize();

Example:

path = Paths.get("/", input).normalize();

this one line will normalize all your paths.

感情洁癖 2024-12-25 20:54:44

来自 https://jaxp.java.net 的 SAXLocalNameCount.java:

/**
 * Convert from a filename to a file URL.
 */
private static String convertToFileURL ( String filename )
{
    // On JDK 1.2 and later, simplify this to:
    // "path = file.toURL().toString()".
    String path = new File ( filename ).getAbsolutePath ();
    if ( File.separatorChar != '/' )
    {
        path = path.replace ( File.separatorChar, '/' );
    }
    if ( !path.startsWith ( "/" ) )
    {
        path = "/" + path;
    }
    String retVal =  "file:" + path;

    return retVal;
}

From SAXLocalNameCount.java from https://jaxp.java.net:

/**
 * Convert from a filename to a file URL.
 */
private static String convertToFileURL ( String filename )
{
    // On JDK 1.2 and later, simplify this to:
    // "path = file.toURL().toString()".
    String path = new File ( filename ).getAbsolutePath ();
    if ( File.separatorChar != '/' )
    {
        path = path.replace ( File.separatorChar, '/' );
    }
    if ( !path.startsWith ( "/" ) )
    {
        path = "/" + path;
    }
    String retVal =  "file:" + path;

    return retVal;
}
泛滥成性 2024-12-25 20:54:44

new File(String) 的参数是路径,而不是 URI。因此,您帖子中“但是”之后的部分是对 API 的无效使用。

The argument to new File(String) is a path, not a URI. The part of your post after 'but' is therefore an invalid use of the API.

丶视觉 2024-12-25 20:54:44
class TestPath {

    public static void main(String[] args) {
        String brokenPath = "file:/C:/a.txt";

        System.out.println(brokenPath);

        if (brokenPath.startsWith("file:/")) {
            brokenPath = brokenPath.substring(6,brokenPath.length());
        }
        System.out.println(brokenPath);
    }
}

给出输出:

file:/C:/a.txt
C:/a.txt
Press any key to continue . . .
class TestPath {

    public static void main(String[] args) {
        String brokenPath = "file:/C:/a.txt";

        System.out.println(brokenPath);

        if (brokenPath.startsWith("file:/")) {
            brokenPath = brokenPath.substring(6,brokenPath.length());
        }
        System.out.println(brokenPath);
    }
}

Gives output:

file:/C:/a.txt
C:/a.txt
Press any key to continue . . .
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文