【问题标题】:how return array of image from rest web services如何从 REST Web 服务返回图像数组
【发布时间】:2014-05-10 17:02:37
【问题描述】:

我在android中很新鲜,, 想从其他网络服务返回图像数组,我发现了这个 @得到 @Produces("图片/jpg") 公共响应 getFullImage() 抛出 IOException { 文件[]myFile = 新文件[2];

myFile[0]=new File("C:\\Program Files\\test.jpg");
myFile[1]=new File("C:\\Program Files\\test2.jpg");
    BufferedImage image =ImageIO.read(myFile[0]) ;
    image =ImageIO.read(myFile[1]) ;

  ByteArrayOutputStream baos = new ByteArrayOutputStream();
   ImageIO.write(image, "png", baos);
    byte[] imageData = baos.toByteArray();  
  return Response.ok(new ByteArrayInputStream(imageData)).build();
}

但是如何将图像数组存储到缓冲区图像中,以便在客户端接收并在图像之间分离 谢谢大家

【问题讨论】:

  • 你贴的代码是什么?

标签: java android


【解决方案1】:

试试下面的代码:

        File file = new File("images.jpg");
        String fileName = file.getName();

        response = Response.ok((Object) file);
        response.header("Content-Disposition", "attachment; filename="+ fileName);

        return response.build();

【讨论】:

  • 能否请您添加一些描述?
  • 问题是关于传递图像的“数组”。没有一张图片。请正确阅读。
【解决方案2】:
@GET
    @Consumes({ MediaType.TEXT_PLAIN, MediaType.TEXT_HTML, MediaType.APPLICATION_JSON })
    @Produces("text/json")
    public Response retrieveClientImage(@PathParam("entityId") final Long userId, @QueryParam("maxWidth") final Integer maxWidth,
            @QueryParam("maxHeight") final Integer maxHeight, @QueryParam("output") final String output) {
        Collection<AdImageData> imageDataResults = this.imageReadPlatformService.retrieveAdImages(userId);
        // TODO: Need a better way of determining image type
        List<String> newfiles = new ArrayList<>();
        String imageDataURISuffix = ContentRepositoryUtils.IMAGE_DATA_URI_SUFFIX.JPEG.getValue();
        for (AdImageData adImageData : imageDataResults) {
            if (StringUtils.endsWith(adImageData.location(), ContentRepositoryUtils.IMAGE_FILE_EXTENSION.GIF.getValue())) {
                imageDataURISuffix = ContentRepositoryUtils.IMAGE_DATA_URI_SUFFIX.GIF.getValue();
            } else if (StringUtils.endsWith(adImageData.location(), ContentRepositoryUtils.IMAGE_FILE_EXTENSION.PNG.getValue())) {
                imageDataURISuffix = ContentRepositoryUtils.IMAGE_DATA_URI_SUFFIX.PNG.getValue();
            }
            final String clientImageAsBase64Text = imageDataURISuffix + Base64.encodeBytes(adImageData.getContentOfSize(null, null));
            newfiles.add(clientImageAsBase64Text);

        }

        return Response.status(Status.OK).entity(new Gson().toJson(newfiles)).type("text/json").build();
    }

在这个示例方法中返回图像数据的响应数组,在浏览器中您可以迭代并获取迭代中的每个对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    相关资源
    最近更新 更多