WebView Direct Download返回使用原始文件扩展名的HTML

时间:2014-04-19 00:25:58

标签: android webview broadcastreceiver download

我正在尝试在WebView内直接下载,而不是链接到浏览器。

webview.setDownloadListener(new DownloadListener() {
    @SuppressLint("DefaultLocale")
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
        MimeTypeMap mtm = MimeTypeMap.getSingleton();
        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Uri downloadUri = Uri.parse(url);

        // get file name. if filename exists in contentDisposition, use it. otherwise, use the last part of the url.
        String fileName = downloadUri.getLastPathSegment();
        int pos = 0;

        if ((pos = contentDisposition.toLowerCase().lastIndexOf("filename=")) >= 0) {
            fileName = contentDisposition.substring(pos + 9);
            pos = fileName.lastIndexOf(";");

            if (pos > 0) {
                fileName = fileName.substring(0, pos - 1);
            }
        }

        // predict MIME Type
        String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
        String mimeType = mtm.getMimeTypeFromExtension(fileExtension);

        // request saving in Download directory
        Request request = new DownloadManager.Request(downloadUri);
        request.setTitle(fileName);
        request.setDescription(url);
        request.setMimeType(mimeType);  
        request.setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS, fileName);

        Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS).mkdirs();

        // request in download manager
        downloadManager.enqueue(request);
    }
});

打开它,

// download complete toast
private BroadcastReceiver completeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Resources res = context.getResources();

        // make toast
        Toast.makeText(context, res.getString(R.string.download_complete), Toast.LENGTH_SHORT).show();

        // go to download finished window
        startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
        }
    };

    @Override
    protected void onPause() {
        super.onPause();

        // if app stops, stop reciever
        unregisterReceiver(completeReceiver);
    }

    @Override
    protected void onResume() {
        // app start, start reciever
        IntentFilter completeFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        registerReceiver(completeReceiver, completeFilter);
        super.onResume();
    }
}

但是,结果不是原始文件,而是使用原始文件扩展名的源的HTML格式;例如它使用.pdf,但它是一个HTML文件。

是什么导致了这个问题,我该如何解决?

我收到的文件

首先,使用普通的PDF查看器打开是不可能的,当我不打开PDF格式时,我会得到一个普通的HTML文档:

<!DOCTYPE html>
<html>
…
</html>

HTML文档中没有任何特殊内容。

1 个答案:

答案 0 :(得分:1)

我发现您的文件名附带引用,所以只需替换它。

fileName=fileName.replaceAll("\"", "");

您将获得正确的文件。我也在我的代码中使用了它,并且它已成功运作。