123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package com.wdkl.callingbed2.util;
- import android.support.annotation.NonNull;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import okhttp3.Call;
- import okhttp3.Callback;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.Response;
- public class DownloadUtil2 {
- private static final String TAG = "DownloadUtil2";
- private static DownloadUtil2 downloadUtil2;
- private final OkHttpClient okHttpClient;
- private DownloadUtil2() {
- okHttpClient = new OkHttpClient();
- }
- public static DownloadUtil2 getInstance() {
- if (downloadUtil2 == null) {
- synchronized (DownloadUtil.class) {
- if (downloadUtil2 == null) {
- downloadUtil2 = new DownloadUtil2();
- }
- }
- }
- return downloadUtil2;
- }
- /**
- * @param url 下载连接
- * @param listener 下载监听
- */
- public void download(final String url,final String dir, final String name, boolean redownload, final OnDownloadListener listener) {
- if (!redownload) {
- File file = new File(dir + "/" + name);
- if (file.exists()) {
- LogUtil.d(TAG, "file exist==" + file.getPath());
- if (listener != null) {
- listener.onDownloadSuccess(); // 下载完成
- }
- return;
- }
- }
- final String fileDir = isHaveExistDir(new File(dir), new File(dir + "/" + name));
- LogUtil.d(TAG, "url==" + url + ", fileDir==" + fileDir);
- if (fileDir == null) {
- ToastUtil.showToast("下载文件路径创建失败");
- if (listener != null) {
- listener.onDownloadFailed(); // 下载失败
- }
- return;
- }
- Request request = new Request.Builder().url(url).build();
- okHttpClient.newCall(request).enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- LogUtil.d(TAG, "onFailure==" + e.toString());
- if (listener != null) {
- listener.onDownloadFailed(); // 下载失败
- }
- }
- @Override
- public void onResponse(Call call, Response response) {
- LogUtil.d(TAG, "response==" + response.body().contentLength());
- InputStream is = null;
- byte[] buf = new byte[2048];
- int len;
- FileOutputStream fos = null;
- try {
- is = response.body().byteStream();
- long total = response.body().contentLength();
- File file = new File(fileDir, name);
- fos = new FileOutputStream(file);
- long sum = 0;
- while ((len = is.read(buf)) != -1) {
- fos.write(buf, 0, len);
- sum = sum + (long) len;
- //int progress = (int) (sum * 1.0f / total * 100);
- float sp = (float) sum / (float) total;
- int progress = (int) (sp * 100);
- //LogUtil.d(TAG, "progress==" + progress);
- if (listener != null) {
- listener.onDownloading(progress);// 下载中
- }
- }
- fos.flush();
- if (listener != null) {
- listener.onDownloadSuccess(); // 下载完成
- }
- } catch (Exception e) {
- LogUtil.d(TAG, "Exception==");
- if (listener != null) {
- listener.onDownloadFailed();
- }
- } finally {
- try {
- if (is != null)
- is.close();
- if (fos != null)
- fos.close();
- } catch (IOException e) {
- LogUtil.d(TAG, "IOException==");
- }
- }
- }
- });
- }
- private String isHaveExistDir(File dir, File file) {
- try {
- if (!dir.mkdirs()) {
- dir.createNewFile();
- }
- deleteFile(file);//只要文件名相同就可以自动替换(按道理此处不需要了,但为了保险起见还是先执行删除操作)。
- return dir.getAbsolutePath();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public boolean deleteFile(File downloadFile) {
- //if (!downloadFile.isDirectory()) throw new NotHaveThisFileException();
- return downloadFile.delete();
- }
- /**
- * @param url
- * @return 从下载连接中解析出文件名
- */
- @NonNull
- private String getNameFromUrl(String url) {
- return url.substring(url.lastIndexOf("/") + 1);
- }
- public interface OnDownloadListener {
- /**
- * 下载成功
- */
- void onDownloadSuccess();
- /**
- * @param progress 下载进度
- */
- void onDownloading(int progress);
- /**
- * 下载失败
- */
- void onDownloadFailed();
- }
- }
|