For Question #1 I tried the following approach and it worked for me.
Create a REST Service. First get the Binary Image data into a Blob and pass it as Base64 encoded String as service response.
Now you can consume the Webservice from any client including Java to get the Base64 String. Decode the Base64 String to get back the Image. In Java you can use FileOutputStream
to write the Image file to your disk.
For testing the Base64 String I used this online tool - Convert Your Base64 To Image
Rest Service
@RestResource(urlMapping='/ImageService')
global with sharing class RESTImageController {
@HttpGet
global static String getImage(){
String strBase64 = 'Hello';
Book__c book = [SELECT Rich_Text__c FROM Book__c WHERE Id = 'a01i000000dwOIb' LIMIT 1];
String bookimg = book.Rich_Text__c.substringBetween('<img', 'img>');
String imgsrc = bookimg.substringBetween('src="', '"');
imgsrc = imgsrc.replace('amp;', '');
PageReference page = new PageReference(imgsrc);
Blob imgblob = page.getContent();
strBase64 = EncodingUtil.base64Encode(imgblob);
return strBase64;
}
}
For Question #2 Please refer to this link, first you have to encode the image to a Base64 String, create img element <img alt="<image_Name>" src="data:image/<image_File_Extension>;base64, <base64_Image_String>"></img>
and copy the entire source text data and use it as the value in the relevant field of the API.
Extracts from the link.
Uploading Images Using the API
If records include rich text area fields containing images, you must convert the image to Base64 and supply that as the value in the relevant field. You can use a text editor to do this.
Solution
1 - Encode the desired image into Base64.
2 - Copy the entire Base64 image string to the clipboard.
3- Copy the following text and paste it into a text editor:
<img alt="<image_Name>" src="data:image/<image_File_Extension>;base64, <base64_Image_String>"></img>
Replace with the file name of the image. Don’t include the file extension. For example: Headshot1
Replace with the extension of the image file. Don’t include the dot. For example: jpg
Replace with the string that you copied in Step 2.
4 - Copy the entire source text data and use it as the value in the relevant field of the API.
5 - Upload the data.