Displaying pdf in jsp

后端 未结 3 1772
既然无缘
既然无缘 2020-11-27 08:15

I have written a jsp page to display contents of pdf, but end up with ascii codes in jsp. I want to display the contents of pdf in jsp. Whats the part that I have missed. Wh

相关标签:
3条回答
  • 2020-11-27 08:38

    Supposing we completely ignore the advice against using a JSP (and as BalusC says - there are BETTER WAYS), here's an ugly and shameful little bodge that worked okay for me. It doesn't even set all the right headers, but here goes:

    <%@ page import="java.io.File" %><%@ page import="org.apache.commons.io.FileUtils" %><%
    File pdfFile = (File) request.getAttribute("pdf");
    byte[] pdfByteArray = FileUtils.readFileToByteArray(pdfFile);
    response.setContentType("application/pdf");
    response.getOutputStream().write(pdfByteArray);
    response.getOutputStream().flush();
    %>
    

    It's important ensure there are no new-lines (or other whitespace) outside the scriptlet tags.

    They made me do it, okay?!

    0 讨论(0)
  • 2020-11-27 08:47

    I could see multiple problems:

    • There are extra html tags at the top and bottom of your JSP. You do not want them there - you only want to have the pdf contents in your response output.
    • The code sets content type is multiple times. That is probably not the root cause, however make sure you do it only once (set it to application/pdf)
    • In the while loop, data are first written to the response output stream, then a toString() is written to the out (which is actually a Writer instance opened on the response output stream - the one in outs). Only use the response stream in the loop, as

      while ((c = in.read(buf, 0, buf.length)) > 0) { outs.write(buf, 0, c);
      }

    0 讨论(0)
  • 2020-11-27 08:52

    JSP is the wrong tool for the job of serving a file download. JSP is designed as a view technology with the intent to easily produce HTML output with taglibs and EL. Basically, with your JSP approach, your PDF file is cluttered with <!DOCTYPE>, <html> etc tags and therefore corrupted and not recognizable as a valid PDF file. This is by the way one of the reasons why using scriptlets is a bad practice. It has namely completely confused you as to how stuff is supposed to work. In this particular case, that is using a normal Java class for the file download job.

    You should be using a servlet instead. Here's a kickoff example, assuming that Servlet 3.0 and Java 7 is available:

    @WebServlet("/foo.pdf")
    public class PdfServlet extends HttpServlet {
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            File file = new File("/absolute/path/to/foo.pdf");
            response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
            response.setHeader("Content-Length", String.valueOf(file.length()));
            response.setHeader("Content-Disposition", "inline; filename=\"foo.pdf\"");
            Files.copy(file.toPath(), response.getOutputStream());
        }
    
    }
    

    (if Servlet 3.0 is not available, then map it in web.xml the usual way, if Java 7 is not available, then use a read/write loop the usual way)

    Just copypaste this class in its entirety into your project and open the desired PDF file by /contextpath/Saba_PhBill.pdf instead of /contextpath/youroriginal.jsp (after having organized it in a package and autocompleted the necessary imports in the class, of course).

    E.g. as follows in a JSP where you'd like to show the PDF inline:

    <object data="${pageContext.request.contextPath}/Saba_PhBill.pdf" type="application/pdf" width="500" height="300">
        <a href="${pageContext.request.contextPath}/Saba_PhBill.pdf">Download file.pdf</a>
    </object>
    

    (the <a> link is meant as graceful degradation when the browser being used doesn't support inlining application/pdf content in a HTML document, i.e. when it doesn't have Adobe Reader plugin installed)

    See also:

    • Simplest way to serve static data from outside the application server in a Java web application
    • Abstract template for a static resource servlet supporting ETags, caching, etc
    0 讨论(0)
提交回复
热议问题