| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package com.uas.hystorage.util;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.support.annotation.NonNull;
- import android.util.Log;
- 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.OkHttpClient;
- import okhttp3.Callback;
- import okhttp3.Request;
- import okhttp3.Response;
- import okhttp3.ResponseBody;
- /**
- * 文件下载工具栏
- */
- public class YLDownload {
- private static YLDownload ylDownload;
- private OkHttpClient client = new OkHttpClient.Builder()
- .connectTimeout(60, TimeUnit.SECONDS)
- .readTimeout(60, TimeUnit.SECONDS)
- .writeTimeout(60, TimeUnit.SECONDS)
- .build();
- // private OkHttpClient client = new OkHttpClient();
- private YLDownload() {
- }
- public static YLDownload getInstance() {
- if (ylDownload == null) {
- ylDownload = new YLDownload();
- }
- return ylDownload;
- }
- /**
- * @param url 下载地址
- * @param fileDir 储存下载文件的目录
- * @param fileName 文件名称
- * @param onDownloadListener 下载监听
- */
- public void download(String url, String fileDir, String fileName, OnDownloadListener onDownloadListener) {
- Request request = new Request.Builder()
- .url(url)
- .build();
- //MyLog.e("aaa","下载路径是:" + url + ",文件夹:" + fileDir + ",文件名:" + fileName);
- Call call = client.newCall(request);
- call.enqueue(new Callback() {
- @Override
- public void onFailure(@NonNull Call call, @NonNull IOException e) {
- onDownloadListener.onDownloadFailed(e);
- }
- @Override
- public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
- if (response.isSuccessful()) {
- MyLog.e("aaa","下载路径是:" + url + ",文件夹:" + fileDir + ",文件名:" + fileName);
- ResponseBody responseBody = response.body();
- File file_dir = new File(fileDir);
- if (!file_dir.exists()) {
- file_dir.mkdirs();
- }
- File file = new File(file_dir, fileName);
- FileOutputStream fos = new FileOutputStream(file);
- // // 获取输入流
- // InputStream inputStream = responseBody.byteStream();
- // // 创建文件输出流
- // FileOutputStream fos = new FileOutputStream(file);
- // // 定义缓冲区
- // byte[] buffer = new byte[4096];
- // int bytesRead;
- //
- // // 读取文件并写入到本地
- // while ((bytesRead = inputStream.read(buffer)) != -1) {
- // fos.write(buffer, 0, bytesRead);
- // }
- // // 关闭流
- // fos.close();
- // inputStream.close();
- fos.write(responseBody.bytes());
- fos.close();
- onDownloadListener.onDownloadSuccess();
- }else {
- MyLog.e("aaa","下载失败路径是:" + url + ",文件名:" + fileName);
- }
- // InputStream inputStream = null;
- // FileOutputStream outputStream = null;
- // byte[] buff = new byte[2048];
- // int length = 0;
- //
- // // 储存下载文件的目录
- // File file_dir = new File(fileDir);
- // if (!file_dir.exists()) {
- // file_dir.mkdirs();
- // }
- // File file = new File(file_dir, fileName);
- //
- // try {
- // inputStream = response.body().byteStream();
- // long total = response.body().contentLength();
- // outputStream = new FileOutputStream(file);
- // length = inputStream.read(buff);
- // long sum = 0;
- // while (length != -1) {
- // outputStream.write(buff, 0, length);
- // sum += length;
- // int progress = (int) (sum * 1.0f / total * 100);
- // // 下载中更新进度条
- // onDownloadListener.onDownloading(progress);
- // }
- // outputStream.flush();
- // // 下载完成
- // onDownloadListener.onDownloadSuccess();
- // } catch (Exception e) {
- // onDownloadListener.onDownloadFailed(e);
- // } finally {
- // if (inputStream != null) {
- // inputStream.close();
- // }
- // if (outputStream != null) {
- // outputStream.close();
- // }
- // }
- }
- });
- // try {
- // call.execute();
- // } catch (IOException e) {
- // e.printStackTrace();
- // }
- }
- }
|