【发布时间】:2014-08-04 14:54:42
【问题描述】:
我使用 ajax 传递主键来获取图像。现在图像在字节数组和 bean 中。如何将此图像显示到 jsp 页面?建议我任何可以正常工作的方法。
【问题讨论】:
标签: javascript ajax spring jsp
我使用 ajax 传递主键来获取图像。现在图像在字节数组和 bean 中。如何将此图像显示到 jsp 页面?建议我任何可以正常工作的方法。
【问题讨论】:
标签: javascript ajax spring jsp
你可以使用data uri scheme
document.getElementById("imgId").src = "data:image/png;base64," + TheBase64Variable;
既然可以直接设置源,为什么还要使用 Ajax 调用?
document.getElementById("imgId").src = "foo.jsp?foo=bar";
【讨论】:
您可以尝试使用 Servlet。只需在响应的输出流中写入字节即可。
JSP:
<img src="${pageContext.servletContext.contextPath}/myServlet"/>
小服务程序:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
}
web.xml:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.x.y.z.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
您可以将图像名称作为查询字符串传递,并根据图像名称在响应的输出流中写入相应的字节。
【讨论】: