【问题标题】:REST call returns png, how do i render this?REST 调用返回 png,我该如何渲染它?
【发布时间】:2014-07-30 14:04:25
【问题描述】:
我正在使用 RESTClient 测试 REST 端点,响应的内容类型是“image/png”。我将如何在 html 中呈现这种内容类型?我正在使用 java 和 Play!对于我的控制器,所以我有这一行:
Promise<WS.Response> imagePromise = WS.url(endpoint).get();
我使用纯 html/css 作为视图。谢谢。
【问题讨论】:
标签:
java
html
playframework
png
【解决方案1】:
您可以使用数据 URI 方案 (http://en.wikipedia.org/wiki/Data_URI_scheme) 来显示图像。例如,在您的 JSP/Servlet 代码中:
<%
Promise<WS.Response> imagePromise = WS.url(endpoint).get();
InputStream imageStream = // get image stream
String base64Image = // read the stream, and base 64 encode it (you can use Apache commons codec library to do this)
%>
<img src="data:image/png;base64,<%= base64Image %>" />