从JAX-RS servlet动态创建映像 [英] Dynamically create image from JAX-RS servlet

查看:170
本文介绍了从JAX-RS servlet动态创建映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to create a PNG image and output it straight to the browser as part of a JAX-RS resource?

这样的事情:

@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 注释完成?可以将图像输出为响应?我可以从流中构建响应吗?

Would this work? Do I have to take care of the correct headers (Content-Type), or is this done by the @Produces annotation? Can output an image as a Response? Can I build a Response from a stream?

推荐答案

看一看在 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();
 }

这篇关于从JAX-RS servlet动态创建映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