views:

631

answers:

0

I do not have as good of an understanding of HTTP as I would like to. Any insight will be greatly appreciated!

I needed to upload a JSON that contained information about an object. This object could have image, audio, and video files associated with it. These data files had to be uploaded, after the JSON, as part of a multipart request.

Edit 1: Currently my HttpClient gets back an error message from the server. I don't have the server code to post; however, I figure that I must be doing something incorrectly. In addition, am I correct when I comment that I should addPart the images to the MultipartRequest after the JSON file?

Edit 2: The server is using MultipartParser, which is "pull". I think MultipartRequest is "push".

Below is the code that works correctly using HttpURLConnection (I have stripped it some to make it easier on the eyes):

String urlStr = http://theIp/etc
String boundary = "nvnvnvnvnvnvnvnvnvnvnvnvnvnvnvnvnvnvnvnvnvnv";
int bufferSize = 16384;

URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("Transfer-Encoding", "chunked");

DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

String json = generateJSON();
ArrayList<String> imgPaths = database.getImagePaths();

dos.writeBytes("--" + boundary + "\r\n");
dos.writeBytes("Content-Disposition: form-data; name=\"jsonFile\"; filename=\"json.json\"" + "\r\n");
dos.writeBytes("Content-Type: application/octet-stream" + "\r\n" + "\r\n");
dos.writeBytes(json + "\r\n");
dos.writeBytes("--" + boundary + "\r\n");

for(int i = 0; i < imgPaths.size(); i++){
 String path = imgPaths.get(i);
 String imgName = extractImgNameFromPath(path);

 dos.writeBytes("Content-Disposition: form-data; name=imgName; filename=imgName.jpg \r\n");
 dos.writeBytes("Content-Type: image/pjpeg \r\n \r\n");

 FileInputStream fStream = new FileInputStream(path);
 byte[] buffer = new byte[bufferSize];  
 while((fStream.read(buffer)) > 0) {
    dos.write(buffer);
 }

 dos.writeBytes("\r\n -- boundary \r\n");
 fStream.close();
}    

dos.flush();
dos.close();

StringBuffer reponse = new StringBuffer();
InputStream inputStream = conn.getInputStream();
byte[] data = new byte[bufferSize];
while((inputStream.read(data)) > 0) {
   response.append(data);
}

conn.disconnect();
displayStatus(reponse);

And now the code that I have thus far for HttpClient (I added the apachemime and httpmime jars for MultipartEntity):

HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://theIP/etc");
MultipartEntity entity = new MultipartEntity();

entity.addPart("jsonFile", generateJSON());
// i think that i need to add the images to the MultipartEntity here
httpPost.setEntity(entity);

HttpResponse response = httpClient.execute(httpPost, httpContext);
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();

StringBuffer response = new StringBuffer();
byte[] data = new byte[1024];
while((inputStream.read(data)) > 0) {
   response.append(data);
}

displayStatus(response);
httpClient.getConnectionManager().shutdown();