Example usage for org.apache.http.entity ContentType APPLICATION_OCTET_STREAM

List of usage examples for org.apache.http.entity ContentType APPLICATION_OCTET_STREAM

Introduction

In this page you can find the example usage for org.apache.http.entity ContentType APPLICATION_OCTET_STREAM.

Prototype

ContentType APPLICATION_OCTET_STREAM

To view the source code for org.apache.http.entity ContentType APPLICATION_OCTET_STREAM.

Click Source Link

Usage

From source file:ClientChunkEncodedPost.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("File path not given");
        System.exit(1);//  w  w  w  .  j  a va2  s  .co  m
    }
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        HttpPost httppost = new HttpPost("http://localhost/");

        File file = new File(args[0]);

        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1,
                ContentType.APPLICATION_OCTET_STREAM);
        reqEntity.setChunked(true);
        // It may be more appropriate to use FileEntity class in this particular
        // instance but we are using a more generic InputStreamEntity to demonstrate
        // the capability to stream out data from any arbitrary source
        //
        // FileEntity entity = new FileEntity(file, "binary/octet-stream");

        httppost.setEntity(reqEntity);

        System.out.println("Executing request: " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:demo.example.ClientChunkEncodedPost.java

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("File path not given");
        System.exit(1);/*from   w  w w  .  j a  v  a 2s  .c  o  m*/
    }
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        HttpPost httppost = new HttpPost("http://httpbin.org/post");

        File file = new File(args[0]);

        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1,
                ContentType.APPLICATION_OCTET_STREAM);
        reqEntity.setChunked(true);
        // It may be more appropriate to use FileEntity class in this particular
        // instance but we are using a more generic InputStreamEntity to demonstrate
        // the capability to stream out data from any arbitrary source
        //
        // FileEntity entity = new FileEntity(file, "binary/octet-stream");

        httppost.setEntity(reqEntity);

        System.out.println("Executing request: " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:httpclientdemo.ClientChunkEncodedPost.java

public static void main(String[] args) throws Exception {
    //        if (args.length != 1)  {
    //            System.out.println("File path not given");
    //            System.exit(1);
    //        }/*from w w  w. j  a v  a  2  s  . c o m*/
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        HttpPost httppost = new HttpPost("http://httpbin.org/post");

        File file = new File("/Users/jack/Downloads/listdata (1).xlsx");

        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1,
                ContentType.APPLICATION_OCTET_STREAM);
        reqEntity.setChunked(true);
        // It may be more appropriate to use FileEntity class in this particular
        // instance but we are using a more generic InputStreamEntity to demonstrate
        // the capability to stream out data from any arbitrary source
        //
        // FileEntity entity = new FileEntity(file, "binary/octet-stream");

        httppost.setEntity(reqEntity);

        System.out.println("Executing request: " + httppost.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httppost);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            System.out.println(EntityUtils.toString(response.getEntity()));
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:core.VirusTotalAPIHelper.java

public static CloseableHttpResponse scanFile(File fileToScan) {
    if (apiKey == null || apiKey.isEmpty()) {
        return null;
    }/*w  w  w.  j a  va 2 s .  c  o  m*/
    try {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpsScanFilePost = new HttpPost(httpsPost + "file/scan");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addTextBody("apikey", apiKey);
        builder.addBinaryBody("file", fileToScan, ContentType.APPLICATION_OCTET_STREAM, fileToScan.getName());
        HttpEntity multipart = builder.build();
        httpsScanFilePost.setEntity(multipart);

        CloseableHttpResponse response = client.execute(httpsScanFilePost);
        return response;
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    return null;
}

From source file:com.github.tomakehurst.wiremock.testsupport.MultipartBody.java

MultipartBody(String name, byte[] body) {
    super(ContentType.APPLICATION_OCTET_STREAM);
    Args.notEmpty(name, "Name was empty");
    Args.notNull(body, "Body was null");
    this.name = name;
    this.body = body;
}

From source file:ApkItem.java

public void uploadFile() {
    try {/*from  w w w .j  ava  2 s .  c  o  m*/
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://192.168.1.130:8080/imooc/");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody(file.getName(), file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
        HttpEntity multipart = builder.build();
        CountingMultipartRequestEntity.ProgressListener pListener = new CountingMultipartRequestEntity.ProgressListener() {
            @Override
            public void progress(float percentage) {
                apkState.setProcess((int) percentage);
                firePropertyChange("uploadProcess", -1, percentage);
            }
        };
        httpPost.setEntity(new CountingMultipartRequestEntity.ProgressEntityWrapper(multipart, pListener));
        CloseableHttpResponse response = client.execute(httpPost);
        client.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:net.yacy.cora.protocol.ByteArrayBody.java

/**
 *
 * @param bytes of 'file'/* w  ww . j  a v a2 s.com*/
 * @param filename
 */
public ByteArrayBody(final byte[] bytes, final String filename) {
    super(ContentType.APPLICATION_OCTET_STREAM);
    this.bytes = bytes;
    this.filename = filename;
}

From source file:org.gradle.internal.resource.transport.http.HttpResourceUploader.java

public void upload(Factory<InputStream> source, Long contentLength, URI destination) throws IOException {
    HttpPut method = new HttpPut(destination);
    final RepeatableInputStreamEntity entity = new RepeatableInputStreamEntity(source, contentLength,
            ContentType.APPLICATION_OCTET_STREAM);
    method.setEntity(entity);// ww  w  .  j  av  a 2s  .  co  m
    HttpResponse response = http.performHttpRequest(method);
    EntityUtils.consume(response.getEntity());
    if (!http.wasSuccessful(response)) {
        throw new IOException(
                String.format("Could not PUT '%s'. Received status code %s from server: %s", destination,
                        response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase()));
    }

}

From source file:onl.area51.httpd.util.ContentTypeResolver.java

public static ContentType resolve(String path) {
    if (path != null && !path.isEmpty()) {
        int i = path.lastIndexOf('.');
        int j = path.lastIndexOf('/');
        if (i > -1 && i > j) {
            return CONTENT_TYPES.getOrDefault(path.substring(i).toLowerCase(), APPLICATION_OCTET_STREAM);
        }/*  w w w.j  av  a 2  s.com*/
    }
    return ContentType.APPLICATION_OCTET_STREAM;
}

From source file:com.movilizer.mds.webservice.models.MovilizerUploadForm.java

/**
 * Create an HTTP multipart form to perform a request.
 *
 * @param file with the document to be uploaded.
 * @param filename of the document./* w  w w .  j  a v a2  s  .  c  om*/
 * @param systemId of Movilizer Cloud.
 * @param password for the system id.
 * @param pool to store the document at.
 * @param key to find the document in the pool.
 * @param lang of the document.
 * @param suffix ending of the document (".zip" for HTML5 apps).
 * @param ack value that the cloud will return to you when the document is uploaded.
 * @return HttpEntity to be used in the request.
 */
public HttpEntity getForm(InputStream file, String filename, long systemId, String password, String pool,
        String key, String lang, String suffix, String ack) {
    MultipartEntityBuilder form = getForm(systemId, password, pool, key, lang, suffix, ack);
    form.addBinaryBody("file", file, ContentType.APPLICATION_OCTET_STREAM, filename);
    return form.build();
}