java 使用 servlet 将动态图像传递给 JSP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10564594/
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
Pass dynamic image to JSP with servlet
提问by cafman
I have a desktop app that creates a graphics 2D object, sticks it in a panel and draws it. I am trying to convert this app to a webpage using servlets and jsps. I have been reading online for 2 days and can't wrap my head around how to do this, each example I find seems to leave out an important piece. I don't want to save the image to file because each user will get a new/different image.
我有一个桌面应用程序,它创建一个图形 2D 对象,将其粘贴在面板中并绘制它。我正在尝试使用 servlet 和 jsps 将此应用程序转换为网页。我已经在网上阅读了 2 天,无法理解如何做到这一点,我发现的每个例子似乎都遗漏了一个重要的部分。我不想将图像保存到文件中,因为每个用户都会得到一个新的/不同的图像。
Can I create the image in the servlet along with the other response variables and pass them to the jsp at the same time? Then call the image using something like ${response.image}.This seems be preferable but doesn't seem to work.
我可以在 servlet 中创建图像以及其他响应变量并将它们同时传递给 jsp 吗?然后使用 ${response.image} 之类的东西调用图像。这似乎更可取,但似乎不起作用。
Or do I need a separate servlet just for the image (this seems harder)? Also how do I prepare the Graphics2D object to be used as an image in HTML?
或者我是否需要一个单独的 servlet 用于图像(这似乎更难)?另外,如何准备要用作 HTML 中的图像的 Graphics2D 对象?
回答by BalusC
You need to understand that it's the webbrowser who has got to download the invididual images based on the URLs of the <img>
elements found in the retrieved HTML code and that it's notthe webserver who has got to inline the image's raw content in the produced HTML code somehow.
您需要了解,网络浏览器必须根据在<img>
检索到的 HTML 代码中找到的元素的 URL 下载单个图像,而不是网络服务器必须以某种方式在生成的 HTML 代码中内联图像的原始内容.
You really need to create a standalone image servlet for this which listens on those specific URLs of the <img>
elements. You can make the servlet reuseable by providing an unique image idenfitier in the request query string or request path info during generating the HTML code.
您确实需要为此创建一个独立的图像 servlet,它侦听<img>
元素的那些特定 URL 。您可以通过在生成 HTML 代码期间在请求查询字符串或请求路径信息中提供唯一的图像标识符来使 servlet 可重用。
E.g.
例如
<img src="imageServlet?param1=value1¶m2=value2" />
with a
与
@WebServlet("/imageServlet")
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Create image based on request.getParameter() information.
// Set proper content type by response.setContentType().
// Write image to response.getOutputStream().
}
}
See also:
也可以看看:
- How to retrieve and display images from a database in a JSP page?- follows a similiar approach
- 如何在 JSP 页面中从数据库中检索和显示图像?- 遵循类似的方法
回答by JB Nizet
I'll just answer on the first part of the question. To embed an image into an HTML page, you first need to generate the HTML page which will contain the following markup:
我只回答问题的第一部分。要将图像嵌入 HTML 页面,您首先需要生成包含以下标记的 HTML 页面:
<img src="somePath" />
This HTML markup will be sent in the response to the request, and the browser will parse it. It will then send a second HTTP request to somePath
, to download the bytes of the image.
这个 HTML 标记将在对请求的响应中发送,浏览器将解析它。然后它将向 发送第二个 HTTP 请求somePath
,以下载图像的字节。
So, you need to somehow store the generated image in memory and wait for the second request, and then send the bytes to the response, or you need to delay the image generation until the second request comes in. I much prefer the second solution. So the goal of the code handling the first request will just be to generate markup containing an img
tag pointing to an appropriate URL. This URL should contain all the parameters necessary to actually generate the image.
因此,您需要以某种方式将生成的图像存储在内存中并等待第二个请求,然后将字节发送到响应,或者您需要延迟图像生成直到第二个请求进入。我更喜欢第二种解决方案。因此,处理第一个请求的代码的目标只是生成包含img
指向适当 URL 的标记的标记。此 URL 应包含实际生成图像所需的所有参数。
For the second part, you will certainly have to create a BufferedImage instance, draw to its Graphics2D object, and use ImageIO to write this BufferedImage to the response output stream.
对于第二部分,您当然必须创建一个 BufferedImage 实例,绘制到它的 Graphics2D 对象,并使用 ImageIO 将此 BufferedImage 写入响应输出流。