2014-03-12 90 views
2

我很難找到一種方法來知道文件是否存在,如果存在,它是否具有相同的內容?如果是,則不要下載,否則下載該文件。如何檢查文件是否存在並具有相同的內容

在我的代碼中,我需要在查看它之前下載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); 
    } 
} 
+3

要測試內容是否相等,可以檢查他們的[MD5校驗和](http://stackoverflow.com/questions/13152736/how-to-generate-an-md5-checksum-for-a-file-in- android) – CodingIntrigue

+0

感謝您的鏈接,我剛剛閱讀了'MD5校驗和',發現這是解決方案。如果這不是評論,我會接受這個答案。 –

回答

1

它總是一個好主意文件的MD5 checksum之前,你下載它(並把它放在你的/ SD卡後)來驗證。在服務器端的正確實現可以確保發佈託管在那裏的文件的MD5總和。驗證您下載的文件的MD5總和可確保您擁有文件的完整,完整且未損壞的版本。

對於您的情況,您可以在下載文件之後下載並將其存儲在sd-card與File_2的MD5 checksum之前比較File_1的MD5 checksum

+0

這會很好,但那需要增加返回到我的API方法。現在在APP方面,我將使用新文件的「MD5校驗和」檢查SD卡中的「MD5校驗和」。 –

相關問題