2013-02-22 29 views
5

Nếu có ai biết cách tải tập tin từ xa trực tiếp vào một đối tượng tập tin, vì vậy không cần lưu trữ tập tin tạm thời trên máy tính. Cho đến bây giờ tôi sao chép các tập tin từ một ios-thiết bị từ xa như sau (sử dụng net.schmizz.sshj):Truyền tập tin từ xa vào các đối tượng tập tin

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

Nếu có bất cứ ai quan tâm đến mã của giải pháp:

Như Nutlike được đề cập trong câu trả lời của anh ta tốt hơn nên sử dụng InMemoryDestFile. Vì vậy, tạo ra các lớp sau đây:

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

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

... trong phương pháp của bạn, nơi bạn thực hiện các thao tác tải về tạo một thể hiện của lớp mới của bạn:

MyInMemoryDestFile a = new StreamingInMemoryDestFile(); 

và truy cập vào OutputStream:

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

Trân trọng

Trả lời

Các vấn đề liên quan