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

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

Introduction

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

Prototype

public FileEntity(File file, ContentType contentType) 

Source Link

Usage

From source file:edu.ucsd.ccdb.cil.xml2json.ElasticsearchClient.java

public static void main(String[] args) throws Exception {
    try {/*from w  ww .ja va  2 s  . c  o m*/
        DefaultHttpClient httpClient = new DefaultHttpClient();

        //HttpPut put = new HttpPut("http://localhost:9200/customer/user/john@smith.com");  //-X PUT
        //put.setEntity(new FileEntity(new File("/Users/ncmir/NetBeansProjects/MyTest/web/customer.json"), "application/json"));  //@ - absolute path
        HttpPut put = new HttpPut("http://localhost:9200/ccdb/data/1"); //-X PUT
        put.setEntity(
                new FileEntity(new File("/Users/ncmir/Documents/CCDB/ccdbJson/1.json"), "application/json")); //@ - absolute path

        BasicResponseHandler responseHandler = new BasicResponseHandler();

        String o = httpClient.execute(put, responseHandler);
        System.out.println(o);
    } catch (Exception e) {
        //-f, fail silently
        e.printStackTrace();
    }
}

From source file:com.client.SoapClientApache.java

public static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from w w  w  .ja  va 2s  . com
        HttpGet httpGet = new HttpGet("http://google.com/");
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        // The underlying HTTP connection is still held by the response object
        // to allow the response content to be streamed directly from the network socket.
        // In order to ensure correct deallocation of system resources
        // the user MUST either fully consume the response content  or abort request
        // execution by calling CloseableHttpResponse#close().

        try {
            System.out.println("Response for http get");
            System.out.println(response1.getStatusLine());
            HttpEntity entity1 = response1.getEntity();
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity1);
        } finally {
            response1.close();
        }

        File file = new File("temConvertRq.xml");
        File fileOutXml = new File("temConvertRs.xml");
        fileOutXml.createNewFile();

        System.out.println("Before file entity");
        FileEntity entity = new FileEntity(file, ContentType.create("text/xml", "UTF-8"));

        System.out.println("Before http post");
        HttpPost httpPost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx");

        httpPost.setEntity(entity);
        System.out.println("Entity set");
        //Add headers
        httpPost.addHeader("SOAPAction", "http://www.w3schools.com/webservices/CelsiusToFahrenheit");
        httpPost.addHeader("Host", "www.w3schools.com");
        httpPost.addHeader("Content-Type", "text/xml; charset=utf-8");
        //httpPost.addHeader("Content-Length", "length");

        System.out.println("after headers added");
        CloseableHttpResponse response2 = httpclient.execute(httpPost);

        try {
            System.out.println("before getting the response status");
            System.out.println("Response Status= " + response2.getStatusLine());
            HttpEntity entity2 = response2.getEntity();
            String outXml = EntityUtils.toString(entity2);
            System.out.println("XMl Response!!!!!!!!! = " + outXml);

            //                entity2.writeTo(System.out);

            //System.out.println("R
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity2);

            FileUtils.writeStringToFile(fileOutXml, outXml, "UTF-8");

        } finally {
            response2.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:Main.java

/**
 * Method to initialize and prepare an HttpPost object
 * @param file//from   w w w  .j  a v  a  2  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:edu.ucsd.ccdb.cil.xml2json.ElasticsearchClient.java

public void xputElastic(String index, String type, String ID, File f) throws Exception {

    DefaultHttpClient httpClient = new DefaultHttpClient();
    String url = "http://localhost:9200/" + index + "/" + type + "/" + ID;
    System.out.println(url);/*from  w w  w.jav  a  2  s .co m*/
    HttpPut put = new HttpPut(url); //-X PUT
    put.setEntity(new FileEntity(f, "application/json")); //@ - absolute path
    BasicResponseHandler responseHandler = new BasicResponseHandler();

    String o = httpClient.execute(put, responseHandler);
    /* System.err.println("----------"+o+"----------");
     if(o == null || o.trim().length() == 0)
     {
         System.err.println(o);
         System.exit(1);
     } */

}

From source file:com.spokenpic.net.RestClientFilePost.java

@Override
protected RestResult doPost() {
    HttpPost httpPost = new HttpPost(getSchemeServer() + this.uri);
    setupHttp(httpPost);/*  w  ww  . ja  v  a  2 s.  c om*/
    try {
        FileEntity e = new FileEntity(new File(data), mime);
        httpPost.setEntity(e);

        HttpResponse httpResponse = HttpManager.execute(httpPost);
        return returnResponse(httpResponse);
    } catch (Exception e) {
        Log.d("RestClientFilePost", "Error doPost " + e.toString());
        return errorResponse("Fatal error during file POST");
    }
}

From source file:com.jaeksoft.searchlib.remote.UriWriteStream.java

public UriWriteStream(URI uri, File file) throws IOException {
    HttpPut httpPut = new HttpPut(uri.toASCIIString());
    httpPut.setConfig(requestConfig);/* www  .  j a  v a2 s .c o m*/
    FileEntity fre = new FileEntity(file, ContentType.DEFAULT_BINARY);
    httpPut.setEntity(fre);
    execute(httpPut);
}

From source file:org.opencredo.cloud.storage.azure.model.FileBlob.java

/**
 * @return//from ww w .  jav  a  2 s  .c  om
 * @see org.opencredo.cloud.storage.azure.model.Blob#createRequestBody()
 */
@Override
public HttpEntity createRequestBody() {
    return new FileEntity(data, null);
}

From source file:org.semantictools.web.upload.FileUploadClient.java

public void post(String contentType, File file) throws IOException {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(serviceURI);

    FileEntity entity = new FileEntity(file, contentType);
    post.setEntity(entity);//from  w  w  w  . j  a  v  a2  s.  c  o  m

    HttpResponse response = client.execute(post);

    int status = response.getStatusLine().getStatusCode();
    switch (status) {
    case HttpStatus.SC_OK:
    case HttpStatus.SC_CREATED:
        System.out.println();
        break;

    default:
        System.out.println(" ERROR: " + status);

    }

}

From source file:service.json.StatisticHandlerSeviceTest.java

@Test
public void test() {
    HttpClient client = new DefaultHttpClient();

    try {//from ww  w  .  j a  v a2 s . c om
        Configuration config = new Configuration(IConstants.TEST_PROPERTIES);

        HttpPost post = new HttpPost(config.getConfigProperties().getProperty("test.statistic.url"));

        File file = new File(".\\config\\test.json");
        assertTrue(file.exists() && file.isFile());

        FileEntity entity = new FileEntity(file, "application/json");
        post.setEntity(entity);

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        assertNotNull(rd);

        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        logger.error(e.getMessage());
    }
}

From source file:com.yanzhenjie.andserver.handler.StorageRequestHandler.java

@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
        throws HttpException, IOException {
    File file = new File(mFilePath);
    if (!file.exists()) {
        requestInvalid(response);//  ww w .  ja  v a2  s.  co m
    } else {
        response.setStatusCode(200);
        response.setEntity(new FileEntity(file, HttpRequestParser.getMimeType(file.getName())));
    }
}