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

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

Introduction

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

Prototype

public FileBody(final File file) 

Source Link

Usage

From source file:com.stikyhive.stikyhive.ChattingActivity.java

public int uploadFile(final File SDFile) {
    String status = "";
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(upLoadServerUri);
    FileBody bin = new FileBody(SDFile);
    MultipartEntity reqEntity = new MultipartEntity();
    try {/*  ww w  . j  a v a  2s  .co m*/
        reqEntity.addPart("Content-Disposition", new StringBody("multipart/form-data"));
        // reqEntity.addPart("filename", new StringBody(filename));
        reqEntity.addPart("uploaded_file", bin);
        reqEntity.addPart("Content-Type", new StringBody("image/png"));
        httppost.setEntity(reqEntity);
        //Log.d("Executing Request ", httppost.getRequestLine().toString());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //dialog.show();
            }
        });
        if (resEntity != null) {
            Log.i("content length: ", resEntity.getContentLength() + "");
            if (resEntity.getContentLength() > 0) {
                fileNameRes = EntityUtils.toString(resEntity);
                Log.i(TAG, " Status ... ^^ " + status);
                return 200;
            } else {
                status = "No Response from Server";
                Log.i("Status----->", status);
                return 500;
            }
        } else {
            status = "No Response from Server";
            Log.i("Status----->", status);
            return 500;
        }
    } catch (Exception e) {
        e.printStackTrace();
        status = "Unable to connect with server";
        return 500;
    }
}

From source file:sjizl.com.ChatActivity.java

