【发布时间】:2016-08-04 13:07:31
【问题描述】:
我正在从我的服务器下载一个 pdf 文件。最近我遇到了 当我的互联网在下载过程中断开时,我 无法打开 pdf 文件,因为它不完整。如何删除 如果下载不完整并且文件包含 该扩展程序已在此过程中保存?这是我的代码 当前用于从服务器下载 pdf。
class DownloadFileAsync extends AsyncTask<String, String, String>
{
final NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
final int notify_id = 1;
final NotificationManager NM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
private String resp;
@Override
protected String doInBackground(String... params) {
int count;
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Download Status...");
builder.setContentText("Download in Progress...");
try {
URL url = new URL(params[0]);
URLConnection connection = url.openConnection();
connection.connect();
int lengthOfFile = connection.getContentLength();
Log.d("ANDRO_ASYNC", "LENGTH OF FILE : " + lengthOfFile);
String fileName = params[0].substring(params[0].lastIndexOf('/') + 1, params[0].length());
Log.d("FILENAME", fileName);
resp = fileName;
if (isSDPresent) {
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream("sdcard/shatayushi/" + fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = inputStream.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lengthOfFile));
outputStream.write(data, 0, count);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} else {
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream(getFilesDir() + "/shatayushi/" + fileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = inputStream.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lengthOfFile));
outputStream.write(data, 0, count);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return params[0];
}
@Override
protected void onPostExecute(String filename) {
Log.d("PARAM", filename);
String fname = filename.substring(filename.lastIndexOf('/')+1, filename.length());
int position = Arrays.asList(pdf_url).indexOf(filename);
Log.d("Position",String.valueOf(position));
String magazine_id = magazine_names[position];
if (isSDPresent) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
builder.setProgress(0, 0, false);
builder.setContentText("Download Complete...");
NM.notify(notify_id, builder.build());
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String device_imei = telephonyManager.getDeviceId();
update_info(device_imei,magazine_id);
Uri uri = Uri.parse("/storage/emulated/0/shatayushi/" + fname);
Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
} else {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
builder.setProgress(0, 0, false);
builder.setContentText("Download Complete...");
NM.notify(notify_id, builder.build());
Uri uri = Uri.parse(getFilesDir() + "/shatayushi/" + fname);
Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected void onProgressUpdate(String... values) {
Log.d("ANDRO_ASYNC", values[0]);
progressDialog.setProgress(Integer.parseInt(values[0]));
builder.setProgress(100, Integer.parseInt(values[0]), false);
NM.notify(notify_id, builder.build());
}
}
感谢任何帮助或建议。谢谢。
【问题讨论】:
-
感谢@PaoloForgia 的编辑,您能提供解决方案吗?
-
file.delete()呢? -
有没有办法查出是下载不完整还是java中保存了不完整的文件。所以根据这个条件我可以删除文件。
-
比较下载文件的文件长度和预期长度。或删除文件以防下载未完成(进度条未达到 100%)