Skip to content Skip to sidebar Skip to footer

Android Downloading Multiple Files With InputStream FileOutputStream

So I have an app which downloads certain files, dedicated to a client of mine who is hosting his files on a remote location, and i'm doing so using the code below: public class Dow

Solution 1:

Following code uses commons-io-2.4.jar library to make your work easy by handling low level data movements as you would focus on method in hand

URL someUrl = new URL("your url String"); //
File someFile = new File("your file, where you want to save the data");
FileUtils.copyURLToFile(someUrl, someFile );

if you want to call this statement few time to download different files from the server following code might give you an idea what you might want to do, but you will have to write it's equivalent code to run in android which you want to probably AsyncTask

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class DownloadTest {

    public static void main(String[] args) {

        Thread thread = new Thread(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                try {
                    dowanloadFile(new URL("some url"), new File("some file"));
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        thread.start();


    }

    private static void dowanloadFile(URL url, File file){
        try {
            FileUtils.copyURLToFile(url, file );
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Post a Comment for "Android Downloading Multiple Files With InputStream FileOutputStream"