0

I am sending files(images , pdfs , ...) from android phone to server which is a servlet I am using the HttpClient and HttpPost . (multipart data)

Here is my code for sending the post request

String postURL = //server url;

HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(postURL);

ByteArrayBody bab = new ByteArrayBody(imageBytes,"file_name_ignored");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("source", bab);
postRequest.setEntity(reqEntity);

HttpResponse response = httpClient.execute(postRequest);

I want to send and recieve files(many files) as multipart. I can send from client bu cant send from server to client in the servlet.( in the response).

How can i do this? (client requests get files and how can server response list of files like all in one capsule).

Habib Zare
  • 1,206
  • 8
  • 17
  • Make a zip file out of them? – Wim Deblauwe Jan 08 '16 at 12:54
  • Am I correct to assume you want a client to download many files at once? If so, you can only send 1 file back per request. So if you want to send everything in 1 request, you need to create a zip file of all your files and have your servlet return that zip file. There are plenty of examples of that available. – Wim Deblauwe Jan 08 '16 at 13:00
  • yes. but a client recives files specification (files have ids,names,... refer to any project) in json . how can send json+ files ?. – Habib Zare Jan 08 '16 at 13:04

1 Answers1

0

If you are e.g. using Jersey on the server you could use : org.glassfish.jersey.media.multipart.MultiPart as response body.

@Produces("multipart/mixed")
public Response multiPartResponder(){
    MultiPart multiPart = new MultiPart();
    BodyPart bodyPart = new BodyPart(myFileInputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE);
    multiPart.bodyPart(bodyPart);
    return Response.status(OK).entity(multiPart);
}

You could then either use one body part the your JSON payload and one for your file content, or annotate your file body part with a custom header which contains your meta data.

light_303
  • 2,101
  • 2
  • 18
  • 35