JSF streamed download on android device returns .htm file

后端 未结 2 1423
一整个雨季
一整个雨季 2021-01-21 05:39

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

What I want to do is to allow a user to download a pdf-file to his mobile device. Therefore I pro

相关标签:
2条回答
  • 2021-01-21 06:01

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

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

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

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

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

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

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

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

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

    0 讨论(0)
  • 2021-01-21 06:21

    had the same problem. I used the example code from primefaces.org for downloading a pdf with mobile devices. It was working fine "on my machine" but not on a iPad Safari or Android browsers. Problem was the content type in the Example-Controller provided in the showcase:

    file = new DefaultStreamedContent(stream, "image/jpg", "some.pdf");
    

    Which works only for images. Well, brain on mode:

    file = new DefaultStreamedContent(stream, "application/pdf", "some.pdf"); 
    

    and everything works fine now.

    0 讨论(0)
提交回复
热议问题