2017-11-17 6 views
0

Ich möchte, um diese pdf-Dateikonnten nicht pdf-Datei

https://scholar.najah.edu/sites/default/files/book/dllt-lhwy-wlrmz-fy-lfwlklwr-lshby.pdf 

Ich verwende diesen Code herunterladen pdf-Dateien zum Download, es funktioniert mit vielen Dateien in Ordnung, aber es scheiterte auf pdf-Datei wie oben

public void downloadFile(@NonNull String urlStr, @NonNull String fullFilePath, 
         @NonNull DownloadListener downloadListener) { 
    cancelStatus = false; 
    InputStream is = null; 
    File ffPath = null; 
    FileOutputStream fos = null; 
    try { 
     downloadListener.onProgress(0); 
     URL url = new URL(urlStr); 
     URLConnection conexion = url.openConnection(); 
     conexion.setReadTimeout(2000000); 
     conexion.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.0 Safari/532.5"); 
     System.setProperty("http.agent", ""); 
     conexion.connect(); 
     int lenghtOfFile = conexion.getContentLength(); 
     if (lenghtOfFile <= 0) lenghtOfFile = 1; 
     is = url.openStream(); 
     ffPath = new File(fullFilePath); 
     fos = new FileOutputStream(ffPath); 
     int count = 0; 
     long total = 0; 
     int progress = 0; 
     byte data[] = new byte[1024]; 
     while ((count = is.read(data)) != -1) { 
      if (cancelStatus == true) { 
       break; 
      } 
      total += count; 
      int progress_temp = (int) total * 100/lenghtOfFile; 
      if (progress != progress_temp) { 
       progress = progress_temp; 
       downloadListener.onProgress(progress >= 0 && progress <= 100 ? progress : 0); 
      } 
      fos.write(data, 0, count); 
      cancelStatus = downloadListener.onPacketDownloaded(total, lenghtOfFile); 
     } 
     if (is != null) is.close(); 
     if (fos != null) fos.close(); 

     if (lenghtOfFile <= 1) { 
      downloadListener.onComplete(); 
     } else if (ffPath.length() < lenghtOfFile) { 
      if (cancelStatus) { 
       downloadListener.onCancel(); 
      } else { 
       downloadListener.onError(); 
      } 

     } else if (ffPath.length() >= lenghtOfFile) { 
      downloadListener.onComplete(); 
     } 
     if (cancelStatus == true) { 
      if (ffPath != null) ffPath.delete(); 
     } 
    } catch (MalformedURLException e) { 
     try { 
      if (is != null) is.close(); 
      if (fos != null) fos.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     if (ffPath != null) ffPath.delete(); 
     downloadListener.onError(); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     try { 
      if (is != null) is.close(); 
      if (fos != null) fos.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     if (ffPath != null) ffPath.delete(); 
     downloadListener.onError(); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     try { 
      if (is != null) is.close(); 
      if (fos != null) fos.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     if (ffPath != null) ffPath.delete(); 
     downloadListener.onError(); 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

das Problem ist count = is.read(data) return -1 und brechen aus while Schleife nach einer Schleife, die Datei etwa 345 kb
helfen bitte

+0

'das Problem Zählung = is.read (Daten) gibt -1 zurück und bricht von der While-Schleife ab. Ist das wahr? Woher weißt du das? Die Schleife bremst auch wenn cancelStatus == true. Fügen Sie einige Protokollanweisungen ein, damit Sie besser wissen, was passiert. Weiter ist unklar, was 'downloadListener' wäre. Und von wo Sie downloadFile() aufrufen. – greenapps

Antwort

0

/** * Download-Datei verwendet auch die Datei herunterladen und speichern in Telefon * * @param fileURL enthalten Datei url * @param filename enthält Dateinamen */

public void DownloadFile(String fileURL, String fileName) { 
    try { 
     String RootDir = Environment.getExternalStorageDirectory() 
       + File.separator + "Cards List"; 
     File RootFile = new File(RootDir); 
     RootFile.mkdir(); 
     // File root = Environment.getExternalStorageDirectory(); 
     java.net.URL u = new URL(fileURL); 
     HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 
     FileOutputStream f = new FileOutputStream(new File(RootFile, 
       fileName + " abc " + ".pdf")); 
     InputStream in = c.getInputStream(); 
     byte[] buffer = new byte[1024]; 
     int len1 = 0; 

     while ((len1 = in.read(buffer)) > 0) { 
      f.write(buffer, 0, len1); 
     } 
     f.close(); 


    } catch (Exception e) { 

     Log.d("Error....", e.toString()); 
    } 

} // used to download the file from server 

class ProgressBack extends AsyncTask<String, Void, Void> { 
    ProgressDialog PD; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     PD = ProgressDialog.show(MainActivity.this, null, "Please Wait ...", true); 
     PD.setCancelable(true); 
    } 

    @Override 
    protected Void doInBackground(String... params) { 
     DownloadFile("https://scholar.najah.edu/sites/default/files/book/dllt-lhwy-wlrmz-fy-lfwlklwr-lshby.pdf", 
       "Cards List"); // calling DownloadFile 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     PD.dismiss(); 
     Toast.makeText(MainActivity.this, "Download Completed", Toast.LENGTH_SHORT).show(); 
    } 
} 
+0

Verwenden Sie den obigen Code, um die Datei herunterzuladen. Und die Datei wird auf Ihrem Gerät gespeichert. –

+0

Danke, es funktioniert gut – JustMe

Verwandte Themen