0

I have a rest api service which will return image as response.

Below my code

@RequestMapping(value = "/sid/{id}", method = RequestMethod.GET,
 produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity < InputStreamResource > getImage(@PathVariable Integer id) throws IOException {
 ClassPathResource imgFile = null;
 if (id == 1) {

  imgFile = new ClassPathResource("image/sid1.jpg");
 }
 if (id == 2) {

  imgFile = new ClassPathResource("image/sid2.jpg");
 }

 return ResponseEntity
  .ok()
  .contentType(MediaType.IMAGE_JPEG)
  .body(new InputStreamResource(imgFile.getInputStream()));
}

I need to write a client code which will consume the above rest api to get the image, convert into blob and insert in DB.

my client code

public void getEntity(){
    System.out.println("Begin /GET request!");
    String getUrl = "http://localhost:8080/sid/1";
    ResponseEntity<InputStreamResource> getResponse = restTemplate.getForEntity(getUrl, InputStreamResource.class);

    if(getResponse.getBody() != null){
        ///stuck here
    }else{
        System.out.println("Response for Get Request: NULL");
    }
}

I'm not sure on how to proceed with the client code as need to read the image.

Any leads will be great

Thanks in advance

Update:

When i try to convert the inputstream to byte array using below code i get exception

public void getEntity() throws IllegalStateException, IOException{
    System.out.println("Begin /GET request!");
    String getUrl = "http://localhost:8080/sid/1";
    ResponseEntity<InputStreamResource> getResponse = restTemplate.getForEntity(getUrl, InputStreamResource.class);
    if(getResponse.getBody() != null){
        InputStream is=getResponse.getBody().getInputStream();
        System.out.println("Begin /GET request!"+is);
        byte[] bytes = IOUtils.toByteArray(is);
    }else{
        System.out.println("Response for Get Request: NULL");
    }
}

My Exception

Exception in thread "main" java.io.IOException: stream is closed
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.ensureOpen(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at java.io.PushbackInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2146)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
at org.apache.commons.io.IOUtils.toByteArray(IOUtils.java:721)

Any help to resolve the exception will be appreciated

user3492471
  • 93
  • 2
  • 16
  • Please show what you have done so far on the client side. Which http client library do you intend to use? Whatever framework, they will all be capabable of consuming binary data, so this is your call. ;) – Mick Dec 11 '18 at 09:33
  • @Mick added my attempt in client code – user3492471 Dec 11 '18 at 09:37
  • getResponse.getBody().getInputStream() ? – Mick Dec 11 '18 at 09:43
  • You updated your question with code but I can't see you converting the image to blob and inserting it into the database. – Zun Dec 11 '18 at 09:43
  • @Mick if i get as stream using getResponse.getBody().getInputStream() how to convert into blob ? – user3492471 Dec 11 '18 at 09:48
  • sorry, but you need to be more specific. What do you need in terms of JAVA API? Do you need a byte array for constructing your blob? – Mick Dec 11 '18 at 10:24
  • @Mick based on my snippet, how to get the image in client code from Rest API. Basically to read the image and insert that image in DB as blob from my rest template client consumer code – user3492471 Dec 11 '18 at 14:12
  • what DB? what library do you use to access the DB? would it be enough for you to know how to create a byte array from an input stream? – Mick Dec 12 '18 at 06:26
  • @Mick please check my update above – user3492471 Dec 12 '18 at 09:14
  • Your next step might be this https://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java – Mick Dec 12 '18 at 11:55

0 Answers0