Java 帮助从 Servlet 获取图像到 JSP 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1154254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help getting image from Servlet to JSP page
提问by ChronoXIII
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 页面,以便它可以显示它。我试图避免保存图像,而是以某种方式将图像传输到 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.
自从我开始寻找如何将图像从 Servlet 传递到 JSP 以来,我还没有找到生成图像的方法,但卡住了。
EDIT: The jsp page is already made and isn't created by the servlet, i have to pass the image into an already existing jsp
编辑:jsp 页面已经创建并且不是由 servlet 创建的,我必须将图像传递到一个已经存在的 jsp
Any help is appreciated.
任何帮助表示赞赏。
采纳答案by Nick Holt
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);
Then in you JSP you just use a standard imgelement:
然后在您的 JSP 中,您只需使用一个标准img元素:
<img src="url to your servlet">
回答by cletus
If I understand your problem correctly, the sequence of events will be:
如果我正确理解您的问题,事件顺序将是:
- You generate an HTML page;
- That HTML page is sent to the client; and
- The client's browser reads the image URL and requests it as a separate request.
- 您生成一个 HTML 页面;
- 该 HTML 页面被发送到客户端;和
- 客户端的浏览器读取图像 URL 并将其作为单独的请求进行请求。
So, you can't generate the imageand pass it to the JSP. You can however generate a URLto get the image and put that in the JSP. That's easy enough to pass by the servlet putting it on the HttpServletRequest object (request scope in JSP). For example, generate:
因此,您无法生成图像并将其传递给 JSP。但是,您可以生成一个URL来获取图像并将其放入 JSP。这很容易通过 servlet 将其放在 HttpServletRequest 对象(JSP 中的请求范围)上。例如,生成:
<a href="http://myhost.com/image_servlet?id=1234"/>
You don't really say what that text is or what information is required to generate the image. If you can't encapsulate that in a GET URL, you may need to add extra information and put it in the HttpSession so it can be retrieved on the next get image request.
您并没有真正说明该文本是什么或生成图像需要哪些信息。如果您无法将其封装在 GET URL 中,则可能需要添加额外信息并将其放入 HttpSession 中,以便在下一次获取图像请求时可以检索到它。
回答by Brian Agnew
You can't1return both in the same response, since you're returning different types (an HTML page of type text/htmland an image of type image/jpeg, say).
您不能1在同一个响应中同时返回两者,因为您要返回不同的类型(比如说,一个 HTML 页面类型text/html和一个图像类型image/jpeg)。
For this sort of thing, I will generate the image during the initial servlet request (for the containing HTML page). I store it in a cache in my servlet, and write the HTML page with the image tag containing a URL to that image with the handle.
对于这类事情,我将在初始 servlet 请求(用于包含 HTML 页面)期间生成图像。我将它存储在我的 servlet 的缓存中,并用包含指向该图像的 URL 的图像标记编写 HTML 页面和句柄。
e.g. the browser asks for http://whatever/page
例如浏览器要求 http://whatever/page
The servlet generates the image, and writes an HTML tag in the page like
servlet 生成图像,并在页面中写入一个 HTML 标记,如
<img src="http://whatever/image/unique_handle_to_image">
The browser will render the HTML page, and as part of that issue a new request to my servlet with the handle for the image.
浏览器将呈现 HTML 页面,并作为该页面的一部分向我的 servlet 发出一个带有图像句柄的新请求。
e.g. the browser now asks for http://whatever/image/unique_handle_to_image
例如浏览器现在要求 http://whatever/image/unique_handle_to_image
I then return the image as content type image/jpegor similar.
然后我将图像作为内容类型image/jpeg或类似类型返回。
So you have two requests going on. One for the page, in which you render the image and store it temporarily, and the second in which you return the image. You have to remember to clear the image cache, but that's all straightforward. I wouldn't worry about storing lots of images, since the two requests from the browser usually (!) come in quick succession.
所以你有两个请求正在进行。一个用于页面,您在其中渲染图像并临时存储它,第二个用于返回图像。您必须记住清除图像缓存,但这很简单。我不会担心存储大量图像,因为来自浏览器的两个请求通常(!)快速连续出现。
I guess it's possible to use a data uriprovided your browser supports it, and create something like
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==" alt="Red dot" />
如果您的浏览器支持它,我想可以使用数据 uri,并创建类似的东西
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==" alt="Red dot" />
Note there are a number of caveats surrounding these. See the linked page.
请注意,围绕这些有许多警告。请参阅链接页面。
回答by Ula Krukar
I would do something along this lines to achieve this:
我会沿着这条线做一些事情来实现这一目标:
On the JSP page you put a link to an image:
在 JSP 页面上放置一个指向图像的链接:
<img src="servlet/path?word=value">the rest</img>
This link points to your servlet, it generates image using request parameters, you do not need to save it, just put it right into response's output stream. You have to remember to disable browser caching for this servlet.
此链接指向您的 servlet,它使用请求参数生成图像,您无需保存它,只需将其放入响应的输出流中即可。您必须记住禁用此 servlet 的浏览器缓存。
JSP page is displayed first, next all the images are requested, it should work just fine.
首先显示JSP页面,然后请求所有图像,它应该可以正常工作。
Of course, you should not put the text to display in a parameter like this, you should propably encipher it somehow or store it in a HTTP session.
当然,您不应该将要显示的文本放在这样的参数中,您应该以某种方式对其进行适当的加密或将其存储在 HTTP 会话中。
Hope this helps.
希望这可以帮助。
回答by tenen
...
response.setContentType("image/jpeg");
response.setContentLength(imageBytes.length); // imageBytes - image in bytes
response.getOutputStream().write(imageBytes);//
outStream.flush();
outStream.close();

