Java 内存中的apachevfs

Java 内存中的apachevfs,java,ram,apache-commons-vfs,Java,Ram,Apache Commons Vfs,我刚刚发现VFS是访问sftp的一种方式。似乎有效,但所有示例都假定为本地文件;相反,我将数据存储在内存中。我只看到copyFrom(FileObject)方法,没有接受流或缓冲区的重载。。。因此,我尝试了ram,因为它听起来几乎正确(一些文档不会有什么问题,但我不能对任何文档进行修改),下面的测试成功了。复制到sftp文件对象也起作用 问题。它给出以下输出: 信息:使用“C:\Users\myname\AppData\Local\Temp\vfs\U缓存”作为临时文件存储 --它实际上是在写临

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

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

--它实际上是在写临时文件吗??这正是我试图避免的(由于运行此程序的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);
    }
}

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

是的,ram文件系统是在内存中存储文件的一种选择。另一方面,如果您有一个现有的数据源或字节缓冲区,则需要对其进行压缩:VFS中没有从InputStream读取的函数(有一个函数将FileContent的内容写入OutputStream)。您通常会使用commons io来实现这一点


是的,描述有点短,但实际上没有太多。唯一的实际配置是可能的。(我实际上注意到,还谈到了用于筛选资源的谓词,但没有实现,因此我将其从该页面的2.1版本中删除)。

这是一个老问题,但我遇到了相同的问题,并且我能够在不使用本地文件的情况下解决它,因此这可能对与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文件只用于源代码,那么最好直接将字符串或缓冲区写入目标(使用
write()
)。如果创建ram文件,则可以尝试直接创建远程文件,并保存ram副本。