实例化自定义 java (nio) 文件系统的问题

Issue instantiating a custom java (nio) file system

我正在基于 link 创建自定义文件系统。我创建了一个 CustomFileSystemProvider(和其他 CustomFileSystem 类)并覆盖了所需的方法。 CustomFileSystemProvider returns 字符串"myfs" 的getScheme() 方法。 但是,当我尝试使用以下代码测试此文件系统时

FileSystem fs = FileSystems.newFileSystem(URI.create("myfs://a_remote_resource"), env);

我收到一个错误

java.nio.file.ProviderNotFoundException: Provider "myfs" not found
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:341)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276)
at Tester.main(Tester.java:30)

我在 FileSystemProvider.installedProviders() 返回的列表中没有看到我的自定义文件系统。我不确定将自定义 FS 注册为 "installedProvider" 需要什么。

这是一个错误URL.Change URL 一个标准 url.You 也可以提供一个分析器。

URL aURL = new URL("http://java.sun.com:80/docs/books/tutorial" + "/index.html?name=networking#DOWNLOADING");

System.out.println("protocol = " + aURL.getProtocol());

System.out.println("authority = " + aURL.getAuthority()); System.out.println("host = " + aURL.getHost());

System.out.println("port = " + aURL.getPort());

System.out.println("path = " + aURL.getPath());

System.out.println("query = " + aURL.getQuery());

System.out.println("filename = " + aURL.getFile());

System.out.println("ref = " + aURL.getRef());

结果如:

ut:ut:protocol = http
ut:ut:authority = localhost:8080
ut:ut:host = localhost
ut:ut:port = 8080
ut:ut:path = /UT2.0/login.action
ut:ut:query = null
ut:ut:filename = /UT2.0/login.action
ut:ut:ref = null

如果您通过互联网操作文件,请选择一种协议,例如RPC、socket...

A FileSystemProvider 已按照 FileSystems 的 Javadoc 中的说明加载:

Installed providers are loaded using the service-provider loading facility defined by the ServiceLoader class. Installed providers are loaded using the system class loader. If the system class loader cannot be found then the extension class loader is used; if there is no extension class loader then the bootstrap class loader is used. Providers are typically installed by placing them in a JAR file on the application class path or in the extension directory, the JAR file contains a provider-configuration file named java.nio.file.spi.FileSystemProvider in the resource directory META-INF/services, and the file lists one or more fully-qualified names of concrete subclass of FileSystemProvider that have a zero argument constructor

您可以为此使用 Google auto-service

注释您的 FileSystemProvider 实现 class 如下:

@AutoService(FileSystemProvider.class)

那么你就不需要fiddle关于 META-INF 中的文件了。