1

I am trying to download a pdf file from a Java page.

This is the following code :

    String format = "pdf";
    String reportName = "test" + "." + format;

    byte[] content = newsletterManager.getPdfFile(selectedNewsletterInstance.getIdentity());

    
    final FacesContext fc = FacesContext.getCurrentInstance();
    final HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
    response.reset();

    response.setHeader("Content-disposition", "attachment; filename=" + reportName);
    response.setContentType("application/" + format);


    OutputStream ouputStream = null;
    try {
        ouputStream = response.getOutputStream();
        ouputStream.write(content);
    } catch (IOException e) {
        e.printStackTrace();
    }

    PrintStream ps = null;
    try {
        ps = new PrintStream(ouputStream, true, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    ps.flush();
    fc.responseComplete();

I'm sure that content is good because I used it to create a local file on my PC and it worked well. It created a pdf doc that i could open and read.

I can't find why, but it just does nothing when executing the code above.

EDIT:

Here is the header of the server's response :

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename=test.pdf
Content-Type: application/pdf
Transfer-Encoding: chunked
Date: Wed, 03 Nov 2021 14:48:41 GMT

Header of request :

POST /newsletterinterface.xhtml HTTP/1.1
Host: localhost:8443
Connection: keep-alive
Content-Length: 968
sec-ch-ua: "Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/xml, text/xml, */*; q=0.01
Faces-Request: partial/ajax
X-Requested-With: XMLHttpRequest
sec-ch-ua-platform: "Windows"
Origin: https://localhost:8443
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://localhost:8443/oxway/newsletterinterface.xhtml
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Could you please provide what is wrong, do you get any exceptions, have you debugged? How can we reproduce your issue? – memoricab Nov 03 '21 at 10:37
  • I don't get any exception, this is why i can't really know where it comes from. Just nothing happens. –  Nov 03 '21 at 10:40
  • Then you need to debug your code. – memoricab Nov 03 '21 at 10:41
  • Why you need the `PrintStream ps ...` code? Could this consume your outputStream? – pleft Nov 03 '21 at 10:43
  • https://stackoverflow.com/a/3592258/3635454 – pleft Nov 03 '21 at 10:44
  • That's what i've done, as i said, content variable is good, no problem from here. The response construction is also good as i used the exact same code lines for another similar case. –  Nov 03 '21 at 10:44
  • @pleft I removed this part of the code and it still doesn't work. –  Nov 03 '21 at 10:48
  • @BalusC Non-ajax request, would it change anything? –  Nov 03 '21 at 10:59
  • @BalusC Allright, i'll do that. Thanks –  Nov 03 '21 at 11:02
  • @BalusC I debugged and can't see where the problem comes from. Everything seems to proceed exactly like an other methods that does the same thing in an other part of my project. But just nothing happens in my browser. –  Nov 03 '21 at 13:22
  • @BalusC It can't be a network problem since i can download other files from other methods –  Nov 03 '21 at 14:19
  • @BalusC I edited to add some more informations if needed. It looks like request header is ajax? –  Nov 03 '21 at 14:53

0 Answers0