Example usage for org.apache.http.entity.mime MultipartEntity MultipartEntity

List of usage examples for org.apache.http.entity.mime MultipartEntity MultipartEntity

Introduction

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

Prototype

public MultipartEntity(final HttpMultipartMode mode) 

Source Link

Usage

From source file:uk.co.jarofgreen.cityoutdoors.API.BaseCall.java

protected void setUpCall(String url) {
    httpclient = new DefaultHttpClient();
    httppost = new HttpPost(informationNeededFromContext.getServerURL() + url);
    multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    int userID = informationNeededFromContext.getSettings().getInt("userID", -1);
    if (userID > 0) {
        addDataToCall("userID", userID);
        addDataToCall("userToken", informationNeededFromContext.getSettings().getString("userToken", ""));
        isUserTokenAttached = true;/*from w  w w  .  j a  v a2  s .c om*/
    }
}

From source file:fr.ippon.wip.http.request.MultipartRequestBuilder.java

public HttpRequestBase buildHttpRequest() throws URISyntaxException {
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    try {//  w w  w. j  a  v  a 2 s.c  om
        for (DiskFileItem fileItem : files) {
            if (fileItem.isFormField())
                multipartEntity.addPart(fileItem.getFieldName(), new StringBody(new String(fileItem.get())));
            else {
                //               FileBody fileBody = new FileBody(fileItem.getStoreLocation(), fileItem.getName(), fileItem.getContentType(), fileItem.getCharSet());
                InputStreamBody fileBody = new InputStreamKnownSizeBody(fileItem.getInputStream(),
                        fileItem.get().length, fileItem.getContentType(), fileItem.getName());
                multipartEntity.addPart(fileItem.getFieldName(), fileBody);
            }
        }

        // some request may have additional parameters in a query string
        if (parameterMap != null)
            for (Entry<String, String> entry : parameterMap.entries())
                multipartEntity.addPart(entry.getKey(), new StringBody(entry.getValue()));

    } catch (Exception e) {
        e.printStackTrace();
    }

    HttpPost postRequest = new HttpPost(requestedURL);
    postRequest.setEntity(multipartEntity);
    return postRequest;
}

From source file:de.xwic.appkit.core.file.impl.hbn.RemoteFileAccessClient.java

@Override
protected int storeFile(final File file) throws IOException {
    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
    multipartEntity.addPart(PARAM_ACTION, new StringBody(ACTION_FILE_HANDLE));
    multipartEntity.addPart(PARAM_FH_ACTION, new StringBody(PARAM_FH_ACTION_UPLOAD));
    multipartEntity.addPart(PARAM_FH_STREAM, new FileBody(file));
    return URemoteAccessClient.multipartRequestInt(multipartEntity, config);
}

From source file:org.opencastproject.remotetest.server.WorkingFileRepoRestEndpointTest.java

@Test
public void testPutAndGetFile() throws Exception {
    // Store a file in the repository
    String mediapackageId = "123";
    String elementId = "456";
    byte[] bytesFromPost = IOUtils
            .toByteArray(getClass().getClassLoader().getResourceAsStream("opencast_header.gif"));
    InputStream in = getClass().getClassLoader().getResourceAsStream("opencast_header.gif");
    String fileName = "our_logo.gif"; // Used to simulate a file upload
    MultipartEntity postEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    postEntity.addPart("file", new InputStreamBody(in, fileName));
    HttpPost post = new HttpPost(BASE_URL + "/files/mediapackage/" + mediapackageId + "/" + elementId);
    post.setEntity(postEntity);// w  w  w .  j ava 2  s.  c o m
    HttpResponse response = client.execute(post);
    HttpEntity responseEntity = response.getEntity();
    String stringResponse = EntityUtils.toString(responseEntity);
    String expectedResponse = BASE_URL + "/files/mediapackage/" + mediapackageId + "/" + elementId + "/"
            + fileName;
    Assert.assertEquals(expectedResponse, stringResponse);

    // Get the file back from the repository
    HttpGet get = new HttpGet(BASE_URL + "/files/mediapackage/" + mediapackageId + "/" + elementId);
    HttpResponse getResponse = client.execute(get);
    byte[] bytesFromGet = IOUtils.toByteArray(getResponse.getEntity().getContent());

    // Ensure that the bytes that we posted are the same we received
    Assert.assertTrue(Arrays.equals(bytesFromGet, bytesFromPost));
}

From source file:com.revo.deployr.client.call.project.ProjectImportCall.java

/**
 * Internal use only, to execute call use RClient.execute().
 *//* w w w.ja va2 s.  c om*/
