用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();
            }



        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值