Package org.apache.http.entity.mime

Examples of org.apache.http.entity.mime.MultipartEntityBuilder


  public String getBody() {
    if (body != null) {
      return body;
    }
    if (!bodyParts.isEmpty()) {
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      for (BodyPart bodyPart : bodyParts) {
        bodyPart.addPart(builder);
      }
      return builder.build().toString();
    }
    return null;
  }
View Full Code Here


      this.contentParts = content;
    }
   
    @Override
    protected HttpEntity createEntity() throws ClientServicesException {
      MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
      for (ContentPart contentPart : contentParts) {
        if (contentPart.getData() instanceof InputStream) {
          entityBuilder.addBinaryBody(contentPart.getName(),
              (InputStream)contentPart.getData(),
              contentPart.getContentType(),
              contentPart.getFileName());
        }
        else if (contentPart.getData() instanceof byte[]) {
          entityBuilder.addBinaryBody(contentPart.getName(),
              (byte[])contentPart.getData(),
              contentPart.getContentType(),
              contentPart.getFileName());
        }
        else if (contentPart.getData() instanceof String) {
          entityBuilder.addTextBody(contentPart.getName(),
              (String)contentPart.getData(),
              contentPart.getContentType());
        }
      }
      return entityBuilder.build();
    }
View Full Code Here

   */
  public Result<PicUploadResult> uploadPic(String oAuthConsumerKey, String accessToken,
      String openid, String title, byte[] picture, String photoDesc, String albumId,
      Boolean mobile, Double lon, Double lat, Boolean needFeed, Integer successNum, Integer picNum) {
    HttpPost request = new HttpPost("https://graph.qq.com/photo/upload_pic");
    MultipartEntityBuilder builder =
        MultipartEntityBuilder.create().addBinaryBody("picture", picture,
            ContentType.create("image/" + title.substring(title.lastIndexOf(".") + 1)), title);
    builder.addTextBody("oauth_consumer_key", oAuthConsumerKey);
    builder.addTextBody("access_token", accessToken);
    builder.addTextBody("openid", openid);
    builder.addTextBody("format", "json");
    builder.addTextBody("title", title);
    if (photoDesc != null) {
      builder.addTextBody("photodesc", photoDesc);
    }
    if (albumId != null) {
      builder.addTextBody("albumid", albumId);
    }
    if (Boolean.TRUE.equals(mobile)) {
      builder.addTextBody("mobile", "1");
    }
    if (lon != null) {
      builder.addTextBody("x", lon.toString());
    }
    if (lat != null) {
      builder.addTextBody("y", lat.toString());
    }
    if (Boolean.FALSE.equals(needFeed)) {
      builder.addTextBody("needfeed", "0");
    }
    if (successNum != null) {
      builder.addTextBody("successnum", successNum.toString());
    }
    if (picNum != null) {
      builder.addTextBody("picnum", picNum.toString());
    }
    request.setEntity(builder.build());
    try {
      HttpResponse response = Http.CLIENT.execute(request);
      String json = IOUtils.toString(response.getEntity().getContent());
      return Result.parse(json, PicUploadResult.class);
    } catch (ClientProtocolException e) {
View Full Code Here

   */
  public Result<NewT> addPicT(String accessToken, String oauthConsumerKey, String openId,
      String content, byte[] pic, String clientIp, Double lon, Double lat, Boolean sync,
      Boolean compatible) {
    HttpPost request = new HttpPost("https://graph.qq.com/t/add_pic_t");
    MultipartEntityBuilder builder = MultipartEntityBuilder.create().addBinaryBody("pic", pic);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    addParameter(params, "access_token", accessToken);
    addParameter(params, "oauth_consumer_key", oauthConsumerKey);
    addParameter(params, "openid", openId);
    addParameter(params, "content", content);
    addNotNullParameter(params, "clientip", clientIp);
    addNotNullParameter(params, "lon", lon);
    addNotNullParameter(params, "lat", lat);
    if (sync != null) {
      addParameter(params, "syncflag", sync ? "0" : "1");
    }
    if (compatible != null) {
      addParameter(params, "compatibleflag", compatible ? "0" : "0x20");
    }
    addParameter(params, "format", "json");

    for (NameValuePair nameValuePair : params) {
      builder.addTextBody(nameValuePair.getName(), nameValuePair.getValue());
    }
    request.setEntity(builder.build());
    try {
      HttpResponse response = Http.CLIENT.execute(request);
      String json = IOUtils.toString(response.getEntity().getContent());
      JSONObject jsonObject = new JSONObject(json);
      Error error = Error.parse(jsonObject);
View Full Code Here

    return this;
  }
 
  public HttpEntity getEntity() {
    if (hasFile) {
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      for(String key: keyOrder) {
        Object value = parameters.get(key);
        if (value instanceof File) {
          builder.addPart(key, new FileBody((File) value));
        } else {
          builder.addPart(key, new StringBody(value.toString(), ContentType.create(ContentType.APPLICATION_FORM_URLENCODED.getMimeType(), Charset.forName(UTF_8))));
        }
      }
      return builder.build();
    } else {
      try {
        return new UrlEncodedFormEntity(MapUtil.getList(parameters), UTF_8);
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
View Full Code Here

    }
   
    @Test
    public void testMultipartPostWithParametersAndPayload() throws Exception {
        HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/multipart/123?query=abcd");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.STRICT);
        builder.addBinaryBody("part1", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        builder.addBinaryBody("part2", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        StringWriter sw = new StringWriter();
        jaxb.createMarshaller().marshal(new Customer(123, "Raul"), sw);
        builder.addTextBody("body", sw.toString(), ContentType.create("text/xml", Consts.UTF_8));
        post.setEntity(builder.build());
        HttpResponse response = httpclient.execute(post);
        assertEquals(200, response.getStatusLine().getStatusCode());
    }
View Full Code Here

    }
   
    @Test
    public void testMultipartPostWithoutParameters() throws Exception {
        HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/multipart");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.STRICT);
        builder.addBinaryBody("part1", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        builder.addBinaryBody("part2", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
        StringWriter sw = new StringWriter();
        jaxb.createMarshaller().marshal(new Customer(123, "Raul"), sw);
        builder.addTextBody("body", sw.toString(), ContentType.create("text/xml", Consts.UTF_8));
        post.setEntity(builder.build());
        HttpResponse response = httpclient.execute(post);
        assertEquals(200, response.getStatusLine().getStatusCode());
    }
View Full Code Here

  @Autowired
  WxConfig config;

  public WxBaseItemMediaEntity remoteMediaUpload(String accessToken,
      WxMediaTypeEnum type, byte[] content) throws WxException {
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
    String typeString = null;
    switch (type) {
    case IMAGE:
    case THUMB:
    case VIDEO:
    case VOICE:
      typeString = type.toString().toLowerCase();
      break;
    case MUSIC:
    case DEFAULT:
    case PIC_DESC:
      throw new WxException("Not supported upload type : "
          + type.toString());
    default:
      break;
    }

    Map<String, String> params = WxUtil.getAccessTokenParams(accessToken);
    System.out.println(typeString);
    params.put("type", typeString);
    ContentBody contentBody = new ByteArrayBody(content, ContentType.MULTIPART_FORM_DATA, "name.jpg");
    entityBuilder.addPart("media", contentBody);
    MediaResultMapper result = WxUtil.sendRequest(
        config.getMediaUploadUrl(), HttpMethod.POST, params,
        entityBuilder.build(), MediaResultMapper.class);

    WxBaseItemMediaEntity resultEntity = null;
    switch (type) {
    case IMAGE:
      WxItemImageEntity imageEntity = new WxItemImageEntity();
View Full Code Here

  private void testRecord(String handler, int statusCode) throws IOException {
    // To follow redirect: .setRedirectStrategy(new LaxRedirectStrategy())
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost("http://localhost:" + getServerPort()
        + "/kmf-content-api-test/" + handler);
    MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
        .create();
    multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    File file = new File("small");
    URL small = new URL(VideoURLs.map.get("small-webm"));
    FileUtils.copyURLToFile(small, file);
    FileBody fb = new FileBody(file);
    multipartEntity.addPart("file", fb);

    HttpEntity httpEntity = multipartEntity.build();
    post.setEntity(httpEntity);

    EntityUtils.consume(httpEntity);
    HttpResponse response = client.execute(post);
    final int responseStatusCode = response.getStatusLine().getStatusCode();
View Full Code Here

      HttpPost post = new HttpPost("http://localhost:9998/controller/router");

      InputStream is = getClass().getResourceAsStream("/UploadTestFile.txt");

      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      ContentBody cbFile = new InputStreamBody(is,
          ContentType.create("text/plain"), "UploadTestFile.txt");
      builder.addPart("fileUpload", cbFile);
      builder.addPart("extTID", new StringBody("2", ContentType.DEFAULT_TEXT));
      builder.addPart("extAction", new StringBody("fileUploadService",
          ContentType.DEFAULT_TEXT));
      builder.addPart("extMethod", new StringBody("uploadTest",
          ContentType.DEFAULT_TEXT));
      builder.addPart("extType", new StringBody("rpc", ContentType.DEFAULT_TEXT));
      builder.addPart("extUpload", new StringBody("true", ContentType.DEFAULT_TEXT));

      builder.addPart(
          "name",
          new StringBody("Jimöäü", ContentType.create("text/plain",
              Charset.forName("UTF-8"))));
      builder.addPart("firstName",
          new StringBody("Ralph", ContentType.DEFAULT_TEXT));
      builder.addPart("age", new StringBody("25", ContentType.DEFAULT_TEXT));
      builder.addPart("email", new StringBody("test@test.ch",
          ContentType.DEFAULT_TEXT));

      post.setEntity(builder.build());
      response = client.execute(post);
      HttpEntity resEntity = response.getEntity();

      assertThat(resEntity).isNotNull();
      String responseString = EntityUtils.toString(resEntity);
View Full Code Here

TOP

Related Classes of org.apache.http.entity.mime.MultipartEntityBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.