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

URLsomeUrl=newURL("your url String"); //FilesomeFile=newFile("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;

publicclassDownloadTest {

    publicstaticvoidmain(String[] args) {

        Threadthread=newThread(){
            @Overridepublicvoidrun() {
                // TODO Auto-generated method stubsuper.run();
                try {
                    dowanloadFile(newURL("some url"), newFile("some file"));
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        thread.start();


    }

    privatestaticvoiddowanloadFile(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"