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

て烟熏妆下的殇ゞ 提交于 2019-11-30 17:29:06

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

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

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.

Have a look at this description about 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 sftp scheme).

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 classes of 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.

(emphasis mine)

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!