Package org.apache.http.entity.mime

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


                            reqBuilder.setEntity(e);
                        }
                        else if(bean instanceof ReqEntityMultipart) {
                            ReqEntityMultipart multipart = (ReqEntityMultipart)bean;
                           
                            MultipartEntityBuilder meb = MultipartEntityBuilder.create();
                           
                            // Format:
                            MultipartMode mpMode = multipart.getMode();
                            switch(mpMode) {
                                case BROWSER_COMPATIBLE:
                                    meb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
                                    break;
                                case RFC_6532:
                                    meb.setMode(HttpMultipartMode.RFC6532);
                                    break;
                                case STRICT:
                                    meb.setMode(HttpMultipartMode.STRICT);
                                    break;
                            }
                           
                            // Parts:
                            for(ReqEntityPart part: multipart.getBody()) {
                                if(part instanceof ReqEntityStringPart) {
                                    ReqEntityStringPart p = (ReqEntityStringPart)part;
                                    String body = p.getPart();
                                    ContentType ct = p.getContentType();
                                    final StringBody sb;
                                    if(ct != null) {
                                        sb = new StringBody(body, HTTPClientUtil.getContentType(ct));
                                    }
                                    else {
                                        sb = new StringBody(body, org.apache.http.entity.ContentType.DEFAULT_TEXT);
                                    }
                                    meb.addPart(part.getName(), sb);
                                }
                                else if(part instanceof ReqEntityFilePart) {
                                    ReqEntityFilePart p = (ReqEntityFilePart)part;
                                    File body = p.getPart();
                                    ContentType ct = p.getContentType();
                                    final FileBody fb;
                                    if(ct != null) {
                                        fb = new FileBody(body, HTTPClientUtil.getContentType(ct), p.getFilename());
                                    }
                                    else {
                                        fb = new FileBody(body, org.apache.http.entity.ContentType.DEFAULT_BINARY, p.getFilename());
                                    }
                                    meb.addPart(p.getName(), fb);
                                }
                            }
                           
                            reqBuilder.setEntity(meb.build());
                        }
                       
                       
                    }
                    catch (UnsupportedEncodingException ex) {
View Full Code Here


                new StringResponseHandler());
    }

    private HttpEntity createHttpEntity(String name, URL archiveUrl) throws OpenESBClientException {
        try {
            MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
            multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            multipartEntity.addPart(name, new FileBody(new File(archiveUrl.toURI())));

            return multipartEntity.build();
        } catch (URISyntaxException ex) {
            logger.log(Level.SEVERE, "Unable to create an HTTP entity to upload file : " + archiveUrl, ex);
            throw new OpenESBClientException("Unable to create an HTTP entity to upload file : " + archiveUrl, ex);
        }
    }
View Full Code Here

    public String uploadFile(String uri, String partName, String filename, String mimeType, byte[] contents, int expectedResponseCode) throws URISyntaxException, IOException {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpPost post = new HttpPost(uri);
            ByteArrayBody contentBody = new ByteArrayBody(contents, ContentType.create(mimeType), filename);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addPart(partName, contentBody);
            post.setEntity(builder.build());
            HttpResponse response = client.execute(post);
            String result = EntityUtils.toString(response.getEntity());
            int statusCode = response.getStatusLine().getStatusCode();
            Assert.assertEquals(String.format("Invalid response code, %s", statusCode), expectedResponseCode, statusCode);
            return result;
View Full Code Here

    try {
      HttpPost post = new HttpPost("http://localhost:9998/controller/router");
      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("fileUploadController",
          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

   */
  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

public class HttpMultipartHelper {

  public static HttpEntity getMultiPartEntity(String fileName, String contentType, InputStream fileStream,
      Map<String, String> additionalFormFields) throws IOException {
   
    MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
   
    if (additionalFormFields != null && !additionalFormFields.isEmpty()) {
      for (Entry<String, String> field : additionalFormFields.entrySet()) {
        entityBuilder.addTextBody(field.getKey(), field.getValue());
      }
    }
   
    entityBuilder.addBinaryBody(fileName, IOUtils.toByteArray(fileStream), ContentType.create(contentType), fileName);
   
    return entityBuilder.build();
  }
View Full Code Here

        final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + TEXTFILENAME);
        final File file = new File(url.getPath());
        final FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
        final StringBody stringBody1 = new StringBody("This is message 1", ContentType.MULTIPART_FORM_DATA);
        final StringBody stringBody2 = new StringBody("This is message 2", ContentType.MULTIPART_FORM_DATA);
        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addPart("upfile", fileBody);
        builder.addPart("text1", stringBody1);
        builder.addPart("text2", stringBody2);
        final HttpEntity entity = builder.build();
        post.setEntity(entity);
        response = client.execute(post);
        final int statusCode = response.getStatusLine().getStatusCode();
        final String responseString = getContent();
        final String contentTypeInHeader = getContentTypeHeader();
View Full Code Here

    @Test
    public final void givenFileandTextPart_whenUploadwithAddBinaryBodyandAddTextBody_ThenNoExeption() throws ClientProtocolException, IOException {
        final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + TEXTFILENAME);
        final File file = new File(url.getPath());
        final String message = "This is a multipart post";
        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, TEXTFILENAME);
        builder.addTextBody("text", message, ContentType.DEFAULT_BINARY);
        final HttpEntity entity = builder.build();
        post.setEntity(entity);
        response = client.execute(post);
        final int statusCode = response.getStatusLine().getStatusCode();
        final String responseString = getContent();
        final String contentTypeInHeader = getContentTypeHeader();
View Full Code Here

        final URL url = Thread.currentThread().getContextClassLoader().getResource("uploads/" + ZIPFILENAME);
        final URL url2 = Thread.currentThread().getContextClassLoader().getResource("uploads/" + IMAGEFILENAME);
        final InputStream inputStream = new FileInputStream(url.getPath());
        final File file = new File(url2.getPath());
        final String message = "This is a multipart post";
        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addBinaryBody("upfile", file, ContentType.DEFAULT_BINARY, IMAGEFILENAME);
        builder.addBinaryBody("upstream", inputStream, ContentType.create("application/zip"), ZIPFILENAME);
        builder.addTextBody("text", message, ContentType.TEXT_PLAIN);
        final HttpEntity entity = builder.build();
        post.setEntity(entity);
        response = client.execute(post);
        final int statusCode = response.getStatusLine().getStatusCode();
        final String responseString = getContent();
        final String contentTypeInHeader = getContentTypeHeader();
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.