public RCoreResult call() {

    RCoreResultImpl pResult = null;

    try {

        HttpPost httpPost = new HttpPost(serverUrl + API);
        super.httpUriRequest = httpPost;

        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new BasicNameValuePair("format", "json"));

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip"));
        entity.addPart("descr", new StringBody(descr, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8")));

        httpPost.setEntity(entity);

        // set any custom headers on the request            
        for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }

        HttpResponse response = httpClient.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        HttpEntity responseEntity = response.getEntity();
        String markup = EntityUtils.toString(responseEntity);

        pResult = new RCoreResultImpl(response.getAllHeaders());
        pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase());

    } catch (UnsupportedEncodingException ueex) {
        log.warn("ProjectImportCall: unsupported encoding exception.", ueex);
    } catch (IOException ioex) {
        log.warn("ProjectImportCall: io exception.", ioex);
    }

    return pResult;
}

From source file:com.revo.deployr.client.call.project.ProjectWorkspaceUploadCall.java

/**
 * Internal use only, to execute call use RClient.execute().
 *//* w  w w  .j a va 2s.co  m*/
public RCoreResult call() {

    RCoreResultImpl pResult = null;

    try {

        HttpPost httpPost = new HttpPost(serverUrl + API);
        super.httpUriRequest = httpPost;

        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new BasicNameValuePair("format", "json"));

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        entity.addPart("project", new StringBody(this.project, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("name", new StringBody(this.name, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip"));
        entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8")));

        httpPost.setEntity(entity);

        // set any custom headers on the request            
        for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }

        HttpResponse response = httpClient.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        HttpEntity responseEntity = response.getEntity();
        String markup = EntityUtils.toString(responseEntity);

        pResult = new RCoreResultImpl(response.getAllHeaders());
        pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase());

    } catch (UnsupportedEncodingException ueex) {
        log.warn("ProjectWorkspaceUploadCall: unsupported encoding exception.", ueex);
    } catch (IOException ioex) {
        log.warn("ProjectWorkspaceUploadCall: io exception.", ioex);
    }

    return pResult;
}

From source file:com.revo.deployr.client.call.repository.RepositoryFileUploadCall.java

/**
 * Internal use only, to execute call use RClient.execute().
 *//* w w w. jav a 2  s . co  m*/
public RCoreResult call() {

    RCoreResultImpl pResult = null;

    try {

        HttpPost httpPost = new HttpPost(serverUrl + API);
        super.httpUriRequest = httpPost;

        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new BasicNameValuePair("format", "json"));

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip"));
        if (options.filename != null)
            entity.addPart("filename",
                    new StringBody(options.filename, "text/plain", Charset.forName("UTF-8")));
        if (options.directory != null)
            entity.addPart("directory",
                    new StringBody(options.directory, "text/plain", Charset.forName("UTF-8")));
        if (options.descr != null)
            entity.addPart("descr", new StringBody(options.descr, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("newversion",
                new StringBody(Boolean.toString(options.newversion), "text/plain", Charset.forName("UTF-8")));
        if (options.newversionmsg != null)
            entity.addPart("newversionmsg",
                    new StringBody(options.newversionmsg, "text/plain", Charset.forName("UTF-8")));
        if (options.restricted != null)
            entity.addPart("restricted",
                    new StringBody(options.restricted, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("shared",
                new StringBody(Boolean.toString(options.shared), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("published",
                new StringBody(Boolean.toString(options.published), "text/plain", Charset.forName("UTF-8")));
        if (options.inputs != null)
            entity.addPart("inputs", new StringBody(options.inputs, "text/plain", Charset.forName("UTF-8")));
        if (options.outputs != null)
            entity.addPart("outputs", new StringBody(options.outputs, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8")));

        httpPost.setEntity(entity);

        // set any custom headers on the request            
        for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }

        HttpResponse response = httpClient.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        HttpEntity responseEntity = response.getEntity();
        String markup = EntityUtils.toString(responseEntity);

        pResult = new RCoreResultImpl(response.getAllHeaders());
        pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase());

    } catch (UnsupportedEncodingException ueex) {
        log.warn("RepositoryFileUploadCall: unsupported encoding exception.", ueex);
    } catch (IOException ioex) {
        log.warn("RepositoryFileUploadCall: io exception.", ioex);
    }

    return pResult;
}

From source file:com.revo.deployr.client.call.repository.RepositoryDirectoryUploadCall.java

/**
 * Internal use only, to execute call use RClient.execute().
 *///from  w  ww  . j  av  a 2  s . com
public RCoreResult call() {

    RCoreResultImpl pResult = null;

    try {

        HttpPost httpPost = new HttpPost(serverUrl + API);
        super.httpUriRequest = httpPost;

        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new BasicNameValuePair("format", "json"));

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        entity.addPart("file", new InputStreamBody(((InputStream) zipStream), "application/zip"));
        if (options.directory != null)
            entity.addPart("directory",
                    new StringBody(options.directory, "text/plain", Charset.forName("UTF-8")));
        if (options.descr != null)
            entity.addPart("descr", new StringBody(options.descr, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("newversion",
                new StringBody(Boolean.toString(options.newversion), "text/plain", Charset.forName("UTF-8")));
        if (options.restricted != null)
            entity.addPart("restricted",
                    new StringBody(options.restricted, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("shared",
                new StringBody(Boolean.toString(options.shared), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("published",
                new StringBody(Boolean.toString(options.published), "text/plain", Charset.forName("UTF-8")));
        if (options.inputs != null)
            entity.addPart("inputs", new StringBody(options.inputs, "text/plain", Charset.forName("UTF-8")));
        if (options.outputs != null)
            entity.addPart("outputs", new StringBody(options.outputs, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8")));

        httpPost.setEntity(entity);

        // set any custom headers on the request            
        for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }

        HttpResponse response = httpClient.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        HttpEntity responseEntity = response.getEntity();
        String markup = EntityUtils.toString(responseEntity);

        pResult = new RCoreResultImpl(response.getAllHeaders());
        pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase());

    } catch (UnsupportedEncodingException ueex) {
        log.warn("RepositoryDirectoryUploadCall: unsupported encoding exception.", ueex);
    } catch (IOException ioex) {
        log.warn("RepositoryDirectoryUploadCall: io exception.", ioex);
    }

    return pResult;
}

From source file:key.access.manager.HttpHandler.java

public boolean sendImage(String url, String employeeId, File imageFile) throws IOException {
    String userHome = System.getProperty("user.home");
    HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpPost httpPost = new HttpPost(url);
    FileBody fileBody = new FileBody(imageFile);
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    reqEntity.addPart("fileToUpload", fileBody);
    reqEntity.addPart("employee_id", new StringBody(employeeId));

    httpPost.setEntity(reqEntity);//from   ww  w.ja v  a2  s. c  om

    // execute HTTP post request
    HttpResponse response = httpClient.execute(httpPost);
    HttpEntity resEntity = response.getEntity();

    if (resEntity != null) {
        String responseStr = EntityUtils.toString(resEntity).trim();
        System.out.println(responseStr);
        return true;
    } else {
        return false;
    }
}

From source file:com.revo.deployr.client.call.project.ProjectDirectoryUploadCall.java

/**
 * Internal use only, to execute call use RClient.execute().
 *///from   w ww  . j  a  va2  s.c  om
public RCoreResult call() {

    RCoreResultImpl pResult = null;

    try {

        HttpPost httpPost = new HttpPost(serverUrl + API);
        super.httpUriRequest = httpPost;

        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new BasicNameValuePair("format", "json"));

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        entity.addPart("file", new InputStreamBody(((InputStream) fileStream), "application/zip"));
        entity.addPart("project", new StringBody(project, "text/plain", Charset.forName("UTF-8")));
        entity.addPart("filename", new StringBody(options.filename, "text/plain", Charset.forName("UTF-8")));
        if (options.descr != null)
            entity.addPart("descr", new StringBody(options.descr, "text/plain", Charset.forName("UTF-8")));

        entity.addPart("overwrite",
                new StringBody(Boolean.toString(options.overwrite), "text/plain", Charset.forName("UTF-8")));
        entity.addPart("format", new StringBody("json", "text/plain", Charset.forName("UTF-8")));

        httpPost.setEntity(entity);

        // set any custom headers on the request            
        for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
            httpPost.addHeader(entry.getKey(), entry.getValue());
        }

        HttpResponse response = httpClient.execute(httpPost);
        StatusLine statusLine = response.getStatusLine();
        HttpEntity responseEntity = response.getEntity();
        String markup = EntityUtils.toString(responseEntity);

        pResult = new RCoreResultImpl(response.getAllHeaders());
        pResult.parseMarkup(markup, API, statusLine.getStatusCode(), statusLine.getReasonPhrase());

    } catch (UnsupportedEncodingException ueex) {
        log.warn("ProjectDirectoryUploadCall: unsupported encoding exception.", ueex);
    } catch (IOException ioex) {
        log.warn("ProjectDirectoryUploadCall: io exception.", ioex);
    }

    return pResult;
}