windows 文件:URI 和斜线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1878024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:34:10  来源:igfitidea点击:

file: URIs and Slashes

javawindowsurifile-uri

提问by Sammy1Am

An application I'm working on involves accessing files on network file shares, and we're using URIs to specify the files' locations.

我正在开发的一个应用程序涉及访问网络文件共享上的文件,我们使用 URI 来指定文件的位置。

My understanding of the file: URI is that they should take the form of file://+path. In the case of a Windows network share, this path looks something like \\servername\dir\file, so the resultant URI becomes file:////servername/dir/file.

我对 file: URI 的理解是它们应该采用file://+ path的形式。对于 Windows 网络共享,此路径类似于\\servername\dir\file,因此生成的 URI 变为file:////servername/dir/file

This seems to be working great for Java's URI class, but the Win32 API seems to want a file://servername/dir/filestyle URI, which Java rejects because it "has an authority component".

这似乎对 Java 的URI 类很有效,但 Win32 API 似乎需要一个file://servername/dir/file样式 URI,Java 拒绝它,因为它“具有权威组件”。

Am I understanding network-share URIs correctly? Is there another way to specify a path without Java complaining about the authority?

我是否正确理解网络共享 URI?有没有另一种方法来指定路径而不用 Java 抱怨权限?

Edit:We were hoping to be able to store paths as URIs, so as to make use of the scheme-part of the URI to specify other locations (e.g. file: versus other:). But as pointed out, it looks like Java may just have its own issues with URIs...

编辑:我们希望能够将路径存储为 URI,以便利用 URI 的方案部分来指定其他位置(例如文件:与其他:)。但正如所指出的,看起来 Java 可能只是有自己的 URI 问题......

回答by dtb

It seems that Java is wrong:

似乎Java是错误的

Incorrect: file:////applib/products/a%2Db/abc%5F9/4148.920a/media/start.swf
Correct: file://applib/products/a-b/abc_9/4148.920a/media/start.swf

不正确:file:////applib/products/a%2Db/abc%5F9/4148.920a/media/start.swf
正确:file://applib/products/a-b/abc_9/4148.920a/media/start.swf

On UNC paths in Java:

Java中的UNC路径

The URI class handles UNC paths reasonably well, but has some problems. In the Java class libraries, the string representation of a UNC path is as follows:

new File("//SERVER/some/path").toURI().toString()
                                                -> "file:////SERVER/some/path

In other words, the URI stores the entire UNC path in the path component of the URI, and leaves the server/authority component empty. As long as you consistently use this string representation you will be able to interact successfully with java.net.URI.

URI 类可以很好地处理 UNC 路径,但存在一些问题。在Java类库中,UNC路径的字符串表示如下:

new File("//SERVER/some/path").toURI().toString()
                                                -> "file:////SERVER/some/path

换句话说,URI 将整个 UNC 路径存储在 URI 的路径组件中,并将服务器/权限组件留空。只要您始终如一地使用这个字符串表示,您就能够成功地与 java.net.URI 交互。