[Progress Communities] [Progress OpenEdge ABL] Forum Post: RE: How to upload a image with REST API

Status
Not open for further replies.
S

Srinivas Panyala

Guest
In above reply, syntax highlighters modified a few things. Please check below example. import java.io.File; import java.io.FileInputStream; import org.apache.commons.codec.binary.Base64; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; public class TestSetBinaryData { public static void main(String[] args) { // Apache HTTP client 3.1 PostMethod post = new PostMethod(); HttpClient httpclient = new HttpClient(); String postResponse = null; try { post = new PostMethod( " www.rollbase.com/.../setBinaryData // Multi-part upload Part[] p1 = new Part[6]; p1[0] = new StringPart("id", "97062227"); // record id p1[1] = new StringPart("objName", "ObjectA"); // object integration name p1[2] = new StringPart("fieldName", "TestFileUpload"); // field integration name p1[3] = new StringPart("contentType", "application/pdf"); // file content type p1[4] = new StringPart("fileName", "FileName.pdf"); // file name File f = new File("C:\\BinaryFileTest\\FileName.pdf"); // actual file FileInputStream fileInputStream = null; byte[] bFile = new byte[(int) f.length()]; try { fileInputStream = new FileInputStream(f); fileInputStream.read(bFile); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } String encodeBase64Str = Base64.encodeBase64String(bFile); // Convert to Base64 encoded format System.out.println("length is " + encodeBase64Str.length()); p1[5] = new StringPart("value", encodeBase64Str); post.setRequestEntity(new MultipartRequestEntity(p1, post.getParams())); httpclient.executeMethod(post); postResponse = post.getResponseBodyAsString(); System.out.println("Response is" + postResponse); } catch (Exception e) { e.printStackTrace(); } finally { post.releaseConnection(); } } } Thanks Srinivas

Continue reading...
 
Status
Not open for further replies.
Top