我目前在 Android 设备上的网页中遇到一个奇怪的问题。
我想做的是允许用户将 pdf 文件下载到他的移动设备上。因此,我提供了一个下载按钮,如所描述的设置中所示here.
只要我使用桌面浏览器 *Mozilla Firefox 10.**,一切都可以正常工作,但一旦我更改为移动设备(SGS II,Android 版本 2.3.5)下载的结果取决于我使用的浏览器应用程序。
Mozilla 和 Opera 移动设备:
两者似乎都能正确下载文件。
任何其他浏览器应用程序(内置、Dolphin HD...):
下载名为<filename>.pdf
or <filename>.htm
两者都代表一个.htm- 显示页面 html 源的文件。
我尝试过的:
-
使用了StreamedContent
方法出自PrimeFaces library
public StreamedContent getFile() {
// prepare file for download
// reference webDAV directory and get file as stream
this.file = new Helper().getWebDavFile(customerId, fileName);
return file;
}
-
按照描述将文件手动流式传输到页面here。 (感谢 BalusC)
public void download() throws IOException {
byte[] is = new Helper().getWebDavFileManually(customerId, fileName);
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
ec.setResponseContentType("application/pdf");
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName.toUpperCase() + "\"");
OutputStream output = ec.getResponseOutputStream();
output.write(is);
fc.responseComplete();
}
-
Set an <a href="">
到文件的本地副本。
(我目前正在使用<p:commandButton>
所以我必须使用执行重定向的方法而不是返回字符串,但它以两种方式工作)
public void goToLink() throws IOException {
// get WebDAV file and save temporarily
byte[] b = new Helper().getWebDavFileManually(customerId, fileName);
String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/") + fileName;
File f = new File(path);
try {
FileOutputStream fos = new FileOutputStream(f);
fos.write(b);
link = "http://someurl/somepage/" + fileName;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
// use link
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(link);
}
即使在我的 Android 设备上,这最终方法也能正常工作,但我不要如果我可以避免走这条路,因为文件是从 WebDAV 流式传输的,我必须将每个文件保存到服务器。这会产生更多的 IO 负载并迫使我手动清理。
方法Helper().getWebDavFile
and Helper().getWebDavFileManually
要么返回一个默认流媒体内容由 PrimeFaces 或byte[]对于我自己的方法。
到目前为止我所知道的:
不幸的是,这不是我的问题的解决方案:)。
使用谷歌几个小时后,我发现有可能双 http 后请求。这将导致 Android 内部下载管理器(在文件下载损坏的情况下使用)发送额外的后请求,其中状态会丢失。
如中所述这个博客(参见部分获取、发布、休息 [更新 20120208])有人面临同样的问题。我已经尝试过该博客中提到的所有方法,但没有成功。
对此forum有人分析了相同的行为WireShark并得出了几乎相同的结论。
我没有找到更多资源,所以我陷入了困境。
我也在 PrimeFaces 上发过帖子forum只是为了确保不存在任何已知问题<p:fileDownload>
成分。
我想知道的是:
我错过了什么吗?
是否有可能从 Android 设备上的 JSF(http-post 操作)网页下载流式文件?
任何帮助/建议/信息将不胜感激!
提前致谢!