Search code examples
androidjsch

Downloading files to Android


I'm writing an application and later on I will need to write a code for downloading file(s) in to the Android device from remote server. I can connect to a server using JSch library but I have no idea how to actually download a file. Any tips on what should I look for or where should I start? Thanks


Solution

  • You could try with something like this:

        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;
        boolean success = false;
        JSch jsch = new JSch();
        session = jsch.getSession("your_username", "your_hostname", "your_port");
        session.setPassword("your_password");
    
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
    
        channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
    
        String fileToDownload = "some_url_to_some_file";
    
        channelSftp.get(fileToDownload , "you_destination_path");
        success = true;
        if (success)
            System.out.println("Downloaded file: " + fileToDownload );
    

    Of course if you were to have a list of file names to download you'd had to iterate through it, also you'd need to add the necessary exception handling if something goes wrong.