Example usage for org.apache.http.entity FileEntity setChunked

List of usage examples for org.apache.http.entity FileEntity setChunked

Introduction

In this page you can find the example usage for org.apache.http.entity FileEntity setChunked.

Prototype

public void setChunked(boolean z) 

Source Link

Usage

From source file:Main.java

/**
 * Method to initialize and prepare an HttpPost object
 * @param file//from   w  ww  .  j  av  a2 s.c o m
 * @param url
 * @param chunked
 * @return HttpPost
 */
public static HttpPost prepareRequest(File file, String url, boolean chunked) {
    HttpPost post = new HttpPost(url);

    FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
    reqEntity.setContentType("binary/octet-stream");
    reqEntity.setChunked(chunked);
    post.setEntity(reqEntity);

    return post;
}

From source file:de.unikassel.android.sdcframework.transmission.SimpleHttpProtocol.java

/**
 * Method for an HTTP upload of file stream
 * /* w w w  . j  a  va 2 s .  c o m*/
 * @param file
 *          the input file
 * 
 * @return true if successful, false otherwise
 */
private final boolean httpUpload(File file) {
    DefaultHttpClient client = new DefaultHttpClient();

    try {
        String fileName = FileUtils.fileNameFromPath(file.getName());
        String contentType = getContentType(fileName);

        URL url = getURL();
        configureForAuthentication(client, url);

        HttpPost httpPost = new HttpPost(url.toURI());

        FileEntity fileEntity = new FileEntity(file, contentType);
        fileEntity.setContentType(contentType);
        fileEntity.setChunked(true);

        httpPost.setEntity(fileEntity);
        httpPost.addHeader("filename", fileName);
        httpPost.addHeader("uuid", getUuid().toString());

        HttpResponse response = client.execute(httpPost);

        int statusCode = response.getStatusLine().getStatusCode();
        boolean success = statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NO_CONTENT;
        Logger.getInstance().debug(this, "Server returned: " + statusCode);

        // clean up if necessary
        HttpEntity resEntity = response.getEntity();
        if (resEntity != null) {
            resEntity.consumeContent();
        }

        if (!success) {
            doHandleError("Unexpected server response: " + response.getStatusLine());
            success = false;
        }

        return success;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        doHandleError(PROTOCOL_EXCEPTION + ": " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        doHandleError(IO_EXCEPTION + ": " + e.getMessage());
    } catch (URISyntaxException e) {
        setURL(null);
        doHandleError(INVALID_URL);
    } finally {
        client.getConnectionManager().shutdown();
    }
    return false;
}

From source file:com.bt.download.android.core.HttpFetcher.java

public void post(File file) throws IOException {
    HttpHost httpHost = new HttpHost(uri.getHost(), uri.getPort());
    HttpPost httpPost = new HttpPost(uri);
    FileEntity fileEntity = new FileEntity(file, "binary/octet-stream");
    fileEntity.setChunked(true);
    httpPost.setEntity(fileEntity);/*from w  w w  . ja va2 s. c om*/

    HttpParams params = httpPost.getParams();
    HttpConnectionParams.setConnectionTimeout(params, timeout);
    HttpConnectionParams.setSoTimeout(params, timeout);
    HttpConnectionParams.setStaleCheckingEnabled(params, true);
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpClientParams.setRedirecting(params, true);
    HttpProtocolParams.setUseExpectContinue(params, false);
    HttpProtocolParams.setUserAgent(params, DEFAULT_USER_AGENT);

    try {

        HttpResponse response = DEFAULT_HTTP_CLIENT.execute(httpHost, httpPost);

        if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 300)
            throw new IOException("bad status code, upload file " + response.getStatusLine().getStatusCode());

    } catch (IOException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("Http error: " + e.getMessage());
    } finally {
        //
    }
}

From source file:org.ellis.yun.search.test.httpclient.HttpClientTest.java

@Test
public void testEntityChunk() throws Exception {
    File file = new File("src/test/resources/README.txt");
    @SuppressWarnings("deprecation")
    FileEntity entity = new FileEntity(file, "text/plain; charset=\"UTF-8\"");
    entity.setChunked(true);
    System.out.println(entity.isChunked() + "");
}

From source file:edu.isi.misd.tagfiler.client.JakartaClient.java

/**
 * Uploads a set of given files with a specified dataset name.
 * //from   ww w  . j  ava  2 s .com
 * @param url
 *            the query url
 * @param file
 *            the file to be uploaded
 * @param cookie
 *            the cookie to be set in the request
 * @return the HTTP Response
 */
public ClientURLResponse postFile(String url, File file, String cookie) {
    HttpPut httpput = new HttpPut(url);
    httpput.setHeader("Content-Type", "application/octet-stream");
    FileEntity fileEntity = new FileEntity(file, "binary/octet-stream");
    fileEntity.setChunked(false);
    httpput.setEntity(fileEntity);
    return execute(httpput, cookie);
}

From source file:org.apache.http.contrib.benchmark.HttpBenchmark.java

private void prepare() {
    // prepare http params
    params = getHttpParams(socketTimeout, useHttp1_0);

    host = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());

    // Prepare requests for each thread
    request = new HttpRequest[threads];

    if (postFile != null) {
        FileEntity entity = new FileEntity(postFile, contentType);
        contentLength = entity.getContentLength();
        if (postFile.length() > 100000) {
            entity.setChunked(true);
        }//w  w w  .  j  a v  a  2s .c  o  m

        for (int i = 0; i < threads; i++) {
            BasicHttpEntityEnclosingRequest httppost = new BasicHttpEntityEnclosingRequest("POST",
                    url.getPath());
            httppost.setEntity(entity);
            request[i] = httppost;
        }

    } else if (doHeadInsteadOfGet) {
        for (int i = 0; i < threads; i++) {
            request[i] = new BasicHttpRequest("HEAD", url.getPath());
        }

    } else {
        for (int i = 0; i < threads; i++) {
            request[i] = new BasicHttpRequest("GET", url.getPath());
        }
    }

    if (!keepAlive) {
        for (int i = 0; i < threads; i++) {
            request[i].addHeader(new DefaultHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE));
        }
    }

    if (headers != null) {
        for (int i = 0; i < headers.length; i++) {
            String s = headers[i];
            int pos = s.indexOf(':');
            if (pos != -1) {
                Header header = new DefaultHeader(s.substring(0, pos).trim(), s.substring(pos + 1));
                for (int j = 0; j < threads; j++) {
                    request[j].addHeader(header);
                }
            }
        }
    }
}