private void doFileUpload(String path) {

    String username = "";
    String password = "";
    String foto = "";
    String foto_num = "";
    SharedPreferences sp = getApplicationContext().getSharedPreferences("loginSaved", Context.MODE_PRIVATE);
    username = sp.getString("username", null);
    password = sp.getString("password", null);
    foto = sp.getString("foto", null);
    foto_num = sp.getString("foto_num", null);

    File file1 = new File(path);

    String urlString = "http://sjizl.com/postBD/UploadToServer.php?intochat=1&pid_user=" + pid_user
            + "&username=" + username + "&password=" + password;
    try {/*from ww w . ja v a 2  s .  com*/
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(urlString);
        FileBody bin1 = new FileBody(file1);

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("uploadedfile1", bin1);

        reqEntity.addPart("user", new StringBody("User"));
        post.setEntity(reqEntity);
        HttpResponse response = client.execute(post);
        resEntity = response.getEntity();
        final String response_str = EntityUtils.toString(resEntity);
        if (resEntity != null) {
            Log.i("RESPONSE", response_str);
            runOnUiThread(new Runnable() {
                public void run() {
                    try {
                        // res.setTextColor(Color.GREEN);
                        // res.setText("n Response from server : n " + response_str);

                        CommonUtilities.custom_toast(getApplicationContext(), ChatActivity.this,
                                "Upload Complete! ", null, R.drawable.iconbd);

                        new UpdateChat().execute();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    } catch (Exception ex) {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    }

    //RegisterActivity.login(username,password,getApplicationContext());
    CommonUtilities.startandsendwebsock(
            "" + pid_user + " " + pid + " " + naam + " " + foto + " " + foto_num + " message send");
}

From source file:duthientan.mmanm.com.Main.java

private void btnUploadFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUploadFileActionPerformed
    // TODO add your handling code here:
    if (!fileUploadPath.equals("")) {
        progressBarCipher.setIndeterminate(true);
        new Thread(new Runnable() {
            @Override//from  ww w  .  ja  va  2 s .  com
            public void run() {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
                            HttpVersion.HTTP_1_1);
                    HttpPost httppost = new HttpPost("http://localhost:4000/api/upload");
                    File file = new File(fileUploadPath);
                    MultipartEntity mpEntity = new MultipartEntity();
                    ContentBody cbFile = new FileBody(file);
                    mpEntity.addPart("files", cbFile);
                    httppost.addHeader("id", id);
                    httppost.addHeader("x-access-token", token);
                    httppost.setEntity(mpEntity);
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity resEntity = response.getEntity();
                    progressBarCipher.setIndeterminate(false);
                    JFrame frame = new JFrame("Upload Completed");
                    JOptionPane.showMessageDialog(frame, "File Uploaded");
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();
    } else {
        JFrame frame = new JFrame("File not Found");
        JOptionPane.showMessageDialog(frame, "Plase Choice File Upload");
    }
}

From source file:be.cytomine.client.Cytomine.java

public JSONObject uploadFile(String url, String file) throws CytomineException {
    try {/*  ww  w .j av  a2 s  .co  m*/
        HttpClient client = null;
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("files[]", new FileBody(new File(file)));

        client = new HttpClient(publicKey, privateKey, getHost());
        client.authorize("POST", url, entity.getContentType().getValue(), "application/json,*/*");
        client.connect(getHost() + url);

        int code = client.post(entity);
        String response = client.getResponseData();
        log.debug("response=" + response);
        client.disconnect();
        return createJSONResponse(code, response);
    } catch (IOException e) {
        throw new CytomineException(e);
    }
}

From source file:be.cytomine.client.Cytomine.java

/**
 * Upload and create an abstract image on the plateform (use async or sync upload depending on synchrone parameter)
 *
 * @param file         The image file path
 * @param idProject    If not null, add the image in this project
 * @param idStorage    The storage where the image will be copied
 * @param cytomineHost The URL of the Core
 * @param properties   These key-value will be add to the AbstractImage as Property domain instance
 * @param synchrone    If true, the response will be send from server when the image will be converted, transfered, created,...(May take a long time)
 *                     Otherwise the server response directly after getting the image and the parameters
 * @return A response with the status, the uploadedFile and the AbstractImage list (only if synchrone!=true)
 * @throws Exception Error during upload
 *//*from ww  w. j  a v a 2  s. com*/
public JSONArray uploadImage(String file, Long idProject, Long idStorage, String cytomineHost,
        Map<String, String> properties, boolean synchrone) throws CytomineException {
    try {
        HttpClient client = null;
        //String url = "/api/uploadedfile";

        String projectParam = "";
        if (idProject != null && idProject != 0l) {
            projectParam = "&idProject=" + idProject;
        }

        String url = "/upload?idStorage=" + idStorage + "&cytomine=" + cytomineHost + projectParam;

        if (properties != null && properties.size() > 0) {

            List<String> keys = new ArrayList<String>();
            List<String> values = new ArrayList<String>();
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                keys.add(entry.getKey());
                values.add(entry.getValue());
            }
            url = url + "&keys=" + StringUtils.join(keys, ",") + "&values=" + StringUtils.join(values, ",");
        }

        if (synchrone) {
            url = url + "&sync=" + true;
        }

        MultipartEntity entity = new MultipartEntity();

        if (idProject != null && idStorage != null) {
            entity.addPart("idProject", new StringBody(idProject + ""));
            entity.addPart("idStorage", new StringBody(idStorage + ""));
        }

        entity.addPart("files[]", new FileBody(new File(file)));

        client = new HttpClient(publicKey, privateKey, getHost());
        client.authorize("POST", url, entity.getContentType().getValue(), "application/json,*/*");
        client.connect(getHost() + url);
        int code = client.post(entity);
        String response = client.getResponseData();
        log.debug("response=" + response);
        client.disconnect();
        return createJSONArrayResponse(code, response);
    } catch (IOException e) {
        throw new CytomineException(e);
    }
}

From source file:wsserver.EKF1TimerSessionBean.java

private void testPost() {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//from   ww w. j  a  va  2 s .c om
        HttpPost httppost = new HttpPost(
                "http://localhost:8080" + "/servlets-examples/servlet/RequestInfoExample");

        FileBody bin = new FileBody(new File(""));
        StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

        HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment)
                .build();

        httppost.setEntity(reqEntity);

        System.out.println("executing request " + httppost.getRequestLine());
        try {
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                }
                try {
                    EntityUtils.consume(resEntity);
                } catch (Exception ee) {

                }
            } finally {
                try {
                    response.close();
                } catch (Exception ee) {

                }
            }
        } catch (Exception ee) {

        }

    } finally {
        try {
            httpclient.close();
        } catch (Exception ee) {

        }
    }
}

From source file:com.quwu.xinwo.release.Release_Activity.java

/**
 * ??//from w ww . j  a v  a 2s.  c  om
 * */
private String uploading(String url, List<String> path, String goods_name, String good_description,
        String audiofile, String good_region, String city_region, String small_area, String twolevel_id,
        String three_id, String goods_price, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String user_id,
        String selling_price, String freight, String whether_pack) {
    String result = null;
    HttpPost httpPost = new HttpPost(url);
    // httpPost.addHeader("Content-Type", "video/mpeg");
    // httpPost.addHeader("User-Agent", "imgfornote");
    // ?
    MultipartEntity reqEntity = new MultipartEntity();

    for (int i = 0; i < path.size(); i++) {// 
        File file1 = null;
        if (path.get(i).equals("drawable://" + R.drawable.fb_icn_video)
                || path.get(i).equals("drawable://" + R.drawable.fb_icn_carema)) {

        } else if (path.get(i).equals(CameraUtils.CAMERA_VIDEO)) {
            file1 = new File(path.get(i));
            // if (!file1.getAbsoluteFile().equals("")) {
            FileBody fileBody = new FileBody(file1);
            reqEntity.addPart("videofile", fileBody);
        } else {
            file1 = new File(path.get(i));
            // if (!file1.getAbsoluteFile().equals("")) {
            FileBody fileBody = new FileBody(file1);
            reqEntity.addPart("imagefile", fileBody);
            // }
        }
    }
    if (!audiofile.equals("")) {
        File file3 = new File(audiofile);// 
        if (!file3.getAbsoluteFile().equals("")) {
            FileBody fileBody = new FileBody(file3);
            reqEntity.addPart("audiofile", fileBody);
        }
    }
    try {
        StringBody type = new StringBody(goods_name);
        reqEntity.addPart("goods_name", type);
        StringBody type1 = new StringBody(good_description);
        reqEntity.addPart("good_description", type1);
        StringBody type2 = new StringBody(good_region);
        reqEntity.addPart("good_region", type2);
        StringBody type3 = new StringBody(city_region);
        reqEntity.addPart("city_region", type3);
        StringBody type4 = new StringBody(small_area);
        reqEntity.addPart("small_area", type4);
        StringBody type6 = new StringBody(twolevel_id);
        reqEntity.addPart("twolevel_id", type6);
        StringBody type19 = new StringBody(three_id);
        reqEntity.addPart("three_id", type19);
        StringBody type7 = new StringBody(goods_price);
        reqEntity.addPart("goods_price", type7);
        StringBody type8 = new StringBody(normal_use);
        reqEntity.addPart("normal_use", type8);
        StringBody type9 = new StringBody(warranty_period);
        reqEntity.addPart("warranty_period", type9);
        StringBody type10 = new StringBody(no_repair);
        reqEntity.addPart("no_repair", type10);
        StringBody type11 = new StringBody(brand_new);
        reqEntity.addPart("brand_new", type11);
        StringBody type12 = new StringBody(jingdong);
        reqEntity.addPart("jingdong", type12);
        StringBody type13 = new StringBody(mainland_licensed);
        reqEntity.addPart("mainland_licensed", type13);
        StringBody type14 = new StringBody(since);
        reqEntity.addPart("since", type14);
        StringBody type15 = new StringBody(user_id);
        reqEntity.addPart("user_id", type15);
        StringBody type16 = new StringBody(selling_price);
        reqEntity.addPart("selling_price", type16);
        StringBody type17 = new StringBody(freight);
        reqEntity.addPart("freight", type17);
        StringBody type18 = new StringBody(whether_pack);
        reqEntity.addPart("whether_pack", type18);

        httpPost.setEntity(reqEntity);
        // ?HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        // ?HttpResponse
        HttpResponse httpResponse;
        try {
            httpResponse = httpclient.execute(httpPost);

            // HttpStatus.SC_OK?
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // ?
                result = EntityUtils.toString(httpResponse.getEntity());
            } else {
                pd.dismiss();
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

From source file:com.quwu.xinwo.release.Release_Activity.java

/**
 * ???//from ww w .j  a  v a2  s. co  m
 * */
private String uploading1(String url, List<String> path, String goods_name, String good_description,
        String audiofile, String good_region, String city_region, String small_area, String twolevel_id,
        String three_id, String goods_price, String normal_use, String warranty_period, String no_repair,
        String brand_new, String jingdong, String mainland_licensed, String since, String user_id,
        String startauctionprice, String retainprice, String increaserange) throws ParseException, IOException {
    String result = null;
    HttpPost httpPost = new HttpPost(url);
    // httpPost.addHeader("Content-Type", "video/mpeg");
    // httpPost.addHeader("User-Agent", "imgfornote");
    // ?
    MultipartEntity reqEntity = new MultipartEntity();

    for (int i = 0; i < path.size(); i++) {// 
        File file1 = null;
        if (path.get(i).equals("drawable://" + R.drawable.fb_icn_video)
                || path.get(i).equals("drawable://" + R.drawable.fb_icn_carema)) {

        } else if (path.get(i).equals(CameraUtils.CAMERA_VIDEO)) {
            file1 = new File(path.get(i));
            // if (!file1.getAbsoluteFile().equals("")) {
            FileBody fileBody = new FileBody(file1);
            reqEntity.addPart("videofile", fileBody);
        } else {
            file1 = new File(path.get(i));
            // if (!file1.getAbsoluteFile().equals("")) {
            FileBody fileBody = new FileBody(file1);
            reqEntity.addPart("imagefile", fileBody);
            // }
        }
    }
    if (!audiofile.equals("")) {
        File file3 = new File(audiofile);// 
        if (!file3.getAbsoluteFile().equals("")) {
            FileBody fileBody = new FileBody(file3);
            reqEntity.addPart("audiofile", fileBody);
        }
    }
    StringBody type = new StringBody(goods_name);
    reqEntity.addPart("goods_name", type);
    StringBody type1 = new StringBody(good_description);
    reqEntity.addPart("good_description", type1);
    StringBody type2 = new StringBody(good_region);
    reqEntity.addPart("good_region", type2);
    StringBody type3 = new StringBody(city_region);
    reqEntity.addPart("city_region", type3);
    StringBody type4 = new StringBody(small_area);
    reqEntity.addPart("small_area", type4);
    StringBody type6 = new StringBody(twolevel_id);
    reqEntity.addPart("twolevel_id", type6);
    StringBody type19 = new StringBody(three_id);
    reqEntity.addPart("three_id", type19);
    StringBody type7 = new StringBody(goods_price);
    reqEntity.addPart("goods_price", type7);
    StringBody type8 = new StringBody(normal_use);
    reqEntity.addPart("normal_use", type8);
    StringBody type9 = new StringBody(warranty_period);
    reqEntity.addPart("warranty_period", type9);
    StringBody type10 = new StringBody(no_repair);
    reqEntity.addPart("no_repair", type10);
    StringBody type11 = new StringBody(brand_new);
    reqEntity.addPart("brand_new", type11);
    StringBody type12 = new StringBody(jingdong);
    reqEntity.addPart("jingdong", type12);
    StringBody type13 = new StringBody(mainland_licensed);
    reqEntity.addPart("mainland_licensed", type13);
    StringBody type14 = new StringBody(since);
    reqEntity.addPart("since", type14);
    StringBody type15 = new StringBody(user_id);
    reqEntity.addPart("user_id", type15);
    StringBody type16 = new StringBody(startauctionprice);
    reqEntity.addPart("startauctionprice", type16);
    StringBody type17 = new StringBody(retainprice);
    reqEntity.addPart("retainprice", type17);
    StringBody type18 = new StringBody(increaserange);
    reqEntity.addPart("increaserange", type18);
    httpPost.setEntity(reqEntity);
    // ?HttpClient
    HttpClient httpclient = new DefaultHttpClient();
    // ?HttpResponse
    HttpResponse httpResponse = httpclient.execute(httpPost);
    // HttpStatus.SC_OK?
    if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        // ?
        result = EntityUtils.toString(httpResponse.getEntity());
    } else {
        pd.dismiss();
    }
    return result;
}

From source file:org.fcrepo.test.api.TestRESTAPI.java

private MultipartEntity _doUploadPost() throws Exception {
    File temp = File.createTempFile("test.txt", null);
    FileUtils.writeStringToFile(temp, "This is the upload test file");

    FileBody part = new FileBody(temp);
    Map<String, AbstractContentBody> parts = new HashMap<String, AbstractContentBody>(1);
    parts.put("file", part);
    return _doUploadPost(parts);
}