帮助从 Servlet 获取图像到 JSP 页面 [英] Help getting image from Servlet to JSP page
问题描述
我目前必须生成一个显示字符串文本的图像,我需要在 Servlet 上制作这个图像,然后以某种方式将图像传递给 JSP 页面,以便它可以显示它.我试图避免保存图像,而是以某种方式将图像流式传输到 JSP.
I currently have to generate an image that displays the text of a string, i need to make this image on a Servlet and then somehow pass the image to a JSP page so that it can display it. I'm trying to avoid saving the image, and instead somehow stream the image to the JSP.
自从我开始寻找如何将图像从 Servlet 传递到 JSP 之后,我还没有找到生成图像的方法,但卡住了.
I haven't found a way of generating the image since i started with finding how to pass an image from the Servlet to the JSP adn got stuck.
jsp 页面已经创建,不是由 servlet 创建的,我必须将图像传递到已经存在的 jsp 中
The jsp page is already made and isn't created by the servlet, i have to pass the image into an already existing jsp
感谢任何帮助.
推荐答案
您需要将图像作为字节数组写入响应的输出流.像这样:
You need to write the image as a byte array to the response's output stream. Something like this:
byte[] imageBytes = getImageAsBytes();
response.setContentType("image/jpeg");
response.setContentLength(imageBytes.length);
response.getOutputStream().write(imageBytes);
然后在您的 JSP 中您只需使用标准的 img
元素:
Then in you JSP you just use a standard img
element:
<img src="url to your servlet">
这篇关于帮助从 Servlet 获取图像到 JSP 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!