3

Returning outputstream tiff file in JSP

 2 years ago
source link: https://www.codesd.com/item/returning-outputstream-tiff-file-in-jsp.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Returning outputstream tiff file in JSP

advertisements

I am using a JSP to display a single TIFF file. The flow is as follows:

  1. I am given a PDF to convert to a TIFF.
  2. I feed a 'black box' API the PDF in the form of a File object and an OutputStream (I am currently using a ByteArrayOutputStream but that can change as needed.
  3. The 'black box' converts the PDF to a TIFF and saves the result to the OutputStream.
  4. I use out.println(outputstream) to spit out the TIFF.

The problem is that I am getting a text stream instead of a displayed image. I have used the following head/meta tag:

    <head><title>PDF to TIFF tester</title>
  <META HTTP-EQUIV="Content-Script-Type" CONTENT="image/tiff"></head>
  <body>

But that does not change the end result. Any help?


You shouldn't use JSP for this. It's a view technology providing a textbased template to put HTML/CSS/JS code in and facilities to interact with backend Java code with help of taglibs (JSTL and so on) and EL (Expression Language, the ${} things).

A TIFF image isn't character (text) data. It's a binary data. You really need to use a servlet for this. You shouldn't use Writer methods to return binary data. You should use OutputStream methods for this. Otherwise the binary data would get corrupted (that's also what happens in a JSP since it under the hoods uses a Writer).

Here's a kickoff example how your servlet should look like:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String pdfFilename = request.getParameter("filename");
    File pdfFile = new File("/path/to/all/pdf/files", pdfFilename);

    response.setHeader("Content-Type", "image/tiff");
    doYourThingToConvertPdfFileToTiff(pdfFile, response.getOutputStream());
}

Map this servlet on an url-pattern of for example /pdf2tiff so that you can invoke it by http://example.com/contextname/pdf2tiff?filename=file.pdf in links or browser address bar or even in src attribute of an <img> element.

The doYourThingToConvertPdfFileToTiff is your "black box" API which seems to already write the TIFF to the given OutputStream. Just make use of it and pass the one of the HTTP response through.


Update: If you really, really need to use JSP for this, you could just write the same code in JSP as you would do in a Servlet class. You can practically copypaste it. Only ensure that you are not writinig any template text to the stream, this includes linebreaks and whitespace outside the scriptlets. Otherwise it would get written to the binary file as well and corrupt it.

If you have multiple scriptlet blocks, then you need to arrange them so that there's no linebreak between the ending %> of a scriptlet and the starting <% of the next scriptlet. Thus, e.g.

<%@page import="java.io.File" %><%
    //...
%>

instead of

<%@page import="java.io.File" %>
<%
    //...
%>




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK