1. 程式人生 > >用HttpURLConnection 下載檔案工具類

用HttpURLConnection 下載檔案工具類

封裝的工具類

package cn.com.bjhj.utils;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 檔案下載utils
 * Created by JIANG on 2016/10/19.
 */
public class HttpClientUtils {
    private static final java.lang.String TAG ="HttpClientUtils" ;
    private String urlPath ;
    private String outPath;
    private String fileName;

    /**
     *
     * @param urlPath
     * @param outPath
     * @param fileName
     */
    public HttpClientUtils(String urlPath, String outPath, String fileName) {
        this.fileName = fileName;
        this.outPath = outPath;
        this.urlPath = urlPath;
    }

    /**
     * 下載檔案
     */
    public void downloadFile()
    {
        // 啟動新執行緒下載軟體
        new downloadFileThread().start();
    }
    /**
     * 下載檔案執行緒
     */
    private class downloadFileThread extends Thread {
        @Override
        public void run() {
            try {
                // 判斷SD卡是否存在,並且是否具有讀寫許可權
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    // 獲得儲存卡的路徑
                    //http://182.92.158.132/cloud/cloudfile?path=/public/notice/201606101737302282/16-131109141345 (1)_20160610173730880.jpg
                    //http://182.92.158.132/cloud/cloudfile?path=/public/notice/201606101737302282/16-131109141345%20(1)_20160610173730880.jpg
                    L.d(TAG,"我是路徑-===="+urlPath);
                    URL url = new URL(urlPath);
                    // 建立連線
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.connect();
                    L.i("頭:"+conn.getHeaderFields());;
                    // 獲取檔案大小
                    int length = conn.getContentLength();
                    L.i("length .." + length);
                    // 建立輸入流
                    InputStream is = conn.getInputStream();

                    File file = new File(outPath);
                    // 判斷檔案目錄是否存在
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    File apkFile = new File(outPath,fileName);
                    FileOutputStream fos = new FileOutputStream(apkFile);
                    int count = 0;
                    // 快取
                    byte buf[] = new byte[1024];
                    // 寫入到檔案中

                        int numread = is.read(buf);
                        count += numread;
                        L.i("count .."+ count +"  numread .." + numread);
                        // 寫入檔案
                        fos.write(buf, 0, numread);
                    fos.close();
                    is.close();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



        }
    }
}