Example usage for org.apache.http.entity.mime MultipartEntityBuilder addPart

List of usage examples for org.apache.http.entity.mime MultipartEntityBuilder addPart

Introduction

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

Prototype

public MultipartEntityBuilder addPart(final String name, final ContentBody contentBody) 

Source Link

Usage

From source file:org.openstreetmap.josm.plugins.mapillary.oauth.UploadUtils.java

/**
 * @param file File that is going to be uploaded
 * @param hash Information attached to the upload
 * @throws IllegalArgumentException if the hash doesn't contain all the needed keys.
 *//*from  w  ww.ja v  a 2 s.  c  o  m*/
private static void uploadFile(File file, Map<String, String> hash) throws IOException {
    HttpClientBuilder builder = HttpClientBuilder.create();
    HttpPost httpPost = new HttpPost(UPLOAD_URL);

    try (CloseableHttpClient httpClient = builder.build()) {
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        for (String key : keys) {
            if (hash.get(key) == null)
                throw new IllegalArgumentException();
            entityBuilder.addPart(key, new StringBody(hash.get(key), ContentType.TEXT_PLAIN));
        }
        entityBuilder.addPart("file", new FileBody(file));
        HttpEntity entity = entityBuilder.build();
        httpPost.setEntity(entity);
        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            if (response.getStatusLine().toString().contains("204")) {
                PluginState.imageUploaded();
                Main.info(PluginState.getUploadString() + " (Mapillary)");
            } else {
                Main.info("Upload error");
            }
        }
    }
    if (!file.delete()) {
        Main.error("MapillaryPlugin: File could not be deleted during upload");
    }
    MapillaryUtils.updateHelpText();
}

From source file:org.urbanstew.soundcloudapi.SoundCloudAPI.java

/**
  * Uploads an arbitrary body by performing a POST request on the "tracks" resource.
 * @throws OAuthExpectationFailedException 
 * @throws OAuthMessageSignerException /*ww  w . jav  a 2  s.c om*/
 * @throws IOException 
 * @throws ClientProtocolException 
 * @throws OAuthCommunicationException 
  */
public HttpResponse upload(ContentBody fileBody, List<NameValuePair> params) throws OAuthMessageSignerException,
        OAuthExpectationFailedException, ClientProtocolException, IOException, OAuthCommunicationException {
    HttpPost post = new HttpPost(urlEncode("tracks", null));
    // fix contributed by Bjorn Roche
    post.getParams().setBooleanParameter("http.protocol.expect-continue", false);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    //MultipartEntity entity = new  MultipartEntity();
    for (NameValuePair pair : params) {
        try {
            builder.addPart(pair.getName(), new StringBodyNoHeaders(pair.getValue()));
        } catch (UnsupportedEncodingException e) {
        }
    }
    builder.addPart("track[asset_data]", fileBody);
    HttpEntity entity = builder.build();

    post.setEntity(entity);
    return performRequest(post);
}

From source file:org.wso2.msf4j.HttpServerTest.java

@Test
public void testFormParamWithFile() throws IOException, URISyntaxException {
    HttpURLConnection connection = request("/test/v1/testFormParamWithFile", HttpMethod.POST);
    File file = new File(Thread.currentThread().getContextClassLoader().getResource("testJpgFile.jpg").toURI());
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
    builder.addPart("form", fileBody);
    HttpEntity build = builder.build();/*from   w  w  w .j av  a 2 s .c  o  m*/
    connection.setRequestProperty("Content-Type", build.getContentType().getValue());
    try (OutputStream out = connection.getOutputStream()) {
        build.writeTo(out);
    }

    InputStream inputStream = connection.getInputStream();
    String response = StreamUtil.asString(inputStream);
    IOUtils.closeQuietly(inputStream);
    connection.disconnect();
    assertEquals(response, file.getName());
}