简体   繁体   English

Apache Commons VFS - 无法解析文件

[英]Apache Commons VFS - cannot resolveFile

The VFS method cannot process this URI ${jboss.server.temp.dir}/local/outgoing configured in jboss-beans.xml which is resolved to "C:\\\\Download\\\\jboss-eap-5.1.1\\\\server\\\\default\\\\tmp/local/outgoing" by JBoss. VFS 方法无法处理在jboss-beans.xml配置的这个 URI ${jboss.server.temp.dir}/local/outgoing解析为"C:\\\\Download\\\\jboss-eap-5.1.1\\\\server\\\\default\\\\tmp/local/outgoing" JBoss 的"C:\\\\Download\\\\jboss-eap-5.1.1\\\\server\\\\default\\\\tmp/local/outgoing" When I try to resolve the URI and get the file, it throws an exception.当我尝试解析 URI 并获取文件时,它会引发异常。 Any ideas what could be the problem?任何想法可能是什么问题?

Exception

17:35:25,024 ERROR [VfsSynchronizerConfImpl] File FromOutgoing cannot be resolved, FileSystemException:
org.apache.commons.vfs2.FileSystemException: Could not find file with URI "C:\Download\jboss-eap-5.1.1\server\default\tmp/local/outgoing" because it is a relative path, and no base URI was provided.
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:719)
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:649)
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:605)

DefaultFileSystemManager.class methods

public FileObject resolveFile(final String uri) throws FileSystemException
  -- this method calls the method below

public FileObject resolveFile(final FileObject baseFile, final String uri,
        final FileSystemOptions fileSystemOptions)
        throws FileSystemException
  -- this method cannot process the string and throws
     throw new FileSystemException("vfs.impl/find-rel-file.error", uri);

Quite old question but popular.很老的问题,但很受欢迎。 This exception is quite generic.这个例外是非常通用的。 For my case, this exception was thrown while uploading files to remote ftp server.就我而言,在将文件上传到远程 ftp 服务器时引发了此异常。 And the root cause was missing sftp library in classpath.根本原因是在类路径中缺少 sftp 库。 Just before this exception, vfs tries to resolve the file using one of the provider corresponding to the scheme mentioned in URI .就在此异常之前, vfs尝试使用与URI提到的方案相对应的提供程序之一来解析文件。

For my case, scheme was sftp and and it tried to find jsch library on classpath.就我而言,方案是sftp ,它试图在类路径上找到jsch库。 Since, it was not present on my classpath, this exception was thrown.Therefore, one must keep the provider jar on classpath.因为它不在我的类路径中,所以抛出了这个异常。因此,必须将提供程序 jar 保留在类路径上。

For other people facing with this issue: I got rid of this error by replacing对于面临此问题的其他人:我通过替换消除了此错误

FileSystemManager fsManager = VFS.getManager();

with

StandardFileSystemManager fsManager = new StandardFileSystemManager();
fsManager.init();

This allowed me to use the file system manager multiple times without getting the error anymore described in the question.这使我可以多次使用文件系统管理器而不会再出现问题中描述的错误。 Don't forget to close you fsManager when you're done with it:完成后不要忘记关闭fsManager

fsManager.close();

@Manish Bansal - thank you for your reply on this issue. @Manish Bansal - 感谢您对此问题的回复。 I tried everything but was still getting the same error: exception in thread "main" org.apache.commons.vfs2.filesystemexception: could not find file with uri "sftp://....." because it is a relative path, and no base uri was provided.我尝试了所有方法,但仍然遇到相同的错误:线程“main” org.apache.commons.vfs2.filesystemexception 中的异常:找不到带有 uri“sftp://.....”的文件,因为它是一个相对路径,并且没有提供基础 uri。

In my MAVEN project i had to add the dependency for 'jsch' & it worked.在我的 MAVEN 项目中,我必须添加对 'jsch' 的依赖并且它起作用了。

So, in a nutshell i added the following POM dependencies:因此,简而言之,我添加了以下 POM 依赖项:

<!-- SFTP -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>   version>0.1.55</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-vfs2</artifactId>
    <version>2.6.0</version>
</dependency>
    public void downloadFile() throws IOException {     
        FileSystemManager manager = VFS.getManager();
                
        FileObject remote = manager.resolveFile(createConnectionString("XYZ.com","ABC","LMNOP","/<FOLDER_NAME>/<FILE_NAME>"));
        FileObject local = manager.resolveFile(System.getProperty("user.dir") + "/" + "src//main//resources//");
        
        local.copyFrom(remote, Selectors.SELECT_SELF);
     
        local.close();
        remote.close();
    }
    
    public static URI createConnectionString(String hostName, String username, String password, String remoteFilePath) {        
        URI sftpURI = null;
        try {
            String userInfo = username + ":" + password;
            
            sftpURI = new URI("sftp",userInfo,hostName,-1,remoteFilePath,null,null);
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return sftpURI;
    }

我认为它需要 file: 方案,因为错误表明它被认为是相对的。

Just to try to complement correct answers already provided.只是为了补充已经提供的正确答案。
In short : check VFS Dependencies Table for VFS features you use.简而言之:检查VFS 依赖项表以了解您使用的 VFS 功能。


I ran into a similar problem when I first tried to connect over FTP ( not SFTP as in the original question ).当我第一次尝试通过FTP连接时遇到了类似的问题(而不是原始问题中的 SFTP )。 I use Maven with IntelliJ IDEA.我将 Maven 与 IntelliJ IDEA 一起使用。
I looked through the VFS official docs and found out the following:我查看了VFS 官方文档,发现了以下内容:

  1. Some of VFS Project Dependencies are optional and not installed by default .某些VFS 项目依赖项是可选的,默认情况下未安装
  2. What one can need to install is on the table in VFS Download & Build page .需要安装的VFS 下载和构建页面中的表格
    Indeed, JSch is required for SFTP , Apache Commons Net is required for FTP , etc.事实上, SFTP需要JSchFTP需要Apache Commons Net ,等等。

My FTP connection had begun work as soon as I installed Commons Net via Maven.我通过 Maven 安装Commons Net ,我的 FTP 连接就开始工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM