【问题标题】:Streaming remote files into file objects将远程文件流式传输到文件对象中
【发布时间】:2013-02-22 10:10:51
【问题描述】:

如果有人知道如何将远程文件直接流式传输到文件对象中的快速方法,因此无需将文件临时存储在计算机上,将不胜感激! 到目前为止,我从远程 ios 设备复制文件如下(使用 net.schmizz.sshj):

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();
}

如果有人对解决方案的代码感兴趣:

正如 Nutlike 在他的回答中提到的,最好使用 InMemoryDestFile。 所以创建以下类:

class MyInMemoryDestFile extends InMemoryDestFile {
    public ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    @Override
    public ByteArrayOutputStream getOutputStream() throws IOException {
        return this.outputStream;
    }
}

...在执行下载操作的方法中创建新类的实例:

MyInMemoryDestFile a = new StreamingInMemoryDestFile();

并访问输出流:

ssh.newSCPFileTransfer().download(remoteFile, a);
a.getOutputStream().toByteArray();

最好的问候

【问题讨论】:

    标签: java sshj


    【解决方案1】:

    使用InMemoryDestFile 代替FileSystemFile 还不够吗?

    编辑:……然后使用getOutputStream() 访问“文件”……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多