Commons VFS 和 Java.net.URL - 添加对“sftp://”协议的支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1471615/
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-10-29 16:40:10  来源:igfitidea点击:

Commons VFS and Java.net.URL - Adding support for "sftp://" protocol

javalinuxurlsftpapache-commons-vfs

提问by Rory

We're trying to use Apache Commons VFSto access something over SFTP. It claims to support SFTP, however we're getting a MalformedURLExceptionwhen it starts. Digging around I see that apache vfs is using java.net.URL. However the documentation tells me that it doesn't actually support sftp://URLs.

我们正在尝试使用Apache Commons VFS通过 SFTP 访问某些内容。它声称支持 SFTP,但是当它启动时我们收到了MalformedURLException。四处挖掘,我发现 apache vfs 正在使用java.net.URL. 但是文档告诉我它实际上并不支持sftp://URL。

Protocol handlers for the following protocols are guaranteed to exist on the search path :-

    http, https, ftp, file, and jar

Protocol handlers for additional protocols may also be available.

以下协议的协议处理程序保证存在于搜索路径中:-

    http, https, ftp, file, and jar

附加协议的协议处理程序也可能可用。

I'm using java 1.6.0 on Linux. How can I prevent java.net.URLfrom throwing a wobbly when it sees a sftp://URL? I need to keep using the Apache commons VFS library, which uses java.net.URL.

我在 Linux 上使用 java 1.6.0。如何防止java.net.URL在看到sftp://URL时出现抖动?我需要继续使用 Apache commons VFS 库,它使用java.net.URL.

回答by Steve K

See the list of dependenciesthat commons-vfs requires for sftp to work. Once JSchis in the classpath, your exception no longer happen. Have a look at Apache's Wikifor examples of connecting via sftp.

请参阅commons-vfs 使 sftp 工作所需的依赖项列表。一旦JSch在类路径中,您的异常就不再发生。有关通过 sftp 连接的示例,请查看Apache 的 Wiki

回答by erickson

I've never used VFS before, but it looks like you'd need to (at least) registeran SFTP FileProvider.It looks like it requires something like:

我以前从未使用过 VFS,但看起来您(至少)需要注册一个SFTP FileProvider看起来它需要类似的东西:

DefaultFileSystemManager fsm = ... /* Create and configure your FSM. */
fsm.addProvider("sftp", new SftpFileProvider());

There is probably a way to do the same thing through configuration file syntax.

可能有一种方法可以通过配置文件语法来做同样的事情

回答by Dirk

Have a look at this descriptionabout how to implement a new URL protocol handler (the document describes an LDAP kind-of-URL, but you should be able to generalize from that to your sftpscheme).

看一看这说明如何实现一个新的URL协议处理器(该文件描述了一种LDAP-OF-URL,但你应该能够从推广到您的sftp方案)。

From the document:

从文件:

As you can see, the only difference between the two examples is that we have used an LDAP URL in the second case. However, for running the LDAP sample you first will have to register the LDAP protocol handler of IAIK-JCE to tell the java.net URL framework where to look for the LDAP supporting classesof IAIK-JCE:

System.getProperties().put("java.protocol.handler.pkgs", "iaik.x509.net");

After having registered the IAIK LDAP protocol handler, an IAIK-JCE LdapURLConnection object is returned when calling url.openConnection for an LDAP URL.

如您所见,两个示例之间的唯一区别是我们在第二种情况下使用了 LDAP URL。但是,为了运行 LDAP 示例,您首先必须注册 IAIK-JCE 的 LDAP 协议处理程序,以告诉 java.net URL 框架在哪里查找IAIK-JCE的 LDAP 支持类

System.getProperties().put("java.protocol.handler.pkgs", "iaik.x509.net");

注册 IAIK LDAP 协议处理程序后,当为 LDAP URL 调用 url.openConnection 时,将返回 IAIK-JCE LdapURLConnection 对象。

(emphasis mine)

(强调我的)

回答by Alexander Kj?ll

I use jschto handle sftp and ssh. It won't help you preventing an exception from java.net.URL, but it will help you doing sftp stuff.

我使用jsch来处理 sftp 和 ssh。它不会帮助您防止来自 java.net.URL 的异常,但它会帮助您执行 sftp 操作。

回答by JRL

Depending on how your code is structured, you could remove the s before you give the string to the URL parsing method, set a flag, and if it validates, check that flag and put it back if it's set. It's ugly, but it'll provide a workaround.

根据您的代码的结构,您可以在将字符串提供给 URL 解析方法之前删除 s,设置一个标志,如果验证通过,请检查该标志,如果已设置则将其放回原处。这很丑陋,但它会提供一种解决方法。