DownloadUtil2.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package com.wdkl.callingbed2.util;
  2. import android.support.annotation.NonNull;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import okhttp3.Call;
  8. import okhttp3.Callback;
  9. import okhttp3.OkHttpClient;
  10. import okhttp3.Request;
  11. import okhttp3.Response;
  12. public class DownloadUtil2 {
  13. private static final String TAG = "DownloadUtil2";
  14. private static DownloadUtil2 downloadUtil2;
  15. private final OkHttpClient okHttpClient;
  16. private DownloadUtil2() {
  17. okHttpClient = new OkHttpClient();
  18. }
  19. public static DownloadUtil2 getInstance() {
  20. if (downloadUtil2 == null) {
  21. synchronized (DownloadUtil.class) {
  22. if (downloadUtil2 == null) {
  23. downloadUtil2 = new DownloadUtil2();
  24. }
  25. }
  26. }
  27. return downloadUtil2;
  28. }
  29. /**
  30. * @param url 下载连接
  31. * @param listener 下载监听
  32. */
  33. public void download(final String url,final String dir, final String name, boolean redownload, final OnDownloadListener listener) {
  34. if (!redownload) {
  35. File file = new File(dir + "/" + name);
  36. if (file.exists()) {
  37. LogUtil.d(TAG, "file exist==" + file.getPath());
  38. if (listener != null) {
  39. listener.onDownloadSuccess(); // 下载完成
  40. }
  41. return;
  42. }
  43. }
  44. final String fileDir = isHaveExistDir(new File(dir), new File(dir + "/" + name));
  45. LogUtil.d(TAG, "url==" + url + ", fileDir==" + fileDir);
  46. if (fileDir == null) {
  47. ToastUtil.showToast("下载文件路径创建失败");
  48. if (listener != null) {
  49. listener.onDownloadFailed(); // 下载失败
  50. }
  51. return;
  52. }
  53. Request request = new Request.Builder().url(url).build();
  54. okHttpClient.newCall(request).enqueue(new Callback() {
  55. @Override
  56. public void onFailure(Call call, IOException e) {
  57. LogUtil.d(TAG, "onFailure==" + e.toString());
  58. if (listener != null) {
  59. listener.onDownloadFailed(); // 下载失败
  60. }
  61. }
  62. @Override
  63. public void onResponse(Call call, Response response) {
  64. LogUtil.d(TAG, "response==" + response.body().contentLength());
  65. InputStream is = null;
  66. byte[] buf = new byte[2048];
  67. int len;
  68. FileOutputStream fos = null;
  69. try {
  70. is = response.body().byteStream();
  71. long total = response.body().contentLength();
  72. File file = new File(fileDir, name);
  73. fos = new FileOutputStream(file);
  74. long sum = 0;
  75. while ((len = is.read(buf)) != -1) {
  76. fos.write(buf, 0, len);
  77. sum = sum + (long) len;
  78. //int progress = (int) (sum * 1.0f / total * 100);
  79. float sp = (float) sum / (float) total;
  80. int progress = (int) (sp * 100);
  81. //LogUtil.d(TAG, "progress==" + progress);
  82. if (listener != null) {
  83. listener.onDownloading(progress);// 下载中
  84. }
  85. }
  86. fos.flush();
  87. if (listener != null) {
  88. listener.onDownloadSuccess(); // 下载完成
  89. }
  90. } catch (Exception e) {
  91. LogUtil.d(TAG, "Exception==");
  92. if (listener != null) {
  93. listener.onDownloadFailed();
  94. }
  95. } finally {
  96. try {
  97. if (is != null)
  98. is.close();
  99. if (fos != null)
  100. fos.close();
  101. } catch (IOException e) {
  102. LogUtil.d(TAG, "IOException==");
  103. }
  104. }
  105. }
  106. });
  107. }
  108. private String isHaveExistDir(File dir, File file) {
  109. try {
  110. if (!dir.mkdirs()) {
  111. dir.createNewFile();
  112. }
  113. deleteFile(file);//只要文件名相同就可以自动替换(按道理此处不需要了,但为了保险起见还是先执行删除操作)。
  114. return dir.getAbsolutePath();
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. return null;
  119. }
  120. public boolean deleteFile(File downloadFile) {
  121. //if (!downloadFile.isDirectory()) throw new NotHaveThisFileException();
  122. return downloadFile.delete();
  123. }
  124. /**
  125. * @param url
  126. * @return 从下载连接中解析出文件名
  127. */
  128. @NonNull
  129. private String getNameFromUrl(String url) {
  130. return url.substring(url.lastIndexOf("/") + 1);
  131. }
  132. public interface OnDownloadListener {
  133. /**
  134. * 下载成功
  135. */
  136. void onDownloadSuccess();
  137. /**
  138. * @param progress 下载进度
  139. */
  140. void onDownloading(int progress);
  141. /**
  142. * 下载失败
  143. */
  144. void onDownloadFailed();
  145. }
  146. }