如何使用CXF RSET service return图片
我前两天遇到一个棘手的问题,需求是要求在浏览器上面直接用GET方法调用我的service程序可以看到图片。如http://xxxx.xxx/read/image/4_pic.jpg
所以这就要求我在service的返回值上面要做成一个return InputStream..于是我上网搜索,看到http://www.romanlogic.com/rest-service-for-images-using-apache-cxf/ 这篇文章是讲如何返回image的,所以写下了如下的代码
@GET@Path("/{name}")@Produces("image/jpg")public InputStream readImage(@PathParam("name") String name ){log.debug("Try to load image [name:"+name+" ]from DB");log.debug("begin transaction");imageDAO.beginTransaction();byte[] b;InputStream is = null;try {b = imageProcessService.loadImage(name);log.debug("commit transaction");imageDAO.commitTransaction();is = new ByteArrayInputStream(b);return is;} catch (Throwable e) {log.warn(e);log.debug("rollback transaction");imageDAO.rollbackTransaction();return null;} }