I needed to find a way to upload a file to my server from my Android application, so after searching the net I found the MultyPartEntityBuilder. I will show you both client and server.
Client:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
1: /**
2: * Executes request with file passing, different then other executions.
3: * @param url only server and servlet path
4: * @param keyOfString the key of the JSON data
5: * @param request the JSON request (example- {name: "some name", age: 27}
6: * @param keyForFile the key for the file
7: * @param file the file to upload
8: * @return String if executed correctly, else returned null.
9: */
10: public String multiPartExecute(String url, String keyOfString, String request, String keyForFile, File file)
11: {
12: HttpClient client = new DefaultHttpClient();
13: HttpPost post = new HttpPost(url);
14: MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
15: multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
16: if(file != null)
17: {
18: multipartEntity.addBinaryBody(keyForFile, file, ContentType.DEFAULT_BINARY, "name");
19: }
20: multipartEntity.addTextBody(keyOfString, request, ContentType.APPLICATION_JSON);
21: post.setEntity(multipartEntity.build());
22: HttpResponse response = null;
23: String line = "", output = "";
24: try
25: {
26: response = client.execute(post);
27: BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
28: while ((line = br.readLine()) != null)
29: {
30: output += line;
31: }
32: }
33: catch (ClientProtocolException e)
34: {
35: e.printStackTrace();
36: return null;
37: }
38: catch (IOException e)
39: {
40: e.printStackTrace();
41: return null;
42: }
43: if (response.getStatusLine().getStatusCode() != 200)
44: {
45: response.getStatusLine().getStatusCode();
46: return null;
47: }
48: HttpEntity entity = response.getEntity();
49: try
50: {
51: entity.consumeContent();
52: }
53: catch (IOException e)
54: {
55: e.printStackTrace();
56: return null;
57: }
58: client.getConnectionManager().shutdown();
59: return output;
60: }
Server:
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
1: @WebServlet("/MyServlet")
2: @MultipartConfig
3: public class MyServlet extends HttpServlet
4: {
5: private static final long serialVersionUID = 1L;
6: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
7: {
8: init(request,response);
9: String req = request.getParameter("requestKey");
10: Part filePart = request.getPart("fileKey");
11: }
12: }