Pdf display in browser using Java servlet

I have a pdf file in my application. I need to display a PDF in a browser. I am reading the file as fileInputStream, I need to display the PDF in the browser in my application. But I have no pdf path, I have a file stream.

Please give me some tips and examples.


I used ajax to display pdf, I use the javascript request method of the javascript call_method () method to call the showPdf action. In action, showpdf simply converts the PDF file as ByteArrayOutputStream and writes the result to the out put stream. But this shows the result below.

Result in JSP :

% PDF-1.4% 1 endstream endobj 4 0 obj <→> / MediaBox [0 0 595 842] → endobj 1 0 obj <> endobj 3 0 obj <endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000389 00000 n 0000000015 00000 n 0000000477 00000 n 0000000232 00000 n 0000000540 00000 n 0000000585 00000 n Trailer <142354f5ebefd65d6aacd33a7cb2b4ab>] / Info 6 0 R / Size 7 → startxref 707 %% EOF

Please give some suggestion.

Javascript ajax:

call_method(); <br/> function call_method(){ Ext.Ajax.request({ waitMsg: 'Saving changes...', url:'test.action?method=showPdf', params : { }, failure:function(response,options){ }, success:function(response,options){ $("#pdf_content").show(); $("#pdf_content").html(response.responseText); $("#pdf_content").show('slow'); } }); } 

Java Methods:

 public String showPdf() throws IOException { getResponse().setContentType("application/pdf"); getResponse().setHeader("Content-disposition","inline; filename=automatic_start.pdf" ); ByteArrayOutputStream baos = getByteArrayOutputStream(); getResponse().setContentLength(baos.size()); ServletOutputStream sos; sos = getResponse().getOutputStream(); baos.writeTo(sos); sos.flush(); return null; } private ByteArrayOutputStream getByteArrayOutputStream() throws IOException { String filePath = "/homefolder/"; String folderPath=filePath+"1122/automatic_start.pdf"; File file = new File(folderPath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[256]; try { for (int readNum; (readNum = fis.read(buf)) != -1;) { bos.write(buf, 0, readNum); //no doubt here is 0 //Writes len bytes from the specified byte array starting at offset off to this byte array output stream. System.out.println("read " + readNum + " bytes,"); } } catch (IOException ex) { ex.printStackTrace(); } return bos; } 
+7
source share
2 answers

In your servlet, set the MIME type to the correct one for PDF: application/pdf

See http://www.iana.org/assignments/media-types/

+5
source

You will need to write your InputStream for the OutputStream response as follows:

  • Your Content-Disposition must be inline .
  • Your Content-Type should be application/pdf .
  • Your Content-Length will be the length (in bytes) of all the data in the InputStream .

After installation, write the input stream data to the response output stream.

Some of this effect:

 /* (non-Javadoc) * @see org.bfs.bayweb.util.renderer.ServletViewRenderer#render(javax.servlet.ServletRequest, javax.servlet.ServletResponse) */ public void render(ServletRequest request, ServletResponse response) throws IOException { // TODO Auto-generated method stub try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int inputStreamLength = 0; int length = 0; while ((length = getInputStream().read(buffer)) > 0) { inputStreamLength += length; baos.write(buffer, 0, length); } if (inputStreamLength > getContentLength()) { setContentLength(inputStreamLength); } if (response instanceof HttpServletResponse) { HttpServletResponse httpResponse = (HttpServletResponse) response; httpResponse.reset(); httpResponse.setHeader("Content-Type", getContentType()); httpResponse.setHeader("Content-Length", String.valueOf(getContentLength())); httpResponse.setHeader("Content-Disposition", "\"" + getContentDisposition() + "\"" + ((getFileName() != null && !getFileName().isEmpty()) ? "; filename=\"" + getFileName() + "\"": "")); } response.getOutputStream().write(baos.toByteArray(), 0, (int)getContentLength()); //finally response.getOutputStream().flush(); //clear baos = null; } finally { // TODO Auto-generated catch block close(response.getOutputStream()); close(getInputStream()); } } private void close(Closeable resource) throws IOException { if (resource != null) { resource.close(); } } 
+9
source

All Articles