JSF streamed download on android device returns .htm file

后端 未结 2 1427
一整个雨季
一整个雨季 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!

提交回复
热议问题