简体   繁体   English

下载管理器仅下载 .bin 文件

[英]Download Manager Downloads .bin File Only

I have a problem with the download manager in android.我在 android 中的下载管理器有问题。 I am trying to download a .mp4 video file from a webview but no matter what I did, it always download the file with .bin extenstion.我正在尝试从 webview 下载 .mp4 视频文件,但无论我做什么,它总是下载带有 .bin 扩展名的文件。 it is not related to the filename, I know because I tried both guessFileName() function and manually entering a name but the result is same.它与文件名无关,我知道是因为我尝试了guessFileName()函数和手动输入名称,但结果是一样的。 In other browsers, there is no problem downloading the content(.mp4).在其他浏览器中,下载内容(.mp4)没有问题。 One issue may be about mimetype, it returns "application/octet-stream" therefore I tried request.setMimeType("video/mp4");一个问题可能是关于 mimetype,它返回"application/octet-stream"因此我尝试request.setMimeType("video/mp4"); but still I get the mimetype unchanged.但我仍然保持 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 class: MyWebViewClient 类:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return true;
}

Logs:日志:

I/TAG1: http://someurl.com/someurl
I/TAG1: attachment; filename="filename.mp4"
I/TAG1: application/octet-stream

One additional note: Downloading happens after redirecting the URL to other URL.附加说明:将 URL 重定向到其他 URL 后会发生下载。 But I have tried downloading without redirecting the URL, and still got the .bin file.但是我尝试在不重定向 URL 的情况下进行下载,并且仍然得到了 .bin 文件。

Removing删除

ForceType application/octet-stream

from .htaccess solves the issue从 .htaccess 解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM