我的Web应用程序是使用JSF2.2和PrimeFaces 5构建的。我有一个用户可以下载文档的功能。 (" .pdf"," .csv"," .xls"和" .log")
我创建了一个android webview应用程序并加载了URL,除了下载功能外,一切正常。当我使用桌面浏览器或Chrome等移动浏览器访问网址时,Firefox下载工作正常,没有任何问题,文件下载成功。
我尝试调试应用程序并找到" DownloadListener"甚至在下载点击时都没有调用。
primefaces按钮:
<p:commandButton id="download" action="#{documents.download()}" icon="ui-icon ui-icon-arrow-d" styleClass="mpzero" ajax="false" update="@form" />
java下载:
public void download() throws IOException {
byte[] pdfData = null;
String fileName = null;
String docId = "13215466546";
documents = new Documents();
pdfData = service.getPdfdoc(docId);
if (pdfData != null) {
FileOutputStream f = null;
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
response.reset();
response.setContentLengthLong(pdfData.length);
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "must-revalidate");
response.addHeader("Expires", "-1");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
fileName = "Doc_" + sdf.format(Calendar.getInstance().getTime()) + docId;
response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + ".pdf\"");
OutputStream output = response.getOutputStream();
output.write(pdfData);
output.close();
facesContext.responseComplete();
} catch (Exception e) {
} finally {
if (f != null)
f.close();
}
} else {
//do something
}
Android Webview:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_main);
WebView mywebview = (WebView) findViewById(webview);
mywebview.clearCache(true);
mywebview.clearHistory();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(null);
cookieManager.flush();
WebSettings myView = mywebview.getSettings();
myView.setAllowFileAccess(false);
myView.setGeolocationEnabled(false);
myView.setAppCacheEnabled(false);
myView.setCacheMode(WebSettings.LOAD_NO_CACHE);
myView.setJavaScriptEnabled(true);
myView.setBuiltInZoomControls(false);
mywebview.loadUrl(url);
mywebview.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient implements DownloadListener {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimeType,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition,
mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File...",
Toast.LENGTH_LONG).show();
}
}
}
由于此下载问题,我的申请不完整。我在这里做错了什么,请建议我一个解决方案。提前谢谢。