Example usage for org.apache.http.entity.mime HttpMultipartMode BROWSER_COMPATIBLE

List of usage examples for org.apache.http.entity.mime HttpMultipartMode BROWSER_COMPATIBLE

Introduction

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

Prototype

HttpMultipartMode BROWSER_COMPATIBLE

To view the source code for org.apache.http.entity.mime HttpMultipartMode BROWSER_COMPATIBLE.

Click Source Link

Usage

From source file:org.energyos.espi.datacustodian.console.ImportUsagePoint.java

public static void upload(String filename, String url, HttpClient client) throws IOException {
    HttpPost post = new HttpPost(url);
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    File file = new File(filename);
    entity.addPart("file", new FileBody(((File) file), "application/rss+xml"));

    post.setEntity(entity);//from ww w.j  a va  2 s.c  o  m

    client.execute(post);
}

From source file:org.eclipse.cbi.common.signing.Signer.java

public static void signFile(File source, File target, String signerUrl)
        throws IOException, MojoExecutionException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(signerUrl);

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    builder.addPart("file", new FileBody(source));
    post.setEntity(builder.build());/*  ww  w  .  j ava 2  s.co m*/

    HttpResponse response = client.execute(post);
    int statusCode = response.getStatusLine().getStatusCode();

    HttpEntity resEntity = response.getEntity();

    if (statusCode >= 200 && statusCode <= 299 && resEntity != null) {
        InputStream is = resEntity.getContent();
        try {
            FileUtils.copyStreamToFile(new RawInputStreamFacade(is), target);
        } finally {
            IOUtil.close(is);
        }
    } else if (statusCode >= 500 && statusCode <= 599) {
        InputStream is = resEntity.getContent();
        String message = IOUtil.toString(is, "UTF-8");
        throw new NoHttpResponseException("Server failed with " + message);
    } else {
        throw new MojoExecutionException("Signer replied " + response.getStatusLine());
    }
}

From source file:org.artags.android.app.util.http.HttpUtil.java

/**
 * Post data and attachements//  w  ww.j  av  a 2  s. co m
 * @param url The POST url
 * @param params Parameters
 * @param files Files
 * @return The return value
 * @throws HttpException If an error occurs
 */
public static String post(String url, HashMap<String, String> params, HashMap<String, File> files)
        throws HttpException {
    String ret = "";
    try {
        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost(url);

        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for (String key : files.keySet()) {
            FileBody bin = new FileBody(files.get(key));
            reqEntity.addPart(key, bin);
        }

        for (String key : params.keySet()) {
            String val = params.get(key);

            reqEntity.addPart(key, new StringBody(val, Charset.forName("UTF-8")));
        }
        post.setEntity(reqEntity);
        HttpResponse response = client.execute(post);

        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            ret = EntityUtils.toString(resEntity);
            Log.i("ARTags:HttpUtil:Post:Response", ret);
        }

        //return response;
    } catch (Exception e) {
        Log.e("ARTags:HttpUtil", "Error : " + e.getMessage());
        throw new HttpException(e.getMessage());
    }
    return ret;
}

From source file:io.confluent.support.metrics.utils.WebClient.java

/**
 * Sends a POST request to a web server//  w  w w.  j a  v a 2s .  c  o m
 * @param customerId: customer Id on behalf of which the request is sent
 * @param bytes: request payload
 * @param httpPost: A POST request structure
 * @return an HTTP Status code
 */
public static int send(String customerId, byte[] bytes, HttpPost httpPost) {
    int statusCode = DEFAULT_STATUS_CODE;
    if (bytes != null && bytes.length > 0 && httpPost != null && customerId != null) {

        // add the body to the request
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addTextBody("cid", customerId);
        builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, "filename");
        httpPost.setEntity(builder.build());

        // set the HTTP config
        final RequestConfig config = RequestConfig.custom().setConnectTimeout(requestTimeoutMs)
                .setConnectionRequestTimeout(requestTimeoutMs).setSocketTimeout(requestTimeoutMs).build();

        // send request
        try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config)
                .build(); CloseableHttpResponse response = httpclient.execute(httpPost)) {
            log.debug("POST request returned {}", response.getStatusLine().toString());
            statusCode = response.getStatusLine().getStatusCode();
        } catch (IOException e) {
            log.debug("Could not submit metrics to Confluent: {}", e.getMessage());
        }
    } else {
        statusCode = HttpStatus.SC_BAD_REQUEST;
    }
    return statusCode;
}

