2017-11-30 9 views
0

ユニバーサルAndroid WebView Appを使用してWebページにアクセスするアプリケーションを作成しています。このWebページにはダウンロード可能なpdfsがあり、ログインしたユーザーのみが利用できます。 pdfがプラグインによって生成され、http応答で送信されるため、組み込みのDownload Managerが機能しません。 自分で接続を実装しようとしましたが、このコードを微調整しようとしました。http://www.codejava.net/java-se/networking/use-httpurlconnection-to-download-file-from-an-http-urlプラグインで生成されたPDFをWebビューでダウンロードする

接続は正常に確立されていますが、受信されたContent-Lengthが-1であるためファイルはダウンロードされません。何が問題なの?私はちょうどDownloadManagerは私が必要なクッキーを使用して構成することができることを見出し

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; 
} 
} 

答えて

0

は、ここでは、コードです。誰かがそれを必要とする場合のためのコードはここにあります(私はもう質問で言及したクラスを使用しません)。

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); 
関連する問題