Java – streaming remote files into file objects
•
Java
If someone knows how to stream remote files directly to file objects, it is not necessary to temporarily store files on the computer, we will be grateful!
SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(fingerprint);
ssh.connect(ip);
try {
ssh.authPassword("username","userpassword".tocharArray());
ssh.newSCPFileTransfer().download(fileRemote,new FileSystemFile(fileLocal));
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
ssh.disconnect();
}
If someone is interested in the code of the solution:
As nutlike mentioned in his answer, it is best to use inmemorydestfile So create the following classes:
class MyInMemoryDestFile extends InMemoryDestFile {
public ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@Override
public ByteArrayOutputStream getOutputStream() throws IOException {
return this.outputStream;
}
}
... in the method performing the download operation, create an instance of the new class:
MyInMemoryDestFile a = new StreamingInMemoryDestFile();
And access the output stream:
ssh.newSCPFileTransfer().download(remoteFile,a); a.getOutputStream().toByteArray();
Best wishes
Solution
Is it not enough to use inmemorydestfile instead of filesystemfile?
Edit:... Then use getoutputstream() to access' file '
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
