简体   繁体   中英

Extract an image from an object JSON in JAVA

i'm trying to send an image that i took from a database and i would like to send that image, using a WebApi written in PHP with JSON to a Java program; the following lines are an extract from PHP program:

$img = file_get_contents($stringPath);
$array = ["immagine" => base64_encode($img)];
echo json_encode($array);

in the following JAVA program i use restlet framework:

ClientResource resource = new ClientResource("link to the webApi");``
String retur = resource.get().getText();
JSONObject obj = new JSONObject(retur);

How can i extract from the JSON object the image that i sent from the PHP progra

If i understand your question correct you just have to do the reverse step like you are doing in your php code.

byte[] content = Base64.decodeBase64(obj.getString("immagine"));
ByteArrayInputStream bais = new ByteArrayInputStream(content);
BufferedImage image = ImageIO.read(bais);

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