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();
private MultipartEntity entity = new MultipartEntity(); entity.addPart(FILE_PART_NAME, new FileBody(mFilePart)); try entity.addPart(STRING_PART_NAME, new StringBody(mStringPart)); public String getBodyContentType() return entity.getContentType().getValue(); try entity.writeTo(bos);
MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("someParam", "someValue"); reqEntity.addPart("someFile", new FileBody("/some/file")); .... httpost.setEntity(reqEntity);
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
entity.addPart("address", new StringBody(failed[0].getAddress())); HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 20000); // Timeout MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("address", new StringBody("my address example")); entity.addPart("image_0", new FileBody(new File("filename of image"))); HttpPost post = new HttpPost("server address"); post.setEntity(entity); HttpResponse response = client.execute(post);
File file = new File(_filePath); MultipartEntity multipart = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multipart.addPart("file", new FileBody(file)); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse res; URI uri = new URI(url.php); HttpPost methodpost = new HttpPost(uri); methodpost.addHeader("pragma","no-cache"); methodpost.setEntity(multipart); res = httpClient.execute(methodpost); InputStream data = res.getEntity().getContent();
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 void post(String url, List<NameValuePair> nameValuePairs) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); for(int index=0; index < nameValuePairs.size(); index++) { if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) { // If the key equals to "image", we use FileBody to transfer the data entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue()))); } else { // Normal string data entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue())); } } httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost, localContext); } catch (IOException e) { e.printStackTrace(); } }
DefaultHttpClient http = new DefaultHttpClient(); SSLSocketFactory ssl = (SSLSocketFactory)http.getConnectionManager().getSchemeRegistry().getScheme( "https" ).getSocketFactory(); ssl.setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER ); final String username = "username"; BasicCredentialsProvider cP = new BasicCredentialsProvider(); cP.setCredentials(AuthScope.ANY, c); http.setCredentialsProvider(cP); HttpResponse res; try { HttpPost httpost = new HttpPost(url); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT); if(nameValuePairs.get(index).getName().equalsIgnoreCase("File")) { File file = new File(nameValuePairs.get(index).getValue()); FileBody isb = new FileBody(file,"application/*"); entity.addPart(nameValuePairs.get(index).getName(), isb); } else { entity.addPart(nameValuePairs.get(index).getName(),cb); httpost.setEntity(entity); res = http.execute(httpost); InputStream is = res.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50);
@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; }
HttpClient client = new DefaultHttpClient(); File file = new File(selectedImagePath); HttpPost post = new HttpPost(uploadUrl); post.setHeader("token", myAuthToken); post.setHeader("Content-type", "multipart/form-data; boundary=" + boundary); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, Constants.BOUNDARY, Charset.defaultCharset()); entity.addPart(Constants.MULTIPART_FORM_DATA_NAME, new FileBody(file)); post.setHeader("Accept", "application/json"); post.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary); post.setEntity(entity); HttpResponse response = client.execute(post); HttpEntity httpEntity = response.getEntity(); Log.d("Response", EntityUtils.toString(httpEntity));
public void postPicture(String path, File file) throws ParseException, IOException, XmlParseException { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost(path); MultipartEntity mpEntity = new MultipartEntity(); FileBody cbFile = new FileBody(file, "image/png"); cbFile.getMediaType(); mpEntity.addPart("userfile", cbFile); httppost.setEntity(mpEntity); HttpResponse response = httpclient.execute(httppost); }
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); entity.addPart("FileData", new FileBody(file)); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); String responseData = EntityUtils.toString(response.getEntity());
client = new DefaultHttpClient(); HttpPost request = new HttpPost(url); String BOUNDARY= "--eriksboundry--"; request.setHeader("Content-Type", "multipart/form-data; boundary="+BOUNDARY); request.addHeader("X-AUTHORIZATION",token); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset()); try { entity.addPart("file01", new StringBody(jsonObject)); entity.addPart("file01", new FileBody(file)); request.addHeader("Accept-Encoding", "gzip, deflate");
HttpPost httpost = new HttpPost("url for upload file"); MultipartEntity entity = new MultipartEntity(); entity.addPart("myIdentifier", new StringBody("somevalue")); entity.addPart("myImageFile", new FileBody(imageFile)); entity.addPart("myAudioFile", new FileBody(audioFile)); httpost.setEntity(entity); HttpResponse response; response = httpclient.execute(httpost);
if (RequestMethod.POST.equals( method )) request = new HttpPost( url.toString() ); MultipartEntity requestMultipartEntity = new MultipartEntity(); request.setHeader( "MIME-Version", "1.0" ); request.addHeader( requestMultipartEntity.getContentType() ); request.setHeader( "Content-Language", "en-US" ); request.setHeader( "Accept-Charset", "UTF-8" ); if (auth!=null) StringBody authPart = new StringBody( auth, Charset.forName( getEncoding() ) ); requestMultipartEntity.addPart( "auth", authPart ); requestMultipartEntity.addPart( "videofile", filePart ); HttpResponse response = httpClient.execute( request );
HttpClient client = new DefaultHttpClient(); client.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 ) paramValue ), "application/zip" )); // For usual String parameters entity.addPart( paramName, new StringBody( paramValue.toString(), "text/plain", Charset.forName( "UTF-8" ))); post.setEntity( entity ); // Here we go! String response = EntityUtils.toString( client.execute( post ).getEntity(), "UTF-8" ); client.getConnectionManager().shutdown();
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);
String k = entry.getKey(); String v = entry.getValue(); reqEntity.addPart(k, new StringBody(v)); FileBody filebody = new FileBody(file, "image/*"); reqEntity.addPart(fileKey, filebody); conn.addRequestProperty("Content-length", reqEntity.getContentLength() + ""); conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue()); OutputStream os = conn.getOutputStream(); reqEntity.writeTo(os); os.close(); conn.connect();
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); if (filePath != null) { File file = new File(filePath); Log.d("EDIT USER PROFILE", "UPLOAD: file length = " + file.length()); Log.d("EDIT USER PROFILE", "UPLOAD: file exist = " + file.exists()); mpEntity.addPart("avatar", new FileBody(file, "application/octet")); }