从 JSP 上的输出流返回 tiff 文件

发布于 2024-09-01 07:08:43 字数 503 浏览 2 评论 0原文

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

  1. 我收到一个 PDF,需要将其转换为 TIFF。
  2. 我以 File 对象和 OutputStream 的形式向“黑匣子”API 提供 PDF(我当前使用的是 ByteArrayOutputStream,但可以根据需要进行更改。
  3. “黑匣子”将 PDF 转换为 TIFF 并将结果保存到 问题
  4. 我使用 out.println(outputstream) 输出 TIFF

是我得到的是文本流而不是显示的图像:

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

但这并没有改变结尾。有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

上课铃就是安魂曲 2024-09-08 07:08:43

您不应该为此使用 JSP。它是一种视图技术,提供基于文本的模板来放入 HTML/CSS/JS 代码,并借助标签库(JSTL 等)和 EL(表达式语言,${}事物)。

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 在链接或浏览器地址栏中,甚至在 src 属性中元素。

doYourThingToConvertPdfFileToTiff 是您的“黑匣子”API,它似乎已经将 TIFF 写入给定的 OutputStream。只需利用它并传递 HTTP 响应即可。


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

如果您有多个 scriptlet 块,则需要对它们进行排列,以便在 scriptlet 的结尾 %> 和下一个 scriptlet 的起始 <% 之间没有换行符小脚本。因此,例如

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

代替

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

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" %>
<%
    //...
%>
是伱的 2024-09-08 07:08:43

这行不通。您需要对 image/tiff 的 http 响应的内容类型。

更多信息: http:// /www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Response-Headers.html

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

苍白女子 2024-09-08 07:08:43

元标记不是浏览器查看的内容。尝试使用 scriptlet 设置响应对象的内容类型。此页面有一些建议。

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.

猫卆 2024-09-08 07:08:43

如果您必须使用JSP:

您可以将输出流存储到具有随机生成名称的文件中,然后在JSP 中引用该文件。您需要确保输出目录位于 Web 服务器的路径中。

这也带来了它自己的问题:

  • 您需要以删除旧文件的方式管理文件系统(这样您的服务器磁盘就会填满)。
  • 您需要管理文件的同步(两个服务器线程不应更新同一文件)。
  • 哦,您必须确保一个用户生成的图像对另一个用户不可见。

我见过很多人做这种事情,我确信有一个图书馆。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文