08-06-2012 12:46 AM
Hi,
Please Guide me where I am going wrong to post multiple images to the server using http connection along with other data in the request body.
My code snippet to send one image first (Not working):
--------------------------------------------------
InputStream mImagen = this.getClass().getResourceAsStream("avatar_defaul
byte[] imgData = IOUtilities.streamToBytes(mImagen);
String message1 = "";
message1 += "AaB03x\r\n";
message1 += "Content-Disposition: form-data; name=\"avatar\"; filename=\"avatar_default.png\"\r\n";
message1 += "Content-Type: image/png\r\n";
message1 += "\r\n";
String reqStr = "http://myurl/updateProfileDetails.json?data1=smil
s = (StreamConnection) Connector.open(reqStr+";deviceside=true",Connector
httpConn = (HttpConnection) s;
httpConn.setRequestMethod(HttpConnection.POST);
httpConn.setRequestProperty("Content-Disposition", "form-data");
httpConn.setRequestProperty("Content-Type","multip
httpConn.setRequestProperty("Content-Length", String.valueOf(imgData.length + message1.length())); httpConn.setRequestProperty("Accept", "application/json");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Authorization", "Basic " + authHeader);
os = httpConn.openOutputStream();
os.write(message1.getBytes());
os.write(imgData, 0, imgData.length);
os.flush();
os.close();
Solved! Go to Solution.
08-06-2012 03:54 AM
08-06-2012 05:19 AM
No Error, but the database is not updated. I mean probably the server is not receiving the image data to store.
08-06-2012 05:34 AM
08-06-2012 05:40 AM
I suggest you check your requests with a sniffer.
That way you would know what was sent and what was the respond.
It might be that we are looking at the wrong direction.
E.
08-06-2012 05:53 AM
Hi sanghamitra_83,
I think you have to send all your images via Httpmultipart.
check out this link and use that class in your application. i have already used this class to send one images to server, and it works fine.
_________________________________
feel free to press the kudos button on the left side to thank the user that helped you.
please mark posts as solved if you found a solution.
08-06-2012 09:50 AM
Hi
I have tried the same code before too. It works fine for other the data except the image.
it shows a success message as server response, but actually the image doesn't get stores in the database.
Can you guess where could be the issue.
Thanks,
08-07-2012 03:42 AM
Hi,
I realized that before sending the image base64encode was required.
Thanks for the advice anyway.