从 JSP 上的输出流返回 tiff 文件
我正在使用 JSP 显示单个 TIFF 文件。流程如下:
- 我收到一个 PDF,需要将其转换为 TIFF。
- 我以 File 对象和 OutputStream 的形式向“黑匣子”API 提供 PDF(我当前使用的是 ByteArrayOutputStream,但可以根据需要进行更改。
- “黑匣子”将 PDF 转换为 TIFF 并将结果保存到 问题
- 我使用 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:
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不应该为此使用 JSP。它是一种视图技术,提供基于文本的模板来放入 HTML/CSS/JS 代码,并借助标签库(JSTL 等)和 EL(表达式语言,
${}事物)。
TIFF 图像不是字符(文本)数据。这是一个二进制数据。为此,您确实需要使用 servlet。您不应使用 Writer 方法返回二进制数据。您应该为此使用
OutputStream
方法。否则,二进制数据将被损坏(这也是 JSP 中发生的情况,因为它在幕后使用Writer
)。下面是一个启动示例,您的 servlet 应该如下所示:
将此 servlet 映射到
/pdf2tiff
的url-pattern
上,以便您可以通过http 调用它: //example.com/contextname/pdf2tiff?filename=file.pdf
在链接或浏览器地址栏中,甚至在的
src
属性中元素。doYourThingToConvertPdfFileToTiff
是您的“黑匣子”API,它似乎已经将 TIFF 写入给定的OutputStream
。只需利用它并传递 HTTP 响应即可。更新:如果您确实需要为此使用 JSP,您可以在 JSP 中编写与在 Servlet 类中编写的代码相同的代码。您几乎可以复制粘贴它。仅确保您不向流中写入任何模板文本,这包括 scriptlet 外部的换行符和空格。否则它也会被写入二进制文件并损坏它。
如果您有多个 scriptlet 块,则需要对它们进行排列,以便在 scriptlet 的结尾
%>
和下一个 scriptlet 的起始<%
之间没有换行符小脚本。因此,例如代替
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 useOutputStream
methods for this. Otherwise the binary data would get corrupted (that's also what happens in a JSP since it under the hoods uses aWriter
).Here's a kickoff example how your servlet should look like:
Map this servlet on an
url-pattern
of for example/pdf2tiff
so that you can invoke it byhttp://example.com/contextname/pdf2tiff?filename=file.pdf
in links or browser address bar or even insrc
attribute of an<img>
element.The
doYourThingToConvertPdfFileToTiff
is your "black box" API which seems to already write the TIFF to the givenOutputStream
. 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.instead of
这行不通。您需要对 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
元标记不是浏览器查看的内容。尝试使用 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.
如果您必须使用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:
I've seen plenty of people do this kind of thing, I'm sure there is a library.