1

I´m trying to open a pdf that I have created using iText library in my browser, but it fails. This is the code I´m using to send to browser

    File file = new File(path);

    try{
        //InputStream stream=blob.getBinaryStream();
        InputStream streamEntrada = new FileInputStream(file);
        //ServletOutputStream fileOutputStream = response.getOutputStream();
        PrintWriter print = response.getWriter();

        int ibit = 256;
        while ((ibit) >= 0)
        {
            ibit = streamEntrada.read();
            print.write(ibit);
         }

        response.setContentType("application/text");
        response.setHeader("Content-Disposition",  "attachment;filename="+name);
        response.setHeader("Pragma", "cache");
        response.setHeader("Cache-control", "private, max-age=0");
        streamEntrada.close();
        print.close();

        return null;
    }
    catch(Exception e){

        return null;
    }
}

I tried with FileOutputStream but isn´t works. I´m desperate.

Thank you.

Now, I´m trying this way, but it doesn´t work:

public class MovilInfoAction extends DownloadAction{

protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    //Here the creation of the PDF

    //Storing data
    PdfData dPdf = pdf.drawPDF(terminal);

    String path = dPdf.getPath();//Path
    String name = dPdf.getName()+".pdf";//Pdf´s name
    String contentType = "application/pdf";//ContentType

    response.setContentType(contentType);
    response.setHeader("Content-Disposition","attachment; filename="+name);
    response.setHeader("Cache-control", "private, max-age=0");
    response.setHeader("Content-Disposition",  "inline"); 

    File file = new File(path);
    byte[] pdfBytes = es.vodafone.framework.utils.Utils.getBytesFromFile(file);

    return new ByteArrayStreamInfo(contentType, pdfBytes);
}

protected class ByteArrayStreamInfo implements StreamInfo {

    protected String contentType;
    protected byte[] bytes;

    public ByteArrayStreamInfo(String contentType, byte[] bytes) {
        this.contentType = contentType;
        this.bytes = bytes;
    }

    public String getContentType() {
        return contentType;
    }

    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(bytes);
    }
}

}

user623020
  • 11
  • 1
  • 3

5 Answers5

3

You specify the mimetype as application/text, when it should be application/pdf.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
3

You should set the Header and ContentType before you write the data. And set the Content Type to application/pdf.

morja
  • 8,297
  • 2
  • 39
  • 59
3

change

 response.setContentType("application/text");

to

response.setContentType("application/pdf");

and if you want your pdf to open in browser then make following change

response.setHeader("Content-Disposition",  "inline");
jmj
  • 237,923
  • 42
  • 401
  • 438
  • With the modifications indicated not works. It doesn´t give me an error but not works. Any suggestions? Could it be an incorrect selection of out Stream? – user623020 Feb 21 '11 at 09:48
  • may be.. is there any exception on the console ? – jmj Feb 21 '11 at 09:54
  • Nope. I post the other way how I trying now in the original post. – user623020 Feb 21 '11 at 10:24
  • I would suggest compare your code with [this](http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/) here just replace the above mentioned parameters of response – jmj Feb 21 '11 at 10:32
0

Android Default Browser requires GET Request. It does not understand POST Request and hence cannot download the attachment. You can send a GET request as by sending GET request, it resolved my problem. Android browser generates a GET request on its own and sends it back to server. The response received after second request will be considered final by the browser even if GET request is sent on first time by the servlet.

Ankit
  • 1
  • 1
    Can you provide a source? I'm pretty sure the version of Webkit used by Android handles issues POSTs properly, and properly interprets cache control headers. – Mike Samuel May 10 '12 at 21:25
0

Put the filename in double quote "

response.setHeader("Content-Disposition","attachment; filename=\"" + attachmentName + "\"");
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81