内存中的Apache VFS



我刚刚发现VFS是访问sftp的一种方式。似乎有效,但所有的例子都假设一个本地文件;相反,我的数据在内存中。我只看到一个方法copyFrom(FileObject),没有接受流或缓冲区的重载。。。所以我尝试了ram,因为它听起来大致正确(有些文档不会有什么问题,但我不能对任何文档进行微调),下面的测试成功了。复制到sftp FileObject中也起到了作用。

问题。它给出以下输出:信息:使用"C:\Users\myname\AppData\Local\Temp\vfs_cache"作为临时文件存储。

--它真的在写一个临时文件吗??这正是我试图避免的(由于运行该程序的Unix服务器上可能存在权限/并发问题)。如果是,我该如何在记忆中完全做到这一点?

// try to copy a string from memory into a FileObject
public class VFSTest {
    public static void main(String[] args) throws IOException {
        String hello = "Hello, World!";
        FileObject ram = VFS.getManager().resolveFile("ram:/tmp");
        OutputStream os = ram.getContent().getOutputStream();
        os.write(hello.getBytes());
        ram.getContent().close();
        FileObject local = VFS.getManager().resolveFile("C:\foo.txt");
        local.copyFrom(ram, Selectors.SELECT_SELF);
    }
}

这是一个老问题,但我遇到了同样的问题,并且我能够在不假设本地文件的情况下解决它,所以这可能对与B W有同样问题的人有用。基本上,我们可以将输入流直接复制到远程文件的输出流中。

代码是这样的:

InputStream is = ... // you only need an input stream, no local file
    
DefaultFileSystemManager fsmanager = (DefaultFileSystemManager) VFS.getManager();
        
FileSystemOptions opts = new FileSystemOptions();
FtpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
StaticUserAuthenticator auth = new StaticUserAuthenticator(host, username, password);
         
DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        
String ftpurl = "ftp://" + host + ":" + port + "/" + folder + "/" + filename;
FileObject remoteFile = fsmanager.resolveFile(ftpurl, opts);
        
try (OutputStream ostream = remoteFile.getContent().getOutputStream()) {
    // either copy input stream manually or with IOUtils.copy()
    IOUtils.copy(is, ostream);
}
        
boolean success = remoteFile.exists();
long size = remoteFile.getContent().getSize();
System.out.println(success ? "Successful, copied " + size + " bytes" : "Failed");

否,日志消息是在设置文件系统管理器时生成的常规消息。它被用于所谓的复制器。你没有在你的例子中使用它。

是的,ram文件系统是在内存中拥有文件的一种选择。另一方面,如果您有一个现有的数据源或字节缓冲区,则需要泵送它:VFS中没有IMHO函数可以从InputStream中读取(有一个函数可以将FileContent的内容写入OutputStream)。您通常会使用commons io IOUtils#copy()来完成此操作。

是的,描述有点简短,但实际上没有太多。唯一的实际配置是可能的最大尺寸。(事实上,我注意到文件系统引用也谈到了用于过滤资源的谓词,但它没有实现,所以我从本页的2.1版本中删除了它)。

最新更新