Image contentUrl to byte array

cancel
Showing results for 
Search instead for 
Did you mean: 
laurav
Active Member

Image contentUrl to byte array

Hi,

I'm using activiti 6.0 rest api and trying to save a process .png image as BLOB in my database.

I run a rest get call to the image contentUrl (http://localhost:8080/activiti-rest/service/repository/deployments/1234/resourcedata/image.png) present in my process resources:

restResponse = rest.get(result[1]['contentUrl']){
    auth username,password
}

How do I convert this response to a byte[ ]?

thanks for your help

3 Replies
daisuke-yoshimo
Senior Member

Re: Image contentUrl to byte array

What's rest client library do you use in the following code?

Java data type that you will receive depends on the implementation on the client side(Rest client library).

restResponse = rest.get(result[1]['contentUrl']){
    auth username,password
}

If now you receive it as InputStream, you can convert it to byte[] by the method of the following article.

bytearray - Convert InputStream to byte array in Java - Stack Overflow 

laurav
Active Member

Re: Image contentUrl to byte array

Sorry for not mentioning this...I am using grails : 

import grails.plugins.rest.client.RestBuilder
import grails.plugins.rest.client.RestResponse

RestBuilder rest = new RestBuilder()
RestResponse restResponse = rest.get(result[1]['contentUrl']){
    auth username,password
}
byte[] png_image = IOUtils.copyToByteArray(restResponse)

And am getting this error:

No signature of method: static grails.io.IOUtils.copyToByteArray() is applicable for argument types: (grails.plugins.rest.client.RestResponse) values: [grails.plugins.rest.client.RestResponse@27a210e7]
Possible solutions: copyToByteArray(java.io.File), copyToByteArray(java.io.InputStream). 

daisuke-yoshimo
Senior Member

Re: Image contentUrl to byte array

How about the following code?

I'm sorry that I don't know about groovy and grails.

I tried to write the example code by using the following sites.

RestResponse (grails-datastore-rest-client 1.0.0.BUILD-SNAPSHOT API) 

ResponseEntity (Spring Framework 5.0.2.RELEASE API) 

HttpEntity (Spring Framework 5.0.2.RELEASE API) 

import grails.plugins.rest.client.RestBuilder
import grails.plugins.rest.client.RestResponse

RestBuilder rest = new RestBuilder()
RestResponse restResponse = rest.get(result[1]['contentUrl']){
    auth username,password
}
byte[] png_image = restResponse.responseEntity.getBody().getBytes()