【问题标题】:Android: Download file octet-stream web viewAndroid:下载文件 octet-stream 网络视图
【发布时间】:2020-07-20 18:06:55
【问题描述】:

我希望能够通过 Android 的 webview 下载文件,但我做不到。

我必须下载的网址是这种格式:

http://{server_path}/Download?mimetype=application/octet-stream

我正在使用此代码:

myWebView.setDownloadListener(new DownloadListener() {
  public void onDownloadStart(String url, String userAgent,
                              String contentDisposition, String mimetype,
                              long contentLength) {
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

    request.setMimeType(mimetype);
    //------------------------COOKIE!!------------------------
    String cookies = CookieManager.getInstance().getCookie(url);
    request.addRequestHeader("cookie", cookies);
    //------------------------COOKIE!!------------------------
    request.addRequestHeader("User-Agent", userAgent);
    request.setDescription("Downloading file...");
    request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
    DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    long donwloadedFileId = dm.enqueue(request);
    String downloadedMimeType = dm.getMimeTypeForDownloadedFile(donwloadedFileId)
    Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
  }
});


当我尝试下载时,我得到的只是一个“download.bin”文件。

当我检查函数 URLUtil.guessFileName(url, contentDisposition, mimetype) 时,它会将其作为文件名返回。

你能帮帮我吗?

编辑

如果我从 Android 文件管理器应用重命名下载的文件,它会成功打开。 所以也许答案是如何设置下载文件的正确 mimeType?

编辑 2(20 年 4 月 13 日)

另外我忘了在上面的代码中mimeType是“application/octet-stream”

【问题讨论】:

    标签: java android webview


    【解决方案1】:

    @Net 这是调试输出:

    this = {MainActivity$1@10423} 
    url = "http://{server_path}/Download?mimetype=application/octet-stream"
    userAgent = "Mozilla/5.0 (Linux; Android 10; Android SDK built for x86 Build/QSR1.191030.002; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.185 Mobile Safari/537.36"
    contentDisposition = "attachment; filename*=UTF-8''M1FBACTFMB9F.pdf"
    mimetype = "application/octet-stream"
    contentLength = 192748
    request = {DownloadManager$Request@10430} 
    dm = {DownloadManager@10432} 
    

    【讨论】:

      【解决方案2】:

      您可以尝试对您的文件执行此操作:

      File file = new File(yourFileHere);
      mimeType= URLConnection.guessContentTypeFromName(file.getName());
      

      或者试试

      Files.probeContentType(path)
      

      编辑

      将下一个标头添加到您的请求中:

      "content-disposition", "attachment; filename=\"" + fileName +"\""
      

      【讨论】:

      • 输出结果也是“application/octet-stream”
      • 您在 url 中收到的文件的扩展名是什么? application/octet-stream 意思是“下载这个文件”
      • 下面是我的调试输出
      • 我不知道我是否理解错了,但它不再起作用了。我添加了这个:request.addRequestHeader("content-disposition", contentDisposition);
      猜你喜欢
      • 2010-12-28
      • 2011-11-21
      • 1970-01-01
      • 2013-12-28
      • 2019-10-12
      • 2023-02-03
      • 2018-06-11
      • 2016-02-25
      相关资源
      最近更新 更多