[英]Apache VFS in-memory
我剛剛發現 VFS 作為訪問 sftp 的一種方式。 似乎工作,但所有的例子都假設一個本地文件; 相反,我的數據在內存中。 我只看到一個方法 copyFrom(FileObject),沒有接受流或緩沖區的重載......所以我嘗試了 ram,因為它聽起來大致正確(一些文檔不會受到傷害,但我不能罰款)並且以下測試成功. 復制到 sftp FileObject 也有效。
題。 它提供以下輸出: INFO:使用“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 文件系統是在內存中保存文件的一種選擇。 另一方面,如果您有現有的數據源或字節緩沖區,則需要對其進行泵送:恕我直言,VFS 中沒有從 InputStream 讀取的功能(有一個將 FileContent 的內容寫入 OutputStream)。 您通常會使用 commons-io IOUtils#copy()
來做到這一點。
是的,描述有點短,但實際上並沒有太多內容。 唯一的實際配置是可能的最大尺寸。 (我實際上注意到文件系統參考也談到了過濾資源的謂詞,但沒有實現,所以我從這個頁面的 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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.