【发布时间】:2014-03-12 08:12:59
【问题描述】:
我在寻找一种方法来了解文件是否存在时遇到了麻烦,如果存在,它是否具有相同的内容?如果是则不下载,否则下载文件。
在我的代码中,我需要先下载 PDF 文件才能查看它。我已经检查文件是否存在,但它只检查文件名(这个我不确定)。 File 类的 exists() 方法是否只检查文件名?如果有,我怎么知道它是否有不同的内容?
这是我的代码:
class DownloadFileTask extends AsyncTask<String, String, String> {
private Context context;
public ProgressDialog pDialog;
private File pdfFile = new File(Environment
.getExternalStorageDirectory().getPath()
+ "/SAMPLE/"
+ pdfFileName);
public DownloadFileTask(Context context) {
this.context = context;
}
/**
* Before starting background thread Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
if (!pdfFile.exists()) {
pDialog = new ProgressDialog(context);
pDialog.setMessage(getString(R.string.loading));
pDialog.setCancelable(false);
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.show();
}
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... path) {
int count;
if (!pdfFile.exists()) {
if (Utility.isNetworkAvailable(parentActivityContext)) {
try {
String urlLastPath = Utility
.getLastPathFromUrl(path[0]);
String urlEncoded = URLEncoder.encode(urlLastPath,
"utf-8");
String urlDecoded = null;
String urlStr;
if (urlEncoded.contains(" ")) {
urlDecoded = urlEncoded.replaceAll(" ", "%20");
urlStr = SystemInfo.getResourceUrl() + "pdf/"
+ urlDecoded;
} else if (urlEncoded.contains("+")) {
urlDecoded = urlEncoded.replaceAll(
Pattern.quote("+"), "%20");
urlStr = SystemInfo.getResourceUrl() + "pdf/"
+ urlDecoded;
} else {
urlStr = SystemInfo.getResourceUrl() + "pdf/"
+ urlEncoded;
}
URL url = new URL(urlStr);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
// getting file length
int lengthOfFile = urlConnection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(
url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(Environment
.getExternalStorageDirectory().getPath()
+ "/'SAMPLE/" + pdfFileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""
+ (int) ((total * 100) / lengthOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("Error: ", e.getMessage());
}
} else {
openDialog(getString(R.string.error),
getString(R.string.internet_connection_error));
}
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
/** PDF reader code */
Intent intent = new Intent(parentActivityContext,
MuPDFActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(pdfFile));
startActivity(intent);
}
}
【问题讨论】:
-
要测试内容是否相等,您可以查看他们的MD5 checksums
-
感谢您的链接,我刚刚阅读了
MD5 checksum并发现这是解决方案。如果这不是评论,我会接受这个作为答案。
标签: android file file-io android-file