简体   繁体   中英

How do I display an image on the browser using Java?

I have a web service which returns a BufferedImage value, but I am unable to display the image in my browser, knowing that I have already converted it from byte[] to BufferedImage .

I receive a result like following.

This is how I convert the byte[] data (I can see the data in byte[] ):

ResponseEntity<byte[]> result =  new RestTemplate(messageConverters).exchange(new URI(url), HttpMethod.GET, httpEntity,  byte[].class );
try {
    BufferedImage img = ImageIO.read(new ByteArrayInputStream(result.getBody()));
    return img;
} catch (IOException e) {
    e.printStackTrace();
}

And I get something like this:

{  
   "accelerationPriority":0.5,
   "colorModel":{  
      "transparency":3,
      "numComponents":4,
      "numColorComponents":3,
      "colorSpace":{  
         "type":5,
         "numComponents":3,
         "profile":{  
            "mediaWhitePoint":[  
               0.9504547,
               1.0,
               1.0890503
            ],
            "matrix":[  
               [  
                  0.43606567,
                  0.3851471,
                  0.1430664
               ],
               [  
                  0.2224884,
                  0.71687317,
                  0.06060791
               ],
               [  
                  0.013916016,
                  0.097076416,
                  0.71409607
               ]
            ],
            "data":"AAAMSGxjbXMCEAAAbW50clJHQiBYWVogB84AAgAJAAYAMQAAYWNzcE1TRlQAAAAASUVDIHNSR0IAAAAAAAAAAAAAAAAAAPbWAAEAAAAA0y1sY21zAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARY3BydAAAAVAAAAAzZGVzYwAAAYQAAABsd3RwdAAAAfAAAAAUYmtwdAAAAgQAAAAUclhZWgAAAhgAAAAUZ1hZWgAAAiwAAAAUYlhZWgAAAkAAAAAUZG1uZAAAAlQAAABwZG1kZAAAAsQAAACIdnVlZAAAA0wAAACGdmlldwAAA9Q...

If I understand correctly, the first byte[] you already have from the rest template is the full in memory representation of the source image. If this is the case, why involve a BufferedImage whatsoever? Can't you just return the byte[] directly from your controller and be sure to set an appropriate mime type in the http response header.

 @GetMapping("/")
    public ResponseEntity<byte[]> getImage() {
        final ResponseEntity<byte[]> result =  new RestTemplate(messageConverters).exchange(new URI(url), HttpMethod.GET, httpEntity,  byte[].class );
        return ResponseEntity.status(OK)
                .contentType(MediaType.IMAGE_JPEG)
                .body(result.getBody());
    }

On a separate note, if you are just proxying the image via your service back to the client there might be better solutions - such as spring cloud zuul or having the client directly access the resource. Might help to know a little about the image that you are fetching in terms of your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM