I am using a JSP to display a single TIFF file. The flow is as follows:
- I am given a PDF to convert to a TIFF.
- 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.
- The 'black box' converts the PDF to a TIFF and saves the result to the OutputStream.
- 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" %>
<%
//...
%>
This wont work. You need to the content type of http response to image/tiff.
Fore more info: http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Response-Headers.html
The meta tag isn't what the browser looks at. Try using a scriptlet to set the content type on the response object. This page has some suggestions.
If you must use the JSP:
You can store the output stream to a file with a randomly generated name, then reference the file in the JSP. You need to make sure the output directory is in the web server's path.
This also comes with its own problems:
- You need to manage the filesystem in a way that deletes old files (so your server's disk will fill up).
- You need to manage synchronization to the file (no two server threads should update the same file).
- Oh, and you must make sure that images generated by one user are not visible by another.
I've seen plenty of people do this kind of thing, I'm sure there is a library.