From source file:net.netheos.pcsapi.request.CloudMeMultipartEntity.java

public CloudMeMultipartEntity() {
    super(HttpMultipartMode.BROWSER_COMPATIBLE, null, PcsUtils.UTF8);
}

From source file:com.liferay.mobile.android.http.file.UploadUtil.java

protected static HttpEntity getMultipartEntity(HttpPostHC4 request, JSONObject parameters) throws Exception {

    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    ContentType contentType = ContentType.create("text/plain", Consts.UTF_8);

    Iterator<String> it = parameters.keys();

    while (it.hasNext()) {
        String key = it.next();/*from   ww w  . ja  v a  2s. com*/
        Object value = parameters.get(key);

        ContentBody contentBody;

        if (value instanceof UploadData) {
            UploadData wrapper = (UploadData) value;
            wrapper.setRequest(request);

            contentBody = wrapper;
        } else {
            contentBody = new StringBody(value.toString(), contentType);
        }

        builder.addPart(key, contentBody);
    }

    return builder.build();
}

From source file:net.bither.api.UploadAvatarApi.java

@Override
public HttpEntity getHttpEntity() throws Exception {
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    multipartEntity.addPart(FILE_KEY, new FileBody(this.mFile));
    return multipartEntity;
}

From source file:org.alfresco.cacheserver.MultipartTest.java

@Test
public void test1() throws Exception {
    byte[] b = "Hello world".getBytes();
    InputStream in = new ByteArrayInputStream(b);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE).addBinaryBody("p_stream", in)
            .addTextBody("p_size", String.valueOf(b.length)).addTextBody("p_idx", String.valueOf(10));
    HttpEntity entity = builder.build();

    entity.writeTo(System.out);//from   ww  w. j ava 2s .  c om
    //      BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
    //      while ((inputLine = br.readLine()) != null) {
    //         System.out.println(inputLine);
    //      }
    //      br.close();

    //      HttpPost httpPost = new HttpPost("http://localhost:2389/TESTME_WITH_NETCAT");
    //      httpPost.setEntity(entity);
}

From source file:com.github.yongchristophertang.engine.web.http.MultipartBodyFormBuilder.java

private MultipartBodyFormBuilder() {
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
}

From source file:org.overlord.sramp.governance.workflow.Multipart.java

public void post(HttpClient httpclient, URI uri, Map<String, Object> parameters)
        throws IOException, WorkflowException {
    MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    for (String key : parameters.keySet()) {
        ContentBody content = null;//  ww  w  .ja  va2 s  .  com
        Object param = parameters.get(key);
        if (param instanceof String) {
            StringBody stringBody = new StringBody((String) param, "text/plain", Charset.forName("UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$
            content = stringBody;
        } else {
            //turn object into byteArray, or it also supports InputStreamBody or FileBody
            ByteArrayBody byteBody = new ByteArrayBody(null, key);
            content = byteBody;
        }
        multiPartEntity.addPart(key, content);
    }
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(multiPartEntity);
    HttpResponse response = httpclient.execute(httpPost);
    InputStream is = response.getEntity().getContent();
    String responseStr = IOUtils.toString(is);
    if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201) {
        logger.debug(responseStr);
    } else {
        throw new WorkflowException(
                "Workflow ERROR - HTTP STATUS CODE " + response.getStatusLine().getStatusCode() + ". " //$NON-NLS-1$ //$NON-NLS-2$
                        + response.getStatusLine().getReasonPhrase() + ". " + responseStr); //$NON-NLS-1$
    }
    is.close();
}