Apache Commons VFS - 无法解析文件

7

VFS方法无法处理在jboss-beans.xml中配置的URI ${jboss.server.temp.dir}/local/outgoing,该URI由JBoss解析为"C:\\Download\\jboss-eap-5.1.1\\server\\default\\tmp/local/outgoing"。当我尝试解析URI并获取文件时,会抛出异常。有什么想法可以解决这个问题吗?

异常

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 方法

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);
5个回答

15

虽然这是一个比较老的问题,但它非常受欢迎。这个异常相当通用。对于我的情况,在上传文件到远程FTP服务器时抛出了这个异常。根本原因是类路径中缺少SFTP库。就在这个异常之前,vfs会尝试使用与URI中提到的方案相对应的一个提供者来解析文件。

对于我的情况,方案是sftp,并且它尝试在类路径上找到jsch库。由于它没有在我的类路径上找到,所以就抛出了这个异常。因此,必须将提供者的jar包保留在类路径上。


1
这确实是我的情况。花了几个小时,得到了你的答案。 - Ishan Liyanage
1
这也是我的情况。如果您正在使用Apache Commons VFS2,且方案为SFTP,请确保在类路径中包含jsch库。 - ThilankaD

8

@Manish Bansal-感谢您对此问题的回复。 我尝试了一切,但仍然遇到相同的错误:在主线程中抛出异常"org.apache.commons.vfs2.filesystemexception: could not find file with uri "sftp://....." ,因为它是一个相对路径,没有提供基本uri。

在我的MAVEN项目中,我不得不添加“jsch”的依赖关系,它起作用了。

因此,简而言之,我添加了以下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;
}

非常感谢您的帮助 - 我一直在为 OP 报告的错误苦苦挣扎,添加 jsch 的 Maven 依赖项解决了这个问题。 - phil76

7

对于其他遇到此问题的人:我通过替换以下内容来解决此错误:

FileSystemManager fsManager = VFS.getManager();

使用

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

这让我可以多次使用文件系统管理器,而不会再遇到在问题中描述的错误。在完成操作后,请不要忘记关闭你的fsManager

fsManager.close();

1
如果您检查VFS.getManager()的代码,您会发现它创建了一个StandardFileSystemManager的静态实例并为您初始化它,或者返回当前实例。 - bachr
这对我解决了问题,因为我正在使用不同的用户连接到相同的SFTP。 - MJavaDev

2

仅尝试补充已提供正确答案。
简而言之: 检查 VFS Dependencies Table以获取您使用的VFS功能。


当我第一次尝试连接FTP(原问题中并非SFTP)时,我遇到了类似的问题。我使用IntelliJ IDEA和Maven。
我查看了VFS官方文档,发现以下内容:

  1. VFS项目依赖项中的一些是可选的,并且默认情况下未安装
  2. VFS下载和构建页面的表格中可以找到需要安装的内容。
    确实,SFTP需要JSchFTP需要Apache Commons Net等。

只要通过Maven安装Commons Net,我的FTP连接就开始工作了。

编辑于2023年:即在你的pom.xml文件中添加:

<!-- FTP -->
<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.9.0</version>
</dependency>

1

我认为它需要文件方案,因为错误提示假定它是相对的。


1
Scheme 没有帮助。但问题出在 FileSystemManager 的一个实例上。每次操作都应该创建一个新的实例。 - user219882

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接