Android:在 Web 视图中下载文件 octet-stream

huangapple go评论92阅读模式
英文:

Android: Download file octet-stream web view

问题

我想通过Android的Webview下载文件,但我无法做到。

我需要从以下格式的URL中下载:

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 (13/4/20)

我还忘记提到在上述代码中,mimeType是"application/octet-stream"。

英文:

I want to be able to download a file through Android's webview but I cannot do it.

The url I have to download from is in this format:

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

I am using this code:

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

<br/>
When I try to download all I get is a "download.bin" file.

As I check the function URLUtil.guessFileName(url, contentDisposition, mimetype) returns this as a file name.

Could you help me?

EDIT <br/>

If I rename the downloaded file from Android File manager app, it opens succefully.
So maybe the answer is how can I set the correctly mimeType of the downloaded file?

EDIT 2 (13/4/20) <br/>

Also I forgot to mention that in the above code mimeType is "application/octet-stream"

答案1

得分: 1

@Net 这是调试输出:

    this = {MainActivity$1@10423} 
    url = "http://{server_path}/Download?mimetype=application/octet-stream"
    userAgent = "Mozilla/5.0 (Linux; Android 10; Android SDK 搭载 x86 构建/QSR1.191030.002; wv) AppleWebKit/537.36 (KHTML,类似 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}
英文:

@Net Here is the debug output:

this = {MainActivity$1@10423} 
url = &quot;http://{server_path}/Download?mimetype=application/octet-stream&quot;
userAgent = &quot;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&quot;
contentDisposition = &quot;attachment; filename*=UTF-8&#39;&#39;M1FBACTFMB9F.pdf&quot;
mimetype = &quot;application/octet-stream&quot;
contentLength = 192748
request = {DownloadManager$Request@10430} 
dm = {DownloadManager@10432} 

答案2

得分: 0

你可以尝试使用以下代码处理你的文件:

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

或者尝试:

Files.probeContentType(path)

编辑

在你的请求中添加下一个头部:

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

You can try to do this with your file:

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

or try

Files.probeContentType(path)

EDIT

Add the next header to your request:

&quot;content-disposition&quot;, &quot;attachment; filename=\&quot;&quot; + fileName +&quot;\&quot;&quot;

huangapple
  • 本文由 发表于 2020年4月8日 18:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/61098825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定