downloadFile文件下载封装

private Exception exception;
private FileOutputStream fos = null;
private InputStream is = null;
/**
 * TODO:文件下载
 * @param downurl:下载链接
 * @param path::下载文件存放地点
 * @param filename:下载文件命名
 * @param requestType:请求方式--GET/POST
 * @param requestProperty:HttpURLConnection 的请求参数
 */
private void downloadFile(String downurl, String path, String filename, String requestType,Map<String, String> requestProperty){
    int mTimeout  = 60 * 1000;
    try {
        //HttpsURLConnection.setDefaultSSLSocketFactory(SSLSocketUtils.createSSLSocketFactory());
       //HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketUtils.createTrustAllHostnameVerifier());
        HttpURLConnection connect = (HttpURLConnection) new URL(downurl).openConnection();
        connect.setRequestMethod(requestType);
        connect.setRequestProperty("Accept-Encoding", "identity");
        connect.setReadTimeout(mTimeout);
        connect.setConnectTimeout(mTimeout);
        if (requestProperty != null) {
            for (Map.Entry<String, String> entry : requestProperty.entrySet()) {
                connect.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        connect.connect();
        int responseCode = connect.getResponseCode();
        Log.d("DefaultDownloadProxy", " Content-Type: %s" + connect.getContentType());
        if (responseCode == HttpURLConnection.HTTP_OK) {
            is = connect.getInputStream();
            long length = connect.getContentLength();//获取下载文件的大小
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                length = (int) connect.getContentLengthLong();
            }
            int progress = 0;
            byte[] buffer = new byte[4096];
            int len;
            File file = new File(path, filename);
            boolean isExist = file.exists();
            if (isExist){
                return;
            }
            Log.e("DefaultDownloadProxy", "file 是否存在: " + isExist);
            fos = new FileOutputStream(file);
            while ((len = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
                progress += len;
                //更新进度
                publishProgress(progress, length);
            }
            fos.flush();
            connect.disconnect();
            //TODO:下载完毕,这里处理下载完成之后的逻辑,比如安装app、打开下载的pdf、加载下载的图片等等
        } else {//连接失败
            throw new ConnectException(String.format("responseCode = %d", responseCode));
        }
    } catch (Exception e) {
        this.exception = e;
        Log.d("DefaultDownloadProxy", e.toString());
    }finally {
        try {
            if (null != fos){
                fos.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            if (null != is){
                is.close();
            }
        }catch (Exception e){
            e.printStackTrace1();
        }
    }
}

private void updateProgress(int progress, long length){   ......   }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值