android文件下载代码,Android代码实现文件下载

import android.os.Environment;

import androidx.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 DownloadUtil {

private static final String TAG = DownloadUtil.class.getName();

private static DownloadUtil downloadUtil;

private final OkHttpClient okHttpClient;

public static DownloadUtil get() {

if (downloadUtil == null) {

downloadUtil = new DownloadUtil();

}

return downloadUtil;

}

private DownloadUtil() {

okHttpClient = new OkHttpClient();

}

/**

* url 下载连接

* saveDir 储存下载文件的SDCard目录

* listener 下载监听

*/

public void download(final String url, final String saveDir, final OnDownloadListener listener) {

Request request = new Request.Builder().url(url).build();

okHttpClient.newCall(request).enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

// 下载失败

listener.onDownloadFailed();

}

@Override

public void onResponse(Call call, Response response) throws IOException {

InputStream is = null;

byte[] buf = new byte[2048];

int len = 0;

FileOutputStream fos = null;

// 储存下载文件的目录

String savePath = isExistDir(saveDir);

try {

is = response.body().byteStream();

long total = response.body().contentLength();

File file = new File(savePath, getNameFromUrl(url));

fos = new FileOutputStream(file);

long sum = 0;

while ((len = is.read(buf)) != -1) {

fos.write(buf, 0, len);

sum += len;

int progress = (int) (sum * 1.0f / total * 100);

// 下载中

listener.onDownloading(progress);

}

fos.flush();

// 下载完成

listener.onDownloadSuccess();

} catch (Exception e) {

listener.onDownloadFailed();

} finally {

try {

if (is != null)

is.close();

} catch (IOException e) {

}

try {

if (fos != null)

fos.close();

} catch (IOException e) {

}

}

}

});

}

/**

* saveDir

* 判断下载目录是否存在

*/

private String isExistDir(String saveDir) throws IOException {

// 下载位置

File downloadFile = new File(saveDir);

if (!downloadFile.mkdirs()) {

downloadFile.createNewFile();

}

String savePath = downloadFile.getAbsolutePath();

return savePath;

}

/**

* 获取SD卡路径

*

* @return

*/

public static String getSDCardPath() {

String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;

LogUtils.i(TAG, "getSDCardPath:" + sdCardPath);

return sdCardPath;

}

/**

* url

* 从下载连接中解析出文件名

*/

@NonNull

public static String getNameFromUrl(String url) {

return url.substring(url.lastIndexOf("/") + 1);

}

public interface OnDownloadListener {

/**

* 下载成功

*/

void onDownloadSuccess();

/**

* @param progress

* 下载进度

*/

void onDownloading(int progress);

/**

* 下载失败

*/

void onDownloadFailed();

}

implementation 'com.squareup.okhttp3:okhttp:3.10.0'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值