JSF流下载Android设备上返回的.htm文件 [英] JSF streamed download on android device returns .htm file

查看:348
本文介绍了JSF流下载Android设备上返回的.htm文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前面临的一个奇怪的问题,在我的Andr​​oid设备上的网页。

I'm currently facing a weird problem in my webpage on android devices.

我想要做的就是让用户的PDF文件下载到自己的移动设备。因此,我提供一个下载按钮中描述的设置<一个href=\"http://stackoverflow.com/questions/9584736/jsf-file-download-nullpointerexception-with-view-parameter\">here.

What I want to do is to allow a user to download a pdf-file to his mobile device. Therefore I provide a download button as in the setup described here.

一切工作正常,只要我用我的桌面浏览器的的Mozilla Firefox 10。的*,但只要我改变我的移动设备的(SGS II,VERS的Andr​​oid 2.3.5) 的下载的结果取决于我使用的是浏览器的应用程序。

Everything works fine as long as I use my desktop browser Mozilla Firefox 10.* but as soon as I change to my mobile device (SGS II, Android vers. 2.3.5) the result of the download depends on the browser-app I'm using.

Mozilla和Opera移动:结果
似乎都能够正确地下载文件。

Mozilla and Opera mobile:
Both seem to be able to download the file correctly.

其他任何浏览器的应用程序(内置,海豚HD,...):结果
下载的文件是命名&LT;文件名&GT;的.pdf &LT;文件名&GT;的.htm 这两个重present一个的.htm - 文件显示网页的HTML源。

Any other browser-app (built-in, Dolphin HD,...):
Downloads a file either named <filename>.pdf or <filename>.htm which both represent a .htm-file showing the html-source of the page.

我已经试过:


  • 使用了 StreamedContent 办法走出的 PrimeFaces 的库

public StreamedContent getFile() {
    // prepare file for download
    // reference webDAV directory and get file as stream
    this.file = new Helper().getWebDavFile(customerId, fileName);

    return file;
}


描述

  • 手动直播的文件页面<一个href=\"http://stackoverflow.com/questions/9391838/how-to-stream-a-file-download-in-a-jsf-backing-bean\">here. (感谢BalusC)

  • Streamed the file manually to the page as described here. (Thx to 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(); 
    }  
    


  • 设置&LT; A HREF =方式&gt; 该文件的本地副本结果
    (我目前使用的是&LT;电话号码:的commandButton&GT; 所以我使用的方法执行重定向,而不​​是返回一个字符串,但它在这两个方面的工作)

  • Set an <a href=""> to a local copy of the file.
    (I'm currently using a <p:commandButton> so I've to use a method executing a redirect instead of returning a String but it works in both ways)

    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);
    }
    

    这最后的方法工作得很好,即使我的Andr​​oid设备上,但我不想走这条路,如果我能避免它,因为文件被从WebDAV流的我必须保存每个文件到服务器。这将产生更多的IO负载,并会迫使我手动清理。

    This final approach worked fine even on my android device but I do not want to go this route if I can avoid it because the file gets streamed from a WebDAV an I'd have to save each file to the server. This would produce more IO load and would force me to manually clean-up.

    该方法助手()。getWebDavFile 助手()。getWebDavFileManually 要么返回的 DefaultStreamedConten 的使用PrimeFaces或字节[] 的我自己的方法。

    The methods Helper().getWebDavFile and Helper().getWebDavFileManually either return a DefaultStreamedConten used by PrimeFaces or a byte[] for my own approach.

    我知道到目前为止什么:

    可惜不是我的问题:)解决方案。结果
    几个小时使用谷歌后,我发现有一个的双HTTP-POST请求的可能性。这将导致机器人内部的下载管理器(在碎文件下载的情况下使用)发送一个额外的后请求所处的状态会丢失。

    Unfortunately not the solution for my problem :).
    After many hours of using google I found out that there is the possibility of a double-http-post-request. This would cause androids internal download manager (used in case of broken file download) to send an additional post-request in which the state gets lost.

    由于rel=\"nofollow\">这个博客在 GET,POST,REST [更新20120208] 的)有一个人面临同样的问题。我试着在这个博客上提到的所有的方法,但没有成功。结果
    在此<一个href=\"http://androidforums.com/application-development/256987-android-phone-download-issue-asp-net-website.html#post3687287\"相对=nofollow>论坛
    有人分析了的 Wireshark的,得到了几乎相同的结论。结果
    我并没有发现任何更多的ressources所以我坚持这个。

    As described in this blog (see section GET, POST, REST [UPDATE 20120208]) there is someone facing the same problem. I've tried all approaches mentioned on this blog but didn't succeed.
    On this forum someone analyzed the same behavior with WireShark and got nearly to the same conclusion.
    I didn't found any more ressources so I'm stuck at this.

    我也贴在PrimeFaces 论坛刚以确保不会有任何关于已知问题&LT;号码:。fileDownload&GT; 组件

    I've also posted on the PrimeFaces forum just to make sure that there aren't any known issues regarding the <p:fileDownload> component.

    我想知道:

    我缺少的东西吗?结果
    是否有可能从一个JSF(HTTP-POST操作)的网页下载的Andr​​oid设备上的文件流?

    Am I missing something?
    Is there any possibility to download a streamed file from an JSF (http-post operating) webpage on an android device?

    任何帮助/建议/信息将AP preciated!结果
    在此先感谢!

    Any help/suggestion/information would be appreciated!
    Thanks in advance!

    推荐答案

    确定,之后,我所面临其他一些问题我终于回来赤在这个问题上的时间。

    OK, after I've faced some other issues I finally got the time to chek back on this problem.

    在spended上使用谷歌没有任何新的线索,在我的问题已经提到了一些更多的时间。

    After some more hours spended on using google I didn't got any new clues as already mentioned in my question.

    这仍然是一个事实,即某些Android浏览器无法处理POST请求并返回相应的文件。

    It still is the fact that some android browsers aren't able to handle POST request and return the appropriate file.

    正因为如此,我选择给的 servlet的方法的(如在评论中提到和描述的这里)尝试构建自己的HTTP-GET请求。

    Because of that I choose to give the servlet-approach (as mentioned in the comments and described here) a try and construct my own http-GET-request.

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        ...
        // Initialize servlet response 
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType(mime);
        response.setHeader("Content-Length", String.valueOf(data.length));
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    
        // write to response
        BufferedOutputStream output = null;
        try {
            output = new BufferedOutputStream(response.getOutputStream());
            output.write(data);
        }
        finally {
            output.close();
        }
    

    ,瞧!下载适用于任何 Android设备无论使用哪种浏览器

    And voilà: the download works on any android device no matter which browser is used!

    再次感谢@BalusC指着​​我在正确的方向。

    Thanks again @BalusC for pointing me in the right direction.

    如果我发现的时候,我会尽力的JSF-GET方法,太多,但起初我很高兴这一点。

    If I find the time I'll try the JSF-GET approach, too but at first I'm happy with this.

    如果还有人面临着同样的问题,或者能够提出另一种解决办法我就AP preciate任何贡献!

    If there is anyone facing the same problem or able to suggest another solution I would appreciate any contribution!

    这帮助了我,我会很开心:D

    This helped me, I'll have Fun :D!

    这篇关于JSF流下载Android设备上返回的.htm文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