如何从 Jersey REST 服务方法返回 PNG 图像到浏览器
我有一个运行 Jersey REST 资源的 Web 服务器,我想知道如何获取浏览器 img 标签的图像/png 参考;提交表单或收到 Ajax 响应后。用于添加图形的图像处理代码正在工作,只需要以某种方式返回它。
代码:
@POST
@Path("{fullsize}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("image/png")
// Would need to replace void
public void getFullImage(@FormDataParam("photo") InputStream imageIS,
@FormDataParam("submit") String extra) {
BufferedImage image = ImageIO.read(imageIS);
// .... image processing
//.... image processing
return ImageIO. .. ?
}
干杯
I have a web server running with Jersey REST resources up and I wonder how to get an image/png reference for the browsers img tag; after submitting a Form or getting an Ajax response. The image processing code for adding graphics is working, just need to return it somehow.
Code:
@POST
@Path("{fullsize}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("image/png")
// Would need to replace void
public void getFullImage(@FormDataParam("photo") InputStream imageIS,
@FormDataParam("submit") String extra) {
BufferedImage image = ImageIO.read(imageIS);
// .... image processing
//.... image processing
return ImageIO. .. ?
}
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不相信在 REST 服务中返回图像数据是个好主意。它会占用应用程序服务器的内存和 IO 带宽。最好将该任务委托给针对此类传输进行了优化的适当 Web 服务器。您可以通过发送到图像资源的重定向(作为带有图像 URI 的 HTTP 302 响应)来实现此目的。当然,这假设您的图像被排列为网页内容。
话虽如此,如果您决定确实需要从 Web 服务传输图像数据,您可以使用以下(伪)代码来实现:
添加异常处理等。
I'm not convinced its a good idea to return image data in a REST service. It ties up your application server's memory and IO bandwidth. Much better to delegate that task to a proper web server that is optimized for this kind of transfer. You can accomplish this by sending a redirect to the image resource (as a HTTP 302 response with the URI of the image). This assumes of course that your images are arranged as web content.
Having said that, if you decide you really need to transfer image data from a web service you can do so with the following (pseudo) code:
Add in exception handling, etc etc.
我为此构建了一个具有以下功能的通用方法:
这里是代码:
注意区域设置切换似乎不是线程安全的。我认为,最好在全球范围内切换区域设置。不过我不确定副作用...
I built a general method for that with following features:
Here the code:
Note that the Locale switching does not seem to be thread-safe. I think, it's better to switch the locale globally. I am not sure about the side-effects though...
关于@Perception的回答,使用字节数组时确实非常消耗内存,但您也可以简单地写回输出流
in regard of answer from @Perception, its true to be very memory-consuming when working with byte arrays, but you could also simply write back into the outputstream
如果您有多种图像资源方法,则非常值得创建一个
MessageBodyWriter
来输出BufferedImage
:如果满足以下条件,将自动使用此
MessageBodyWriter
为 Jersey 启用自动发现,否则需要由自定义应用程序子类返回。有关详细信息,请参阅 JAX-RS 实体提供程序。设置完成后,只需从资源方法返回
BufferedImage
,它将作为图像文件数据输出:这种方法的几个优点:
OutputSteam< /code> 而不是中介
BufferedOutputStream
png
和jpg
输出(取决于资源方法允许的媒体类型)If you have a number of image resource methods, it is well worth creating a
MessageBodyWriter
to output theBufferedImage
:This
MessageBodyWriter
will be used automatically if auto-discovery is enabled for Jersey, otherwise it needs to be returned by a custom Application sub-class. See JAX-RS Entity Providers for more info.Once this is set up, simply return a
BufferedImage
from a resource method and it will be be output as image file data:A couple of advantages to this approach:
OutputSteam
rather than an intermediaryBufferedOutputStream
png
andjpg
output (depending on the media types allowed by the resource method)