2016-12-29 170 views
0

我試圖使用DownloadManager從服務器上下載文件,但它沒有下載並說下載失敗,我讀過下載管理器存在問題在下面的鏈接 android-chrome-browser-unnecessarily-renames-names-types-of-downloaded-files所以我試圖通過使用下面的方法添加RequestHeader到DownloadManagerRequest下載文件,但沒有發生任何事情。請找到我的Downloader類的下面的代碼部分任何人請建議我使用DownloadManager從url下載文件的正確方法。下載使用DownLoadManager.Request從URL下載文件時失敗

網址:

的http:// 吸藏。達網絡/ 95 - ****/95-3194.pdf AWSAccessKeyId = T9YG9HWQC1LHE5G5VF38 &過期= 1483443248 &簽名= Z%2BkWzfEI2VawbCx%2F2Yto1kPcJKA%3D

由於

 public void download(Uri uri) { 
       if (!isDownloading()) { 
        String fileName=getName(uri); 
        DownloadManager.Request request = new DownloadManager.Request(uri); 
        request.setTitle(fileName); 
        request.setNotificationVisibility(1); 
        request.allowScanningByMediaScanner(); 
        request.addRequestHeader("Content-Type", "application/octet-stream"); 
        request.addRequestHeader("Content-Disposition", "attachment; filename=\""+fileName.split("\\.")[0]+"."+fileName.split("\\.")[1].toUpperCase()+"\""); 
        String cookieContent = getCookieFromAppCookieManager(uri.getHost()); 
        request.addRequestHeader("Cookie", cookieContent); 
        downloadId = downloadManager.enqueue(request); 
        register(); 
       } 
      } 

和下面的框返回的文件名

private String getName(Uri uri) { 
     String name=uri.toString().split("jabord/")[1].split("\\?")[0]; 
     return name; 
    } 

返回appcookie經理

public String getCookieFromAppCookieManager(String url){ 
     android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance(); 
     if (cookieManager == null) { 
      return null; 
     } 
     String rawCookieHeader = null; 

     // Extract Set-Cookie header value from Android app CookieManager for this URL 
     rawCookieHeader = cookieManager.getCookie(url); 
     if (rawCookieHeader == null) { 
      return null; 
     } 

     return rawCookieHeader; 
    }; 

回答

1

請刪除標題並設置mimetype然後它將工作。例如

public void download(Uri uri) { 
    if (!isDownloading()) { 
     String fileName=getName(uri); 
     DownloadManager.Request request = new DownloadManager.Request(uri); 
     request.setTitle(fileName); 
     request.setNotificationVisibility(1); 
     request.allowScanningByMediaScanner(); 
     request.setMimeType("application/pdf"); 
     Log.e("Extension with ","UpperCase-->"+"\""+fileName.split("\\.")[0]+"."+fileName.split("\\.")[1].toUpperCase()+"\""); 
     downloadId = downloadManager.enqueue(request); 
     register(); 
    } 
}