Example usage for org.apache.http.entity.mime.content StringBody StringBody

List of usage examples for org.apache.http.entity.mime.content StringBody StringBody

Introduction

In this page you can find the example usage for org.apache.http.entity.mime.content StringBody StringBody.

Prototype

public StringBody(final String text, Charset charset) throws UnsupportedEncodingException 

Source Link

Usage

From source file:com.lexmark.saperion.services.PutFileToSaperionECM.java

private static HttpEntity buildMultipart(String base64FileString, String fileName) throws IOException {
    String indexString = "indexName";
    StringBuilder jsonString = new StringBuilder();
    jsonString.append(setECMRequest(indexString, fileName));
    StringBody jsonBody = new StringBody(jsonString.toString(), ContentType.TEXT_PLAIN);

    FormBodyPart jsonBodyPart = new FormBodyPart("body", jsonBody);
    jsonBodyPart.addField("Content-Type", "application/json; charset=UTF-8");
    jsonBodyPart.addField("Content-ID", "body");

    StringBuilder fileBuilder = new StringBuilder();
    fileBuilder.append(base64FileString);
    StringBody fileBody = new StringBody(fileBuilder.toString(), ContentType.TEXT_PLAIN);

    FormBodyPart fileBodyPart = new FormBodyPart("filePart", fileBody);
    fileBodyPart.addField("Content-Type", "image/png");
    fileBodyPart.addField("Content-ID", "<imagefile>");

    MultipartEntityBuilder multipartBuilder = MultipartEntityBuilder.create();
    multipartBuilder.setMode(HttpMultipartMode.STRICT);
    multipartBuilder.setBoundary("2676ff6efebdb664f8f7ccb34f864e25");
    multipartBuilder.addPart(jsonBodyPart);
    multipartBuilder.addPart(fileBodyPart);

    /*ByteArrayOutputStream out = new ByteArrayOutputStream();
    multipartBuilder.build().writeTo(out);
    out.close();/* w w  w.  j a  v  a 2s .  co m*/
    String s = out.toString("UTF-8");
    System.err.println("output IS "+s);*/

    HttpEntity entity = multipartBuilder.build();
    return entity;

}