2 回答

TA貢獻1943條經(jīng)驗 獲得超7個贊
這是一個老問題,但我遇到了同樣的問題,并且我能夠在不假設(shè)本地文件的情況下解決它,例如使用 ByteArrayInputStream,所以這對于與 pise 遇到相同問題的人可能很有用?;旧?,我們可以將輸入流直接復(fù)制到遠程文件的輸出流中。
代碼是這樣的:
InputStream is = ... // you only need an input stream, no local file, e.g. ByteArrayInputStream
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");

TA貢獻1890條經(jīng)驗 獲得超9個贊
/**
?* Write the byte array to the given file.
?*
?* @param file The file to write to
?* @param data The data array
?* @throws IOException
?*/
public static void writeData(FileObject file, byte[] data)
? ? ? ? throws IOException {
? ? OutputStream out = null;
? ? try {
? ? ? ? FileContent content = file.getContent();
? ? ? ? out = content.getOutputStream();
? ? ? ? out.write(data);
? ? } finally {
? ? ? ? close(out);
? ? }
}
添加回答
舉報