Example usage for org.apache.http.entity.mime.content ByteArrayBody ByteArrayBody

List of usage examples for org.apache.http.entity.mime.content ByteArrayBody ByteArrayBody

Introduction

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

Prototype

public ByteArrayBody(final byte[] data, final String filename) 

Source Link

Document

Creates a new ByteArrayBody.

Usage

From source file:hpcc.hut.edu.vn.ocr.ocrserviceconnector.HpccOcrServiceConnector.java

public static String postToOcrService(byte[] data, int size, int imgw, int imgh, String lang, int psm,
        boolean isPrePostProcess) {
    System.out.println("Sent data: w = " + imgw + ", h = " + imgh + ", psm = " + psm + ", process: "
            + isPrePostProcess + ", with lang: " + lang);
    String result = "";

    try {//from w w  w  .ja v a  2  s  . c om
        HttpParams httpParamenters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParamenters, 30000);
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParamenters);
        HttpPost postRequest = new HttpPost(HOST);

        ByteArrayBody bab = new ByteArrayBody(data, "input.jpg");
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("image", bab);
        reqEntity.addPart("size", new StringBody("" + size)); // size to
        // check if
        // decompress
        // fail
        reqEntity.addPart("width", new StringBody("" + imgw));
        reqEntity.addPart("height", new StringBody("" + imgh));
        reqEntity.addPart("lang", new StringBody(lang));
        reqEntity.addPart("psm", new StringBody("" + psm));
        reqEntity.addPart("process", new StringBody("" + isPrePostProcess));

        postRequest.setEntity(reqEntity);

        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();
        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        result = s.toString();
        System.out.println("result in Json: " + result);
    } catch (Exception e) {
        // handle exception here
        Log.e(e.getClass().getName(), e.getMessage());
        return null;
    }
    return result;

}

From source file:cn.clxy.upload.ApacheHCUploader.java

@Override
public void upload(Part part) {

    String partName = part.getName();
    Map<String, ContentBody> params = new HashMap<String, ContentBody>();
    params.put(Config.keyFile, new ByteArrayBody(part.getContent(), partName));
    post(params);/*from   ww w .j av  a2 s  .  c  om*/
    log.debug(partName + " uploaded.");
}

From source file:cn.clxy.codes.upload.ApacheHCUploader.java

@Override
public void upload(Part part) {

    String fileName = part.getFileName();
    Map<String, ContentBody> params = new HashMap<String, ContentBody>();
    params.put(Config.KEY_FILE, new ByteArrayBody(part.getContent(), fileName));
    post(params);//  w w w  .  j a v a2 s .c o  m
    log.debug(fileName + " uploaded.");
}

From source file:com.qcloud.CloudClient.java

public String post(String url, Map<String, String> header, Map<String, Object> body, byte[] data)
        throws UnsupportedEncodingException, IOException {
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("accept", "*/*");
    httpPost.setHeader("connection", "Keep-Alive");
    httpPost.setHeader("user-agent", "qcloud-java-sdk");
    if (header != null) {
        for (String key : header.keySet()) {
            httpPost.setHeader(key, header.get(key));
        }/*from   w w  w . j  a v  a2 s  .c  o m*/
    }

    if (false == header.containsKey("Content-Type")
            || header.get("Content-Type").equals("multipart/form-data")) {
        MultipartEntity multipartEntity = new MultipartEntity();
        if (body != null) {
            for (String key : body.keySet()) {
                multipartEntity.addPart(key, new StringBody(body.get(key).toString()));
            }
        }

        if (data != null) {
            ContentBody contentBody = new ByteArrayBody(data, "qcloud");
            multipartEntity.addPart("fileContent", contentBody);
        }
        httpPost.setEntity(multipartEntity);
    } else {
        if (data != null) {
            String strBody = new String(data);
            StringEntity stringEntity = new StringEntity(strBody);
            httpPost.setEntity(stringEntity);
        }
    }

    //        HttpHost proxy = new HttpHost("127.0.0.1",8888);
    //        mClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

    HttpResponse httpResponse = mClient.execute(httpPost);
    int code = httpResponse.getStatusLine().getStatusCode();
    return EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
}

From source file:org.overlord.sramp.governance.workflow.Multipart.java

