当我点击JSF页面中的下载按钮时,我正试图在Android的WebView上触发DownloadListener事件,这些下载链接是动态生成的。 它在谷歌浏览器中运行得很好,但它在WebView上无效,我无法理解。
我只需要在单击按钮时触发此事件。 你能救我吗?
下载:
public void download(Download obj) throws IOException {
byte[] download = .... // obj byte[] loaded here
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context
.getExternalContext().getResponse();
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", String.valueOf(download.length));
response.setHeader("Content-Disposition", "attachment;filename=\"download\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new ByteArrayInputStream(download),
DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(),
DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
input.close();
output.close();
}
context.responseComplete();
}
数据表下载:
<p:dataTable value="#{downloadBean.downloads}" var="obj"
emptyMessage="#{erros.sem_registros}">
<p:column headerText="#{msgs.acoes}" styleClass="width50">
<h:form>
<h:commandLink value="#{msgs.download}" action="#{downloadBean.download(obj)}" />
</h:form>
</p:column>
</p:dataTable>
WebView事件:
viewer.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
try {
Toast.makeText(MainActivity.this, "Download ativo", Toast.LENGTH_SHORT).show();
String retorno = new DownloadUtil().getNew(url);
if (retorno != null) {
findBT();
openBT();
sendData(retorno);
closeBT();
} else {
Toast.makeText(MainActivity.this, "Não foi possível receber o arquivo do servidor", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Conexão mal sucedida", Toast.LENGTH_SHORT).show();
}
}
});