lmw
2023-06-06 7a563b559c48b9b339784c25fc5f0adc2ab5154e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.sinata.download.http;
 
 
 
import androidx.annotation.NonNull;
 
import com.sinata.download.utils.Logger;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
 
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
 
/**
 * author:Created by zhaohaoting on 2019/6/25
 * email:526309416@qq.com
 * desc:
 */
public class DownLoadManager {
 
    private static OkHttpClient downLoadHttpClient;
 
    static {
        OkHttpClient.Builder downLoadHttpClientBuilder = new OkHttpClient.Builder();
        downLoadHttpClient = downLoadHttpClientBuilder.connectTimeout(3, TimeUnit.MINUTES)
                .readTimeout(3, TimeUnit.MINUTES).writeTimeout(3, TimeUnit.MINUTES).build();
    }
 
    /**
     * 下载文件
     *
     * @param url
     * @param filePath
     * @param listener
     */
    public static void downLoadFile(String url, final String filePath, final DownloadListener listener) {
        downLoadHttpClient.newCall(new Request.Builder().url(url).get().build()).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                listener.onFail(e.getMessage());
            }
 
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                ResponseBody responseBody = response.body();
                if (responseBody == null) return;
                InputStream is = null;
                long currentLen = 0;
                byte[] buf = new byte[1024];
                int len = 0;
                FileOutputStream fos = null;
                try {
                    is = responseBody.byteStream();
                    File file = new File(filePath);
                    long length = responseBody.contentLength();
                    Logger.e("文件大小------------->", String.valueOf(length));
                    File parentFile = file.getParentFile();
                    if (!parentFile.exists()) {
                        boolean mkdirs = parentFile.mkdirs();
                    }
                    if (!file.exists()) {
                        boolean newFile = file.createNewFile();
                    } else {
                        if (file.length() == length) {//若本地存在当前下载文件,且文件大小一致(说明是同一个文件)则,无需再向磁盘写入
                            if (is != null) is.close();
                            listener.onProgress(100);
                            listener.onFinishDownload(true);
                            Logger.e("本地已有该文件------------->", "");
                            return;
                        }
                    }
                    listener.onStartDownload();
                    fos = new FileOutputStream(file);
                    while ((len = is.read(buf)) != -1) {
                        fos.write(buf, 0, len);
                        currentLen += len;
                        int progress = (int) (currentLen * 100 / length);
                        Logger.e("下载中------------->", String.valueOf(progress));
                        listener.onProgress(progress);
                    }
                    fos.flush();
                    //如果下载文件成功,第一个参数为文件的绝对路径
                    listener.onFinishDownload(false);
                } catch (Exception e) {
                    listener.onFail(e.getMessage());
                } finally {
                    try {
                        if (is != null) is.close();
                    } catch (Exception e) {
                    }
                    try {
                        if (fos != null) fos.close();
                    } catch (Exception e) {
                    }
                }
            }
        });
    }
}