Menu

Slow performance downloading files using SFTP in Android

Help
casolorz
2015-05-08
2015-05-08
  • casolorz

    casolorz - 2015-05-08

    I am using JSch to perform an SFTP download in Android. This is on a LAN with 802.11n and an SFTP server on a wired gigabit connection. I am getting about 8 mbytes/sec on a laptop (also 802.11n) with the same code but I am only getting 40kbytes/sec on Android. Are there some flags or something I need to turn on to get this to transfer faster? I have tried it on a Nexus 5 and a Nexus 6, both with Android 5.1. I also tried a couple of apps and one downloaded the file at 230kbytes/sec and the other right around 40kbytes/sec so I'm guessing one of them has the same issue I do.

    My test code is this:

    public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        JSch jsch = new JSch();
    
                        jsch.setConfig("StrictHostKeyChecking", "no");
                        Session session = jsch.getSession("ftptest", "192.168.1.205");
    
                        session.setPort(22);
                        session.setPassword("password");
                        session.connect();
                        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
                        channel.connect();
                        SftpProgressMonitor monitor = new SftpProgressMonitor() {
                            long finalCount = 0;
                            long start = -1;
    
                            @Override
                            public void init(int op, String src, String dest, long max) {
                                start = System.currentTimeMillis();
                            }
    
                            @Override
                            public boolean count(long count) {
                                finalCount += count;
                              /*  long took = (System.currentTimeMillis() - start) / 1000;
                                if (took > 0) {
                                    Log.w("SFTP", "Transferred so far " + finalCount + " at speed bytes/sec " + (finalCount / took));
                                }*/
                                return true;
                            }
    
                            @Override
                            public void end() {
                                long took = (System.currentTimeMillis() - start) / 1000;
                                Log.w("SFTP", "Transferred " + finalCount + " in " + took + " speed bytes/sec " + (finalCount  / took ));
                            }
                        };
                        InputStream stream = channel.get("file", monitor);
                        int read = -1;
                        byte[] bs = new byte[8192];
                        while((read = stream.read(bs)) >= 0){
                            //do nothing
                        }
                    } catch (JSchException e) {
                        e.printStackTrace();
                    } catch (SftpException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
            thread.start();
        }
    
    
    }
    
     
  • casolorz

    casolorz - 2015-05-08

    I would like to add that it is a lot faster, about 160kbytes/sec if I use the get method that writes into a file directly instead of giving me an input stream. It is still a lot slower than running it outside Android though.

     

Log in to post a comment.

MongoDB Logo MongoDB