通过接口返回数据下载文件的两种返回数据类型方法

Posted Trouble_Chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过接口返回数据下载文件的两种返回数据类型方法相关的知识,希望对你有一定的参考价值。

/** 下载pdf文件 */
private String updateContentName;//文件名
private Call call;
private boolean isSuccess = true;//是否下载成功
File target = null;

protected void download(String downloadUrl) {
showLoading();

target = new File(Environment.getExternalStorageDirectory() + "/ZyHealthy/PDF", updateContentName);
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}

OkHttpClient client = new OkHttpClient();
//构造请求
FormBody body = new FormBody.Builder()
.build();
final Request request1 = new Request.Builder()
.url(downloadUrl)
.post(body)
.build();

//包装Response使其支持进度回调
call = ProgressHelper.addProgressResponseListener(client, uiProgressResponseListener).newCall(request1);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
DesityUtil.showToast("下载失败,请尝试重新下载"+e);
closeLoading();
}

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

// //方案一 String型的byte数组
// int n = 1024;
// String bodyStr = response.body().string();
// try {
// String decodedString = new String(Base64Utils.decode(bodyStr));
// byte[] bytes = gson.fromJson(decodedString, new TypeToken<byte []>(){}.getType());
// FileOutputStream fos = new FileOutputStream(target, true);
// int length = bytes.length;
// int start = 0;
// while (length > start+n) {
// fos.write(bytes, start, n);
// start = start+n;
// }
// if (length != start+n) {
// n = length-start;
// fos.write(bytes, start, n);
// }
// fos.flush();
// fos.close();
// } catch (Exception e) {
// e.printStackTrace();
// }


//方案二 io流
InputStream is = response.body().byteStream();
FileOutputStream fos = new FileOutputStream(target, true);
byte[] buffer = new byte[2048];//缓冲数组2kB
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
is.close();
fos.close();


closeLoading();

if (target.exists()) {
dialogFragment = new PromptDialogFragment();
dialogFragment
.setTitle(getResources().getString(R.string.tjbb_exportPDF) + " /ZyHealthy/PDF/" + updateContentName)
.setMissVisibility(View.GONE)
.setSureOnClickListener(new NoDoubleClickListener() {

@Override
protected void onNoDoubleClick(View v) {
dialogFragment.dismiss();
}
});
dialogFragment.show(getFragmentManager(), "FullScreenDownPDFFragment");
} else {
DesityUtil.showToast("下载失败,请尝试重新下载");
}
}
});
}

//这个是ui线程回调,可直接操作UI
PromptDialogFragment dialogFragment;
UIProgressListener uiProgressResponseListener = new UIProgressListener() {
@Override
public void onUIProgress(long bytesRead, long contentLength, boolean done) {
isSuccess = done;
NumberFormat formatter = new DecimalFormat("00.#");
// String format = formatter.format(Float.valueOf(bytesRead) / contentLength * 100);
// 下载完成 弹框提示
if (100 <= (Float.valueOf(bytesRead) / contentLength * 100)) {
// DesityUtil.showToast("下载完成");
}
}

@Override
public void onUIStart(long bytesRead, long contentLength, boolean done) {
super.onUIStart(bytesRead, contentLength, done);
}

@Override
public void onUIFinish(long bytesRead, long contentLength, boolean done) {
super.onUIFinish(bytesRead, contentLength, done);
isSuccess = done;
}
};


public void showLoading() {
if (loadingFragment != null)
loadingFragment.show(getFragmentManager(), "loading");
}

public void closeLoading() {
if (loadingFragment != null)
loadingFragment.dismiss();
}

以上是关于通过接口返回数据下载文件的两种返回数据类型方法的主要内容,如果未能解决你的问题,请参考以下文章

谈谈关于文件下载

谈谈关于文件下载

谈谈关于文件下载

2021-07-20 dao层获取数据的两种途径

谈谈关于文件下载

js学习之-------前端下载文件