Example usage for org.apache.http.entity.mime CustomMultiPartEntity getContentLength

List of usage examples for org.apache.http.entity.mime CustomMultiPartEntity getContentLength

Introduction

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

Prototype

public long getContentLength() 

Source Link

Usage

From source file:com.jelastic.JelasticService.java

public UploadResponse upload(AuthenticationResponse authenticationResponse) {
    UploadResponse uploadResponse = null;
    try {//from www . j a  va 2s  .com
        DefaultHttpClient httpclient = getHttpClient();

        final File file = new File(getDir() + File.separator + getFilename());
        if (!file.exists()) {
            throw new IllegalArgumentException(
                    "First build artifact and try again. Artifact not found in location: ["
                            + file.getAbsolutePath() + "]");
        }

        CustomMultiPartEntity multipartEntity = new CustomMultiPartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
                new CustomMultiPartEntity.ProgressListener() {
                    public void transferred(long num) {
                        if (((int) ((num / (float) totalSize) * 100)) != numSt) {
                            System.out.println(
                                    "File Uploading : [" + (int) ((num / (float) totalSize) * 100) + "%]");
                            numSt = ((int) ((num / (float) totalSize) * 100));
                        }
                    }
                });

        multipartEntity.addPart("fid", new StringBody("123456"));
        multipartEntity.addPart("session", new StringBody(authenticationResponse.getSession()));
        multipartEntity.addPart("file", new FileBody(file));
        totalSize = multipartEntity.getContentLength();

        URI uri = URIUtils.createURI(getProtocol(), getApiHoster(), getPort(), getUrlUploader(), null, null);
        project.log("Upload url : " + uri.toString(), Project.MSG_DEBUG);
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setEntity(multipartEntity);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = httpclient.execute(httpPost, responseHandler);
        project.log("Upload response : " + responseBody, Project.MSG_DEBUG);

        uploadResponse = deserialize(responseBody, UploadResponse.class);
    } catch (URISyntaxException | IOException e) {
        project.log(e.getMessage(), Project.MSG_ERR);
    }
    return uploadResponse;
}