【发布时间】:2015-09-27 18:31:25
【问题描述】:
我有一个要求,我必须在浏览器上呈现图像。给定应用程序的 url 应该在浏览器中显示图像(不下载图像)。 URL 类似于http://localhost:8080/image/render。任何已经编写的代码或任何想法都将不胜感激。
【问题讨论】:
标签: java url servlets web-applications
我有一个要求,我必须在浏览器上呈现图像。给定应用程序的 url 应该在浏览器中显示图像(不下载图像)。 URL 类似于http://localhost:8080/image/render。任何已经编写的代码或任何想法都将不胜感激。
【问题讨论】:
标签: java url servlets web-applications
把它放到你的 HTML 页面中。
<img src="http://site.domain/yourimage.extension"/>
【讨论】:
这样的事情对我有用。 在您的 servlet 中:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ByteArrayOutputStream bout = // write your image on the stream; you can use request.getParameter(...) and so on to have it specialize
bout.close();
response.setContentType("png");
response.setContentLength(bout.size());
response.getOutputStream().write(bout.toByteArray());
response.getOutputStream().flush();
}
【讨论】: