淘先锋技术网

首页 1 2 3 4 5 6 7
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){   ......   }