java - Downloading file via SFTP using JSCH (android) -


i'm trying make app downloads file server using sftp. whenever run debugger, seems fine no errors. log says has downloaded specified file. however, can't seem find file supposedly downloaded anywhere in device. help?

import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem;  import com.jcraft.jsch.channel; import com.jcraft.jsch.channelsftp; import com.jcraft.jsch.jsch; import com.jcraft.jsch.jschexception; import com.jcraft.jsch.session; import com.jcraft.jsch.sftpexception;  import java.util.list;   public class mainactivity extends activity {      private string user = "user";     private string pass = "pass";     private string host = "hostname";     private int portnum = 22;      private string filename = "sample.txt";      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          new asynctask<void, void, list<string>>() {             @override             protected list<string> doinbackground(void... params) {                 try {                     downloader(filename);                 } catch (exception e) {                     e.printstacktrace();                 }                 return null;             }          }.execute();     }      public void downloader(string filename) {          jsch jsch = new jsch();         session session = null;          string knownhostsdir = "/home/user/";          try {             jsch.setknownhosts(knownhostsdir);         } catch (jschexception e) {             e.printstacktrace();         }          try {              session = jsch.getsession(user, host, portnum);             session.setconfig("stricthostkeychecking", "no");             session.setpassword(pass);             session.connect();              channel channel = session.openchannel("sftp");             channel.connect();             channelsftp sftpchannel = (channelsftp) channel;             sftpchannel.get(filename);              log.d(filename, " has been downloaded");              sftpchannel.exit();             session.disconnect();          } catch (jschexception e) {             e.printstacktrace();         } catch (sftpexception e) {             e.printstacktrace();         }     }  } 

channelsftp sftpchannel = (channelsftp) channel; sftpchannel.get(filename); log.d(filename, " has been downloaded"); 

the single-argument version of channelsftp.get() doesn't write remote file local file. returns inputstream. you're supposed read contents of remote file inputstream, example:

try (fileoutputstream out = new fileoutputstream("/some/file")) {     try (inputstream in = sftpchannel.get(filename)) {         // read in, write out         byte[] buffer = new byte[1024];         int len;         while ((len = in.read(buffer)) != -1) {             out.write(buffer, 0, len);         }     } } 

alternately, there other versions of the get() method write remote content local file you.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -