2017-11-30 8 views
0

Ich erstelle eine App mit der universellen Android WebView App, um auf eine Webseite zuzugreifen. Diese Webseite enthält einige herunterladbare PDFs, die nur angemeldeten Benutzern zur Verfügung stehen. Der integrierte Download-Manager funktioniert nicht, da die PDF-Datei von einem Plugin generiert und in einer HTTP-Antwort gesendet wird. Ich versuchte, die Verbindung selbst, zwicken diesen Code http://www.codejava.net/java-se/networking/use-httpurlconnection-to-download-file-from-an-http-urlHerunterladen eines von einem Plugin in einer Webansicht erstellten PDFs

Es funktioniert und die Verbindung hergestellt ist ganz gut, aber die Datei heruntergeladen ist nicht zu implementieren, da anscheinend die empfangenen Content-Length -1 ist. Was könnte das Problem sein?

Hier ist der Code:

public class HttpDownloadUtility extends AsyncTask<String, Integer, String> { 
private static final int BUFFER_SIZE = 4096; 

/** 
* Downloads a file from a URL 
* @param fileURL HTTP URL of the file to be downloaded 
* @param saveDir path of the directory to save the file 
* @throws IOException 
*/ 
public String downloadFile(String fileURL, String saveDir) throws IOException { 

    String fileName = ""; 
    URL url = new URL(fileURL); 

    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 
    httpConn.setRequestProperty("Cookie", android.webkit.CookieManager.getInstance().getCookie("http://mywebsite.com")); 

    httpConn.connect(); 

    int responseCode = httpConn.getResponseCode(); 

    // always check HTTP response code first 
    if (responseCode == HttpURLConnection.HTTP_OK) { 

     String disposition = httpConn.getHeaderField("Content-Disposition"); 
     String contentType = httpConn.getContentType(); 
     int contentLength = httpConn.getContentLength(); 

     if (disposition != null) { 
      // extracts file name from header field 

      int index = disposition.indexOf("filename="); 
      if (index > 0) { 
       fileName = disposition.substring(index + 10, 
         disposition.length() - 1); 
      } 
     } else { 
      // extracts file name from URL 
      fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1, 
        fileURL.length()); 
     } 

     System.out.println(url.toString()); 
     System.out.println("Content-Type = " + contentType); 
     System.out.println("Content-Disposition = " + disposition); 
     System.out.println("Content-Length = " + contentLength); 
     System.out.println("fileName = " + fileName); 

     // opens input stream from the HTTP connection 
     InputStream inputStream = httpConn.getInputStream(); 
     String saveFilePath = saveDir + File.separator + fileName; 

     // opens an output stream to save into file 
     FileOutputStream outputStream = new FileOutputStream(saveFilePath); 

     int bytesRead = -1; 
     byte[] buffer = new byte[BUFFER_SIZE]; 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      outputStream.write(buffer, 0, bytesRead); 
     } 

     outputStream.close(); 
     inputStream.close(); 

     System.out.println("File downloaded"); 



    } else { 
     System.out.println("No file to download. Server replied HTTP code: " + responseCode); 
    } 
    httpConn.disconnect(); 

    return fileName; 
} 

@Override 
protected String doInBackground(String... url) { 

    String downloadedFile = ""; 

    try{ 
     downloadedFile = downloadFile(url[0], Environment.DIRECTORY_DOWNLOADS); 
    }catch(Exception e){ 
     System.out.println(e.toString()); 
    } 
    return downloadedFile; 
} 
} 

Antwort

0

Ich fand nur, dass der Downloadmanager kann mit den Cookies konfiguriert werden, die ich brauchte. Hier ist der Code für den Fall, dass jemand es braucht (ich verwende nicht die Klasse, die ich in der Frage erwähnt habe).

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "mypdf.pdf"); 
       request.allowScanningByMediaScanner(); 
       request.setNotificationVisibility(1); 
       request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url)); 
       DownloadManager manager = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE); 
       manager.enqueue(request); 
Verwandte Themen