我对android的下载管理器有个问题。我正在尝试从一个with视图下载一个.mp4视频文件,但是不管我做了什么,它总是下载带有.bin扩展的文件。它与文件名无关,我知道,因为我尝试了guessFileName()函数并手动输入了一个名称,但是结果是一样的。在其他浏览器中,下载内容(.mp4)没有问题。其中一个问题可能是mimetype,它返回"application/octet-stream",因此我尝试了request.setMimeType("video/mp4");,但仍然没有改变mimetype。
onCreate:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.setMimeType(mimetype);
request.addRequestHeader("User-Agent", userAgent);
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.setDescription("Downloading File...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
Log.i("TAG1", url);
Log.i("TAG1", contentDisposition);
Log.i("TAG1", mimetype);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);MyWebViewClient类:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}日志:
I/TAG1: http://someurl.com/someurl
I/TAG1: attachment; filename="filename.mp4"
I/TAG1: application/octet-stream另一个注意事项:下载是在将URL重定向到其他URL之后发生的。但我已经尝试下载而不重定向网址,但仍然得到了.bin文件。
发布于 2022-01-01 05:57:37
移除
ForceType application/octet-stream从.htaccess解决了这个问题
https://stackoverflow.com/questions/69201744
复制相似问题