load File from URL - Android Network

Android examples for Network:URL

Description

load File from URL

Demo Code


//package com.java2s;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    private static final int CONNECT_TIMEOUT = 30000;
    private static final int READ_TIMEOUT = 120000;

    public static boolean loadFile(String urlString, File file) {
        try {//from  ww  w .j ava2s  . co m
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();

            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECT_TIMEOUT);
            conn.setRequestMethod("GET");
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(false);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "*/*");
            conn.setRequestProperty("Accept", "*/*");

            conn.connect();

            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                InputStream is = conn.getInputStream();
                FileOutputStream fos = new FileOutputStream(file);

                try {
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                    }
                } finally {
                    try {
                        is.close();
                    } catch (IOException ioException) {
                    }
                    try {
                        fos.flush();
                        fos.close();
                    } catch (IOException ioException) {
                    }
                }

                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            Log.e("KVEST_TAG", "getFile error " + e + "(" + urlString + ")");
            return false;
        }
    }
}

Related Tutorials