我收到这个错误,我在程序中所做的唯一更改是我将参数硬编码为
String hostName = "122.183.217.133" ;
String username = "root";
String password = "something";
String localFilePath = "C://Sample.jpg";
String remoteFilePath = "/var/www/html/intranetupload/uploads/Sample.jpg";
Jun 25, 2012 2:52:18 PM org.apache.commons.vfs2.VfsLog info
INFO: Using "C:\DOCUME~1\MEENAK~1.DCK\LOCALS~1\Temp\vfs_cache" as temporary files store.
Exception in thread "main" java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Invalid absolute URI "sftp://root:***@122.183.217.133:22/var/www/html/intranetupload/uploads/Sample1.jpg".
at sftp.sample.Main.delete(Main.java:120)
at sftp.sample.Main.main(Main.java:36)
Caused by: org.apache.commons.vfs2.FileSystemException: Invalid absolute URI "sftp://root:***@122.183.217.133:22/var/www/html/intranetupload/uploads/Sample1.jpg".
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:61)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:693)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:621)
at sftp.sample.Main.delete(Main.java:111)
... 1 more
Caused by: org.apache.commons.vfs2.FileSystemException: Expecting / to follow the hostname in URI "sftp://root:***@122.183.217.133:22/var/www/html/intranetupload/uploads/Sample1.jpg".
at org.apache.commons.vfs2.provider.HostFileNameParser.extractToPath(HostFileNameParser.java:155)
at org.apache.commons.vfs2.provider.URLFileNameParser.parseUri(URLFileNameParser.java:50)
at org.apache.commons.vfs2.provider.AbstractFileProvider.parseUri(AbstractFileProvider.java:188)
at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:57)
... 4 more
我没有服务器的域名。我只有一台 ubuntu 机器,它的 IP/用户名和密码是已知的。 我的工作需要删除该远程 Ubuntu 机器中的文件。
你能帮我解决这个错误吗?
最佳答案
解决方法是在调用resolveFile()之前对用户名和密码进行Url编码;
所以如果你需要这样调用:
manager.resolveFile("sftp://sftpuser@Location:Test!@#$%^&*()[]:;@US456564/home57556");
其中用户名为“sftpuser@Location”,密码为“Test!@#$%^&*[]:;:
你只对用户名和密码进行编码,然后像这样调用它:
manager.resolveFile("sftp://sftpuser%40Location:Test!%40%23%24%25%5E%26*%5B%5D%3A%3B@US456564/home57556");
作为 Sting,它可能看起来有点乱,但在调用代码中没有太大问题。
关于java - 通过java编码的SFTP抛出异常并说无效的绝对URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11187718/