【发布时间】:2014-11-03 09:40:05
【问题描述】:
我刚刚发现 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);
}
}
【问题讨论】:
-
如果您不将 ram 文件用于源以外的任何东西,最好将您的字符串或缓冲区直接写入目标(使用
write())。如果您创建 ram 文件,您可以尝试直接创建远程文件并保存 ram 副本。
标签: java ram apache-commons-vfs