开发者

REST WebService to serve images dynamically?

开发者 https://www.devze.com 2023-01-17 12:14 出处:网络
Consider 3 Jpeg files image1.jpg image2.jpg image3.jpg For a given URL and a set of parameters, I would like the s开发者_如何学Cerver to select and return one of those images.

Consider 3 Jpeg files

  • image1.jpg
  • image2.jpg
  • image3.jpg

For a given URL and a set of parameters, I would like the s开发者_如何学Cerver to select and return one of those images.

I'm working in a JEE6 environment. What approach would you recommend?

  • A JSF redirect?
  • A REST WebService?
  • A good old servlet?
  • ... ?

Any suggestions welcome!


This is what I brewed so far:

import org.apache.commons.io.IOUtils;

@Path("/item")
public class MyResource {

  @GET
  @Path("/object/{id}")
  @Produces("image/jpeg")
  public byte[] getImageRepresentation(@PathParam("id") int id) {
     byte[] bytes = null;
     switch (id) {
        case 1: bytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/img/image01.jpg"));break;
        case 2: bytes = IOUtils.toByteArray(this.getClass().getResourceAsStream("/img/image02.jpg"));
      }
      return bytes;
  }

}

Still curious about alternative approaches! Thank you! J. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号