List of usage examples for org.apache.http.entity.mime MultipartEntity addPart
public void addPart(final String name, final ContentBody contentBody)
From source file:fr.ippon.wip.http.request.MultipartRequestBuilder.java
public HttpRequestBase buildHttpRequest() throws URISyntaxException { MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); try {// www . jav a 2s. co m for (DiskFileItem fileItem : files) { if (fileItem.isFormField()) multipartEntity.addPart(fileItem.getFieldName(), new StringBody(new String(fileItem.get()))); else { // FileBody fileBody = new FileBody(fileItem.getStoreLocation(), fileItem.getName(), fileItem.getContentType(), fileItem.getCharSet()); InputStreamBody fileBody = new InputStreamKnownSizeBody(fileItem.getInputStream(), fileItem.get().length, fileItem.getContentType(), fileItem.getName()); multipartEntity.addPart(fileItem.getFieldName(), fileBody); } } // some request may have additional parameters in a query string if (parameterMap != null) for (Entry<String, String> entry : parameterMap.entries()) multipartEntity.addPart(entry.getKey(), new StringBody(entry.getValue())); } catch (Exception e) { e.printStackTrace(); } HttpPost postRequest = new HttpPost(requestedURL); postRequest.setEntity(multipartEntity); return postRequest; }
From source file:org.ez.flickr.api.CommandArguments.java
public MultipartEntity getBody(Map<String, String> additionalParameters) { try {/*www . j a v a2s . c o m*/ MultipartEntity entity = new MultipartEntity(); for (Parameter param : params) { if (!param.internal) { if (param.value instanceof File) { entity.addPart(param.key, new FileBody((File) param.value)); } else if (param.value instanceof String) { entity.addPart(param.key, new StringBody((String) param.value, UTF8)); } } } for (Map.Entry<String, String> entry : additionalParameters.entrySet()) { entity.addPart(entry.getKey(), new StringBody(entry.getValue(), UTF8)); } return entity; } catch (UnsupportedEncodingException ex) { throw new UnsupportedOperationException(ex.getMessage(), ex); } }
From source file:org.casquesrouges.missing.HttpAdapter.java
@Override public void run() { handler.sendMessage(Message.obtain(handler, HttpAdapter.START)); httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, 80), new UsernamePasswordCredentials(login, pass)); HttpConnectionParams.setSoTimeout(httpClient.getParams(), 25000); try {//from www . j ava 2 s . com HttpResponse response = null; switch (method) { case GET: response = httpClient.execute(new HttpGet(url)); break; case POST: HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(data)); response = httpClient.execute(httpPost); break; case FILE: File input = new File(picFile); HttpPost post = new HttpPost(url); MultipartEntity multi = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multi.addPart("person_id", new StringBody(Integer.toString(personID))); multi.addPart("creator_id", new StringBody(creator_id)); multi.addPart("file", new FileBody(input)); post.setEntity(multi); Log.d("MISSING", "http FILE: " + url + " pic: " + picFile); response = httpClient.execute(post); break; } processEntity(response); } catch (Exception e) { handler.sendMessage(Message.obtain(handler, HttpAdapter.ERROR, e)); } ConnectionManager.getInstance().didComplete(this); }
From source file:org.apache.sling.testing.tools.sling.SlingClient.java
/** Create a node at specified path, with optional properties * @param path Used in POST request to Sling server * @param properties If not null, properties are added to the created node * @return The actual path of the node that was created *//*from w w w . ja v a 2 s . c om*/ public String createNode(String path, Map<String, Object> properties) throws UnsupportedEncodingException, IOException { String actualPath = null; final MultipartEntity entity = new MultipartEntity(); // Add Sling POST options entity.addPart(":redirect", new StringBody("*")); entity.addPart(":displayExtension", new StringBody("")); // Add user properties if (properties != null) { for (Map.Entry<String, Object> e : properties.entrySet()) { entity.addPart(e.getKey(), new StringBody(e.getValue().toString())); } } final HttpResponse response = executor .execute(builder.buildPostRequest(path).withEntity(entity).withCredentials(username, password)) .assertStatus(302).getResponse(); final Header location = response.getFirstHeader(LOCATION_HEADER); assertNotNull("Expecting " + LOCATION_HEADER + " in response", location); actualPath = locationToPath(location.getValue()); return actualPath; }
From source file:com.testmax.util.FileUtility.java
public void fileUpload(String url, String filename) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); FileBody fileContent = new FileBody(new File(filename)); try {//from w ww .j a v a 2 s . c o m StringBody comment = new StringBody("Filename: " + filename); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("file", fileContent); httppost.setEntity(reqEntity); HttpResponse response = null; try { response = httpclient.execute(httppost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpEntity resEntity = response.getEntity(); }
From source file:org.wso2.am.integration.tests.other.InvalidAuthTokenLargePayloadTestCase.java
/** * Upload a file to the given URL/* www . j a v a2s.c om*/ * * @param endpointUrl URL to be file upload * @param fileName Name of the file to be upload * @throws IOException throws if connection issues occurred */ private HttpResponse uploadFile(String endpointUrl, File fileName, Map<String, String> headers) throws IOException { //open import API url connection and deploy the exported API URL url = new URL(endpointUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); FileBody fileBody = new FileBody(fileName); MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT); multipartEntity.addPart("file", fileBody); connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue()); //setting headers if (headers != null && headers.size() > 0) { Iterator<String> itr = headers.keySet().iterator(); while (itr.hasNext()) { String key = itr.next(); if (key != null) { connection.setRequestProperty(key, headers.get(key)); } } for (String key : headers.keySet()) { connection.setRequestProperty(key, headers.get(key)); } } OutputStream out = connection.getOutputStream(); try { multipartEntity.writeTo(out); } finally { out.close(); } int status = connection.getResponseCode(); BufferedReader read = new BufferedReader(new InputStreamReader(connection.getInputStream())); String temp; StringBuilder responseMsg = new StringBuilder(); while ((temp = read.readLine()) != null) { responseMsg.append(temp); } HttpResponse response = new HttpResponse(responseMsg.toString(), status); return response; }
From source file:org.xmetdb.rest.protocol.attachments.CallableAttachmentImporter.java
protected HttpEntity createPOSTEntity(DBAttachment attachment) throws Exception { Charset utf8 = Charset.forName("UTF-8"); if ("text/uri-list".equals(attachment.getFormat())) { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("title", attachment.getTitle())); formparams.add(new BasicNameValuePair("dataset_uri", attachment.getDescription())); formparams.add(new BasicNameValuePair("folder", attachment_type.substrate.equals(attachment.getType()) ? "substrate" : "product")); return new UrlEncodedFormEntity(formparams, "UTF-8"); } else {//from w ww . ja v a 2s . c o m if (attachment.getResourceURL() == null) throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Attachment resource URL is null! "); MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, utf8); entity.addPart("title", new StringBody(attachment.getTitle(), utf8)); entity.addPart("seeAlso", new StringBody(attachment.getDescription(), utf8)); entity.addPart("license", new StringBody("XMETDB", utf8)); entity.addPart("file", new FileBody(new File(attachment.getResourceURL().toURI()))); return entity; } //match, seeAlso, license }
From source file:edu.uah.itsc.aws.RubyClient.java
public void postFile(byte[] image) throws ClientProtocolException, IOException { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); // HttpPost httppost = new // HttpPost("http://ec2-107-21-179-173.compute-1.amazonaws.com:3000/posts"); HttpPost httppost = new HttpPost(publicURL); ContentBody cb = new ByteArrayBody(image, "text/plain; charset=utf8", fname); // ContentBody cb = new InputStreamBody(new ByteArrayInputStream(image), // "image/jpg", "icon.jpg"); MultipartEntity mpentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); // mpentity.addPart(cb.); // mpentity.addPart("utf8", new // StringBody((Character.toString('\u2713')))); mpentity.addPart("post[photo]", cb); mpentity.addPart("post[content]", new StringBody(desc)); // mpentity.addPart("post[filename]", new StringBody( fname)); mpentity.addPart("post[title]", new StringBody(title)); mpentity.addPart("post[bucket]", new StringBody(bucket)); mpentity.addPart("post[user]", new StringBody(User.username)); mpentity.addPart("post[folder]", new StringBody(folder)); mpentity.addPart("commit", new StringBody("Create Post")); httppost.setEntity(mpentity);//from w w w . j a v a2 s. com String response = EntityUtils.toString(httpclient.execute(httppost).getEntity(), "UTF-8"); System.out.println(response); }