Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm going to assume that you know the path and filename of the image that you want to upload. Add this string to your <code>NameValuePair</code> using <code>image</code> as the key-name.</p> <p>Sending images can be done using the <a href="http://hc.apache.org/downloads.cgi" rel="noreferrer" title="HttpComponents library">HttpComponents libraries</a>. Download the latest HttpClient (currently <a href="http://apache.cs.uu.nl/dist/httpcomponents/httpclient/binary/httpcomponents-client-4.0.1-bin-with-dependencies.zip" rel="noreferrer" title="HttpClient 4.0.1 - Binary with dependencies">4.0.1</a>) binary with dependencies package and copy <code>apache-mime4j-0.6.jar</code> and <code>httpmime-4.0.1.jar</code> to your project and add them to your Java build path.</p> <p>You will need to add the following imports to your class.</p> <pre><code>import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.entity.mime.content.StringBody; </code></pre> <p>Now you can create a <code>MultipartEntity</code> to attach an image to your POST request. The following code shows an example of how to do this:</p> <pre><code>public void post(String url, List&lt;NameValuePair&gt; 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 &lt; 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(); } } </code></pre> <p>I hope this helps you a bit in the right direction.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload