I cannot open .pdf in my browser using Java

I am trying to open a pdf file that I created using the iText library in my browser, but it fails. This is the code that I use to send to the 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 not working. I'm desperate.

Thanks.

Now I try this way, but it does not 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);
    }
}

}

+1
source share
5 answers

You specify the mimetype type as application/textwhen it should be application/pdf.

+3
source

Before writing data, you must set the header and ContentType. And set the content type to application/pdf.

+3

 response.setContentType("application/text");

response.setContentType("application/pdf");

, pdf ,

response.setHeader("Content-Disposition",  "inline");
+3

"

response.setHeader("Content-Disposition","attachment; filename=\"" + attachmentName + "\"");
0

Android- GET-. POST- , , . GET, GET, . Android GET . , , , GET .

0

Source: https://habr.com/ru/post/1623434/


All Articles