从JAX-RS servlet动态创建映像

是否可以创建PNG图像并将其作为JAX-RS资源的一部分直接输出到浏览器?

像这样的东西:

@Path("img/{externalId}") @Stateless @Produces({"image/png"}) public class MyImgResource { @GET public Response (@PathParam("externalId") String externalId) { // create image, write to buffered output stream return Response.ok().entity(stream).build(); } } 

这会有用吗? 我是否必须处理正确的标题(Content-Type),还是由@Produces注释完成? 可以输出图像作为Response吗? 我可以从流构建Response吗?

看看http://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e323 :

  @GET @Path("/images/{image}") @Produces("image/*") public Response getImage(@PathParam("image") String image) { File f = new File(image); if (!f.exists()) { throw new WebApplicationException(404); } String mt = new MimetypesFileTypeMap().getContentType(f); return Response.ok(f, mt).build(); }