HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); FileBody bin = new FileBody(new File(fileName)); StringBody comment = new StringBody("Filename: " + fileName); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("bin", bin); reqEntity.addPart("comment", comment); httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity();
MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("someParam", "someValue"); reqEntity.addPart("someFile", new FileBody("/some/file")); .... httpost.setEntity(reqEntity);
HttpPost req = new HttpPost(composeTargetUrl()); MultipartEntity entity = new MultipartEntity(); entity.addPart(POST_IMAGE_VAR_NAME, new FileBody(toUpload)); try { entity.addPart(POST_SESSION_VAR_NAME, new StringBody(uploadSessionId)); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } req.setEntity(entity);
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(uploadUrlReturnedFromStep2); FileBody fileBody = new FileBody(thumbnailFile); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("file", fileBody); httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httppost)
private MultipartEntity entity = new MultipartEntity(); entity.addPart(FILE_PART_NAME, new FileBody(mFilePart)); try entity.addPart(STRING_PART_NAME, new StringBody(mStringPart));
HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); FileBody uploadFilePart = new FileBody(uploadFile); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("upload-file", uploadFilePart); httpPost.setEntity(reqEntity); HttpResponse response = httpclient.execute(httpPost);
httppost = new HttpPost(URL); MultipartEntity entity = new MultipartEntity(); entity.addPart("title", new StringBody("position.csv", Charset.forName("UTF-8"))); File myFile = new File(Environment.getExternalStorageDirectory(), file); FileBody fileBody = new FileBody(myFile); entity.addPart("file", fileBody); httppost.setEntity(entity); httppost.getParams().setParameter("project", id);
HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); MultipartEntity entity = new MultipartEntity(); entity.addPart("file", new FileBody(file)); post.setEntity(entity); HttpResponse response = client.execute(post); // ...
MultipartEntity reqEntity = new MultipartEntity(); try { reqEntity.addPart("email", new StringBody(email)); reqEntity.addPart("image", new FileBody(new File(imagePath))); reqEntity.addPart("img_desc", new StringBody(img_desc)); reqEntity.addPart("amount", new StringBody(amount)); reqEntity.addPart("request_type", new StringBody("INSERT")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
MultipartEntity multipart = new MultipartEntity(); File file = new File("/filepath"); // File with some location (filepath) Charset chars = Charset.forName("UTF-8"); // Setting up the encoding FileBody fileB = new FileBody(file); // Create a new FileBody with the above mentioned file multipart.addPart("data", fileB); // Add the part to my MultipartEntity. "data" is parameter name for the file StringBody stringB; // Now lets add some extra information in a StringBody try { stringB = new StringBody("I am the caption of the file",chars); // Adding the content to the StringBody and setting up the encoding multipart.addPart("caption", stringB); // Add the part to my MultipartEntity } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpPost post = new HttpPost(url); // Setting up a HTTP Post method with the target url post.setEntity(multipart); // Setting the multipart Entity to the post method HttpResponse resp = client.execute(post); // Using some HttpClient (I'm using DefaultHttpClient) to execute the post method and receive the response
private JSON request(HttpEntityEnclosingRequestBase req, File file) throws RestException, IOException { if (file != null) { File fileUpload = file; req.setHeader("X-Atlassian-Token", "nocheck"); MultipartEntity ent = new MultipartEntity(); ent.addPart("file", new FileBody(fileUpload)); req.setEntity(ent); } return request(req); }
public String uploadFile(String url, String paramName, File fileToUpload) { String response = null; try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE); // For File parameters entity.addPart(paramName, new FileBody((File) fileToUpload)); post.setEntity(entity); // Here we go! response = EntityUtils.toString(httpClient.execute(post) .getEntity(), "UTF-8"); post.releaseConnection(); } catch (Exception e) { throw new RuntimeException(e); } return response; }
public String uploadFiles(String url, String[] paramNames, File[] fileToUploads) { String response = null; try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE); // For File parameters for (int i=0; i<paramNames.length; i++) { entity.addPart(paramNames[i], new FileBody(fileToUploads[i])); } post.setEntity(entity); // Here we go! response = EntityUtils.toString(httpClient.execute(post) .getEntity(), "UTF-8"); post.releaseConnection(); } catch (Exception e) { throw new RuntimeException(e); } return response; }
HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost("http://localhost:9001/upload.php"); File file = new File("c:/TRASH/zaba_1.jpg"); MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile = new FileBody(file, "image/jpeg"); mpEntity.addPart("userfile", cbFile); httppost.setEntity(mpEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity();
public String uploadFileWithForm(String url, String paramName, File fileToUpload, Map<String, String> formParameters) { String response = null; try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE); // For File parameters entity.addPart(paramName, new FileBody((File) fileToUpload)); // add form parameters: if (formParameters != null) { for (Entry<String, String> parameter : formParameters .entrySet()) { entity.addPart(parameter.getKey(), new StringBody(parameter.getValue())); } } post.setEntity(entity); // Here we go! response = EntityUtils.toString(httpClient.execute(post) .getEntity(), "UTF-8"); post.releaseConnection(); } catch (Exception e) { throw new RuntimeException(e); } return response; }
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(YOUR_URL); FileBody filebodyVideo = new FileBody(new File(videoPath)); StringBody title = new StringBody("Filename: " + videoPath); StringBody description = new StringBody("This is a description of the video"); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("videoFile", filebodyVideo); reqEntity.addPart("title", title); reqEntity.addPart("description", description); httppost.setEntity(reqEntity); HttpResponse response = httpclient.execute( httppost ); HttpEntity resEntity = response.getEntity( );
@Override public byte[] executeHttpRequest(String uri, LinkedHashMap<String, byte[]> entity, boolean verifyResponse) throws Exception { //NOSONAR byte[] responseDataRaw = null; method = new HttpPost(url + uri); MultipartEntity requestEntity = new MultipartEntity(); for (String key : entity.keySet()) { requestEntity.addPart(key, new ByteArrayBody(entity.get(key), null)); } method.setEntity(requestEntity); if (!Thread.currentThread().isInterrupted()) { LOG.debug("Executing request {}", method.getRequestLine()); HttpResponse response = httpClient.execute(method); try { LOG.debug("Received {}", response.getStatusLine()); int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { responseDataRaw = getResponseBody(response, verifyResponse); } else { throw new TransportException(status); } } finally { method = null; } } else { method = null; throw new InterruptedException(); } return responseDataRaw; }
MultipartEntity entity = new MultipartEntity(); entity.addPart("file", new FileBody(file)); HttpPost request = new HttpPost(url); request.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(request);
MultipartEntity entity = new MultipartEntity(); entity.addPart("foo", new InputStreamBody(new ByteArrayInputStream(fooGzippedBytes), "foo.txt")); HttpPost post = new HttpPost("http://example.com/some"); post.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); // ...
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.tumblr.com/api/write"); try { MultipartEntity entity = new MultipartEntity(); entity.addPart("type",new StringBody("yourstring")); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); } catch (ClientProtocolException e) { } catch (IOException e) { }