gpt4 book ai didi

android - 如何检查文件是否存在且内容相同

转载 作者:行者123 更新时间:2023-11-29 01:44:23 27 4
gpt4 key购买 nike

我正在寻找一种方法来了解文件是否存在,如果存在,是否具有相同的内容?如果是则不下载,否则下载文件。

在我的代码中,我需要在查看之前下载 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);
}
}

最佳答案

在下载文件之前和之后(以及将其放入/sdcard 之后)验证文件的 MD5 校验和 始终是个好主意。服务器端的正确实现可确保发布那里托管的文件的 MD5 总和。验证您下载的文件的 MD5 总和可确保您拥有完整、完整且未损坏的文件版本。

在您的情况下,您可以将 File_1 下载并存储到 sd-card 后的 MD5 校验和MD5 校验和 进行比较下载前的 File_2。

关于android - 如何检查文件是否存在且内容相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22345526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com