当我点击页面servlet发布数据上的按钮时,我想下载带有webview的文件。请求网址类似于website.com/download.htm。此网址响应包含文件网址作为附件但没有任何反应,我在Chrome远程调试工具中看到此响应:
Response Headers
Cache-Control:post-check=0, pre-check=0
Connection:Keep-Alive
Content-Disposition:attachment; filename=1436366157805NANItmuPUnKWRaQO.xls
Content-Encoding:gzip
Content-Language:en-US
Content-Type:application/vnd.ms-excel
Date:Wed, 08 Jul 2015 14:35:57 GMT
Expires:0
Keep-Alive:timeout=10, max=35
Pragma:public
Transfer-Encoding:chunked
X-Powered-By:Servlet/3.0
cookies:[]
content: {
size: 0,
mimeType: application/octet-stream,
compression: 403
},
redirectURL: ,
headersSize: 403,
bodySize: -403,
_transferSize:
我不是servlet页面的所有者,这是我的应用程序代码:
web = (WebView) findViewById(R.id.webView);
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAppCacheEnabled(true);
webSetting.setSupportMultipleWindows(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setDomStorageEnabled(true);
web.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
getWindow().setTitle(title);
}
});
web.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.d("error", "" + errorCode + description + failingUrl);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.d("started", url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d('url', url);
return false;
}
web.loadUrl("https://website.com");
web.setDownloadListener(new DownloadListener() {
public void onDownloadStart (String url, String userAgent, String contentDisposition, String mimetype,
long contentLength)
{
Log.d("download", url + "\n" + mimetype + contentDisposition + contentLength);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
final String[] separated = url.split("/");
final String myFile = separated[separated.length - 1];
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, myFile);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
}
});
});
我用chrome和stock浏览器检查了网站,文件下载得很好,但没有在webview上,我检查了不同的网站下载文件; webview使用直接网址在其他网站上下载文件。下载侦听器不适用于该URL。我在oncatstarted和onPagefinish的logcat中看到了website.com/download.htm,但是没有在shouldOverrideUrlLoading上看到。其他页面显示在所有三个功能上。有什么问题?