filebody in java

Solutions on MaxInterview for filebody in java by the best coders in the world

showing results for - "filebody in java"
Gaëlle
21 Jun 2018
1 HttpClient httpclient = new DefaultHttpClient();
2HttpPost httppost = new HttpPost(url);
3
4FileBody bin = new FileBody(new File(fileName));
5StringBody comment = new StringBody("Filename: " + fileName);
6
7MultipartEntity reqEntity = new MultipartEntity();
8reqEntity.addPart("bin", bin);
9reqEntity.addPart("comment", comment);
10httppost.setEntity(reqEntity);
11
12HttpResponse response = httpclient.execute(httppost);
13HttpEntity resEntity = response.getEntity();
14
Matteo
15 Mar 2019
1public static void postFile(String fileName) throws Exception {//fileName is path+filename of picture
2   String url_upload_image = "http://url-to-api/upload_photo.php";
3   HttpClient client = new DefaultHttpClient();
4   HttpPost post = new HttpPost(url_upload_image);
5   MultipartEntityBuilder multipartEntity = MultipartEntityBuilder
6       .create();
7   multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
8   multipartEntity.addPart("file", new FileBody(new File(fileName)));
9   post.addHeader("id", id);//id is anything as you may need
10   post.setEntity(multipartEntity.build());
11   HttpResponse response = client.execute(post);
12   HttpEntity entity = response.getEntity();
13   entity.consumeContent();
14   client.getConnectionManager().shutdown();
15 }
16