我需要从网站下载PDF,但是此 PDF 是通过 ASPX 运行中生成的 。 strong>服务,所以我无法正常下载。我尝试了一些不同的下载,但是下载并非每次都完成,有时无法将字节提取到文件中。
我的代码:
public static class OpenPdfFromAspxUrl extends AsyncTask<Void, Integer, Boolean> {
private Activity mActivity;
private String fileUrl;
private String FILENAME = "bmed_document.pdf";
public OpenPdfFromAspxUrl(Activity activity, String fileURL) {
this.mActivity = activity;
this.fileUrl = fileURL;
}
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
//showProgressDialog(mActivity, mActivity.getResources().getString(R.string.common_getting_document), false);
}
/**
* Downloading file in background thread
*/
@Override
protected Boolean doInBackground(Void... voids) {
try {
FileUtils.copyURLToFile(
new URL(fileUrl),
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + FILENAME),
120000,
60000);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onCancelled() {
super.onCancelled();
//ownProgressDialog.dismiss();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
int current = values[0];
String percentaje = new StringBuilder().append("").append(current).append(" %").toString();
// mActivity.runOnUiThread(() -> {
// ownProgressDialog.setMessage(percentaje);
// ownProgressDialog.setPercentage(current);
// });
}
/**
* After completing background task
* Dismiss the progress dialog
**/
@Override
protected void onPostExecute(Boolean stored) {
// dismiss the dialog after the file was downloaded
try {
// if (ownProgressDialog != null){
// if(ownProgressDialog.getDialog().isShowing()) {
// ownProgressDialog.dismiss();
// }
// }
} catch (NullPointerException e) {
e.printStackTrace();
}
if (stored) {
// Displaying downloaded pdf
File newFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + FILENAME);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(newFile), "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
mActivity.startActivity(Intent.createChooser(target, mActivity.getResources().getString(R.string.common_open_with)));
} else {
showAlert(
mActivity,
mActivity.getResources().getString(R.string.common_error_inform),
mActivity.getResources().getString(R.string.common_error_getting_document),
false,
mActivity.getResources().getString(R.string.common_accept),
new OnSingleClickListener() {
@Override
public void onSingleClick(View v) {
// dismiss the dialog after the file was downloaded
try {
if (getDialog() != null && getDialog().isShowing()) {
getDialog().dismiss();
BusProvider.getInstance().post(new ShowHideLoadingEvent(false, false));
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}
},
null,
null);
}
}
}
我已经使用博客StackAbuse - How to Download a File from a URL in Java中写的所有方法,但有时会失败。
如何成功完成此下载?