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" %>
<%
//...
%>