public void post(HttpClient httpclient, URI uri, Map<String, Object> parameters)
        throws IOException, WorkflowException {
    MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    for (String key : parameters.keySet()) {
        ContentBody content = null;/*from  w  w  w.j ava 2  s.  c o m*/
        Object param = parameters.get(key);
        if (param instanceof String) {
            StringBody stringBody = new StringBody((String) param, "text/plain", Charset.forName("UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$
            content = stringBody;
        } else {
            //turn object into byteArray, or it also supports InputStreamBody or FileBody
            ByteArrayBody byteBody = new ByteArrayBody(null, key);
            content = byteBody;
        }
        multiPartEntity.addPart(key, content);
    }
    HttpPost httpPost = new HttpPost(uri);
    httpPost.setEntity(multiPartEntity);
    HttpResponse response = httpclient.execute(httpPost);
    InputStream is = response.getEntity().getContent();
    String responseStr = IOUtils.toString(is);
    if (response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 201) {
        logger.debug(responseStr);
    } else {
        throw new WorkflowException(
                "Workflow ERROR - HTTP STATUS CODE " + response.getStatusLine().getStatusCode() + ". " //$NON-NLS-1$ //$NON-NLS-2$
                        + response.getStatusLine().getReasonPhrase() + ". " + responseStr); //$NON-NLS-1$
    }
    is.close();
}

From source file:org.ubicompforall.BusTUC.Speech.HTTP.java

public void sendPostByteArray(byte[] buf) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://vm-6114.idi.ntnu.no:1337/SpeechServer/sst");

    try {/*  w  w  w .j a  v a  2s .  c o m*/

        MultipartEntity entity = new MultipartEntity();
        // entity.addPart("speechinput", new FileBody((buf,
        // "application/zip"));
        entity.addPart("speechinput", new ByteArrayBody(buf, "Jun.wav"));

        httppost.setEntity(entity);
        String response = EntityUtils.toString(httpclient.execute(httppost).getEntity(), "UTF-8");
        System.out.println("RESPONSE: " + response);
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.cmd.LoginWithCredentialsCommand.java

@Override
protected String login(ApplicationContext currentContext) throws CLIException {
    File credentials = new File(pathname);
    if (!credentials.exists()) {
        throw new CLIException(REASON_INVALID_ARGUMENTS,
                String.format("File does not exist: %s", credentials.getAbsolutePath()));
    }//from w ww  .ja v a  2 s.  c o m
    if (warn) {
        writeLine(currentContext, "Using the default credentials file: %s", credentials.getAbsolutePath());
    }
    HttpPost request = new HttpPost(currentContext.getResourceUrl("login"));
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("credential",
            new ByteArrayBody(FileUtility.byteArray(credentials), APPLICATION_OCTET_STREAM.getMimeType()));
    request.setEntity(entity);
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        return StringUtility.responseAsString(response).trim();
    } else {
        handleError("An error occurred while logging: ", response, currentContext);
        throw new CLIException(REASON_OTHER, "An error occurred while logging.");
    }
}

From source file:com.sat.sonata.MenuActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection.
    switch (item.getItemId()) {
    case R.id.stop:
        Log.d("SITTING", "Inside stop case");
        //stopService(new Intent(this, SonataService.class));
        return true;
    case R.id.recognise:
        String imagePath = getIntent().getStringExtra("image");
        getIntent().removeExtra("image");
        Log.d("SITTING", imagePath);

        HttpPost postRequest = new HttpPost("http://129.31.195.224:8080/picUpload");
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        try {//from w ww .j a  v a2  s  .co m

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Bitmap bitmap;
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = BitmapFactory.decodeFile(imagePath, options);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();
            ByteArrayBody bab = new ByteArrayBody(data, "music.jpg");

            reqEntity.addPart("music", bab);

            postRequest.setEntity(reqEntity);
            HttpPost[] posts = new HttpPost[1];
            posts[0] = postRequest;

            GetImageTask getImageTask = new GetImageTask();
            getImageTask.execute(posts);

        } catch (Exception e) {
            Log.v("Exception in Image", "" + e);
            //                    reqEntity.addPart("picture", new StringBody(""));
        }

    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:com.qcloud.CloudClient.java

public String postfiles(String url, Map<String, String> header, Map<String, Object> body, byte[][] data,
        String[] pornFile) throws UnsupportedEncodingException, IOException {
    HttpPost httpPost = new HttpPost(url);
    httpPost.setHeader("accept", "*/*");
    httpPost.setHeader("user-agent", "qcloud-java-sdk");
    if (header != null) {
        for (String key : header.keySet()) {
            httpPost.setHeader(key, header.get(key));
        }/*from w  ww.  java  2  s  .  com*/
    }

    if (false == header.containsKey("Content-Type")
            || header.get("Content-Type").equals("multipart/form-data")) {
        MultipartEntity multipartEntity = new MultipartEntity();
        if (body != null) {
            for (String key : body.keySet()) {
                multipartEntity.addPart(key, new StringBody(body.get(key).toString()));
            }
        }

        if (data != null) {
            for (int i = 0; i < data.length; i++) {
                ContentBody contentBody = new ByteArrayBody(data[i], pornFile[i]);
                multipartEntity.addPart("image[" + Integer.toString(i) + "]", contentBody);
            }
        }
        httpPost.setEntity(multipartEntity);
    }

    //        HttpHost proxy = new HttpHost("127.0.0.1",8888);
    //        mClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

    HttpResponse httpResponse = mClient.execute(httpPost);
    int code = httpResponse.getStatusLine().getStatusCode();
    return EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
}