YLDownload.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.uas.hystorage.util;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.support.annotation.NonNull;
  5. import android.util.Log;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.concurrent.TimeUnit;
  11. import okhttp3.Call;
  12. import okhttp3.OkHttpClient;
  13. import okhttp3.Callback;
  14. import okhttp3.Request;
  15. import okhttp3.Response;
  16. import okhttp3.ResponseBody;
  17. /**
  18. * 文件下载工具栏
  19. */
  20. public class YLDownload {
  21. private static YLDownload ylDownload;
  22. private OkHttpClient client = new OkHttpClient.Builder()
  23. .connectTimeout(60, TimeUnit.SECONDS)
  24. .readTimeout(60, TimeUnit.SECONDS)
  25. .writeTimeout(60, TimeUnit.SECONDS)
  26. .build();
  27. // private OkHttpClient client = new OkHttpClient();
  28. private YLDownload() {
  29. }
  30. public static YLDownload getInstance() {
  31. if (ylDownload == null) {
  32. ylDownload = new YLDownload();
  33. }
  34. return ylDownload;
  35. }
  36. /**
  37. * @param url 下载地址
  38. * @param fileDir 储存下载文件的目录
  39. * @param fileName 文件名称
  40. * @param onDownloadListener 下载监听
  41. */
  42. public void download(String url, String fileDir, String fileName, OnDownloadListener onDownloadListener) {
  43. Request request = new Request.Builder()
  44. .url(url)
  45. .build();
  46. //MyLog.e("aaa","下载路径是:" + url + ",文件夹:" + fileDir + ",文件名:" + fileName);
  47. Call call = client.newCall(request);
  48. call.enqueue(new Callback() {
  49. @Override
  50. public void onFailure(@NonNull Call call, @NonNull IOException e) {
  51. onDownloadListener.onDownloadFailed(e);
  52. }
  53. @Override
  54. public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
  55. if (response.isSuccessful()) {
  56. MyLog.e("aaa","下载路径是:" + url + ",文件夹:" + fileDir + ",文件名:" + fileName);
  57. ResponseBody responseBody = response.body();
  58. File file_dir = new File(fileDir);
  59. if (!file_dir.exists()) {
  60. file_dir.mkdirs();
  61. }
  62. File file = new File(file_dir, fileName);
  63. FileOutputStream fos = new FileOutputStream(file);
  64. // // 获取输入流
  65. // InputStream inputStream = responseBody.byteStream();
  66. // // 创建文件输出流
  67. // FileOutputStream fos = new FileOutputStream(file);
  68. // // 定义缓冲区
  69. // byte[] buffer = new byte[4096];
  70. // int bytesRead;
  71. //
  72. // // 读取文件并写入到本地
  73. // while ((bytesRead = inputStream.read(buffer)) != -1) {
  74. // fos.write(buffer, 0, bytesRead);
  75. // }
  76. // // 关闭流
  77. // fos.close();
  78. // inputStream.close();
  79. fos.write(responseBody.bytes());
  80. fos.close();
  81. onDownloadListener.onDownloadSuccess();
  82. }else {
  83. MyLog.e("aaa","下载失败路径是:" + url + ",文件名:" + fileName);
  84. }
  85. // InputStream inputStream = null;
  86. // FileOutputStream outputStream = null;
  87. // byte[] buff = new byte[2048];
  88. // int length = 0;
  89. //
  90. // // 储存下载文件的目录
  91. // File file_dir = new File(fileDir);
  92. // if (!file_dir.exists()) {
  93. // file_dir.mkdirs();
  94. // }
  95. // File file = new File(file_dir, fileName);
  96. //
  97. // try {
  98. // inputStream = response.body().byteStream();
  99. // long total = response.body().contentLength();
  100. // outputStream = new FileOutputStream(file);
  101. // length = inputStream.read(buff);
  102. // long sum = 0;
  103. // while (length != -1) {
  104. // outputStream.write(buff, 0, length);
  105. // sum += length;
  106. // int progress = (int) (sum * 1.0f / total * 100);
  107. // // 下载中更新进度条
  108. // onDownloadListener.onDownloading(progress);
  109. // }
  110. // outputStream.flush();
  111. // // 下载完成
  112. // onDownloadListener.onDownloadSuccess();
  113. // } catch (Exception e) {
  114. // onDownloadListener.onDownloadFailed(e);
  115. // } finally {
  116. // if (inputStream != null) {
  117. // inputStream.close();
  118. // }
  119. // if (outputStream != null) {
  120. // outputStream.close();
  121. // }
  122. // }
  123. }
  124. });
  125. // try {
  126. // call.execute();
  127. // } catch (IOException e) {
  128. // e.printStackTrace();
  129. // }
  130. }
  131. }