在JSP上从outputstream返回tiff文件 [英] Return tiff file from outputstream on JSP

查看:105
本文介绍了在JSP上从outputstream返回tiff文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSP显示单个TIFF文件.流程如下:

  1. 我收到了要转换为TIFF的PDF.
  2. 我以File对象和OutputStream的形式向PDF提供了黑匣子" API(我目前正在使用ByteArrayOutputStream,但可以根据需要进行更改.
  3. 黑匣子"将PDF转换为TIFF,并将结果保存到OutputStream.
  4. 我使用out.println(outputstream)吐出TIFF.

问题是我正在获取文本流而不是显示的图像.我使用了以下head/meta标签:

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

但这不会改变最终结果.有帮助吗?

解决方案

您不应为此使用JSP.这是一种视图技术,它提供了一个基于文本的模板,可以将HTML/CSS/JS代码放入其中,并且可以借助taglib(JSTL等)和EL(表达语言,${}东西)与后端Java代码进行交互.

TIFF图像不是字符(文本)数据.这是一个二进制数据.您确实需要为此使用servlet.您不应该使用Writer方法返回二进制数据.您应该为此使用OutputStream方法.否则二进制数据将被破坏(这也是JSP中发生的情况,因为它在后台使用Writer).

这是一个启动示例,您的servlet应该是这样的:

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());
}

将此servlet映射到例如/pdf2tiffurl-pattern上,以便您可以通过http://example.com/contextname/pdf2tiff?filename=file.pdf在链接或浏览器地址栏中甚至在<img>元素的src属性中调用它.

doYourThingToConvertPdfFileToTiff是您的黑匣子" API,似乎已经将TIFF写入给定的OutputStream.只需使用它并通过其中一个HTTP响应即可.


更新:如果确实需要使用JSP,则可以像在Servlet类中那样在JSP中编写相同的代码.您几乎可以将其复制粘贴.仅确保您将任何模板文本写入流中,其中包括小脚本外部的换行符和空格.否则,它也会被写入二进制文件并损坏它.

如果有多个scriptlet块,则需要对其进行排列,以便在scriptlet的结束%>与下一个scriptlet的开始<%之间没有换行符.因此,例如

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

代替

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

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

这篇关于在JSP上从outputstream返回tiff文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