问题描述
如果我听与 DownloadListener
,我得到我需要请求的后,浏览器已经请求它的网址。浏览器已经打开到URL的连接(这是怎么知道这是一个下载),为什么不能把它传给我的连接?
If I listen with DownloadListener
, I get the URL which I need to request after the browser already requested it. The browser already opened a connection to the URL (which is how it knows this is a download), why can't it pass me the connection?
我也试过分配一个自定义的 WebViewClient
到的WebView
和使用 shouldOverrideUrlLoading
赶上网址,他们被要求先。要下载文件的方式,我要求每一个URL浏览器之前和它的内容类型我决定是否下载与否,如果是那么我从本已打开的连接下载它,否则我关闭连接并指示浏览器加载它,浏览器...再次请求了。另外,在 shouldOverrideUrlLoading
我没有告诉哪种方法,我应该使用要求给定的URL是什么饼干。
I also tried to assign a custom WebViewClient
to the WebView
and use shouldOverrideUrlLoading
to catch URLs before they are requested. To download files that way, I request every URL before the browser and by it's Content-Type I decide whether to download it or not, if it is then I download it from the already-opened connection, otherwise I close the connection and instruct the browser to load it, and the browser... requests it again. Plus, in shouldOverrideUrlLoading
I'm not told which method and what cookies should I use to request the given URL.
我如何的没有的不必要请求两次,仍然能够下载文件的WebView?
How can I not unnecessarily request twice and still be able to download files with WebView?
推荐答案
为什么不直接使用的网址使用的OutputStream下载?下面是一个例子:
Why not just use the url to download it using outputstream? Here is an example:
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/file_name.extension");
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
这篇关于的WebView - 无法下载文件,而无需请求了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!