是否可以创建 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();
}
}
这行得通吗?我是否必须处理正确的 header (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();
}
关于java - 从 JAX-RS servlet 动态创建图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5736973/