Displaying pdf in jsp

后端 未结 3 1799
既然无缘
既然无缘 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?!

提交回复
热议问题