简体   繁体   English

内存中的 Apache VFS

[英]Apache VFS in-memory

I just found VFS as a way to access sftp.我刚刚发现 VFS 作为访问 sftp 的一种方式。 Seems to work but all the examples assume a local file;似乎工作,但所有的例子都假设一个本地文件; instead I have my data in memory.相反,我的数据在内存中。 I only see a method copyFrom(FileObject), no overloads accepting a stream or a buffer... So I tried ram as it sounds approximately right (some documentation wouldn't hurt but I can't fine any) and the following test succeeds.我只看到一个方法 copyFrom(FileObject),没有接受流或缓冲区的重载......所以我尝试了 ram,因为它听起来大致正确(一些文档不会受到伤害,但我不能罚款)并且以下测试成功. Copying into an sftp FileObject also worked.复制到 sftp FileObject 也有效。

QUESTION.题。 It gives the following output: INFO: Using "C:\\Users\\myname\\AppData\\Local\\Temp\\vfs_cache" as temporary files store.它提供以下输出: INFO:使用“C:\\Users\\myname\\AppData\\Local\\Temp\\vfs_cache”作为临时文件存储。

-- is it actually writing a temp file?? -- 它实际上是在写一个临时文件吗?? That's what I was trying to avoid (due to potential permissions/concurrency problems on the Unix servers where this thing will run).这就是我试图避免的(由于运行这个东西的 Unix 服务器上潜在的权限/并发问题)。 If it does, how do I do it completely in-memory?如果是这样,我如何完全在内存中完成它?

// 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);
    }
}

No, the log message is a general one generated while setting up the file system manager.不,日志消息是在设置文件系统管理器时生成的一般消息。 It is used for the so-called replicator.它用于所谓的复制器。 You do not use it in your example.你没有在你的例子中使用它。

Yes ram filesystem is one option to have files in memory.是的 ram 文件系统是在内存中保存文件的一种选择。 If you on the other hand have an existing data source or byte buffer, you would need to pump it: there is IMHO no function in VFS to read from a InputStream (there is one to write the content of a FileContent to a OutputStream).另一方面,如果您有现有的数据源或字节缓冲区,则需要对其进行泵送:恕我直言,VFS 中没有从 InputStream 读取的功能(有一个将 FileContent 的内容写入 OutputStream)。 You typically would use commons-io IOUtils#copy() to do that.您通常会使用 commons-io IOUtils#copy()来做到这一点。

Yes the description is a bit short, but there is really not much to it.是的,描述有点短,但实际上并没有太多内容。 The only actual configuration is a possible maximum size .唯一的实际配置是可能的最大尺寸 (I actually noticed the file system reference also talks about a predicate for filtering resources, but that is not implemented so I removed it from the 2.1 version of this page). (我实际上注意到文件系统参考也谈到了过滤资源的谓词,但没有实现,所以我从这个页面的 2.1 版本中删除了它)。

This is an old question, but I was having the same problem and I was able to solve it without assuming a local file, so this may be useful for someone having the same problem as B W. Basically, we can copy an input stream directly into the output stream of the remote file.这是一个老问题,但我遇到了同样的问题,我能够在不假设本地文件的情况下解决它,所以这对于与 B W 有相同问题的人可能很有用。 基本上,我们可以直接复制输入流进入远程文件的输出流。

The code is this:代码是这样的:

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");

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

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