I am trying to send a photo over http to my server. So i convert the image to bytes and then i send it across as a name value pair. Here is my code below for doing so. Now my trouble is the server side, how can i recreate and store the image from the string of bytes recieved
i also am using java servlets
Code on android
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pinnedV.getPhoto().compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("photo",new String(b)));
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(URL);
try {
request.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Code on server
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fromClientphoto= request.getParameter("photo");
byte[] b = fromClientphoto.getBytes();
FileOutputStream fos = new FileOutputStream("D:\\img.png");
fos.write(b);
fos.close();
}
This above code writes a file but it will not open as an image. also is this byte[] b = fromClientphoto.getBytes();
the correct way to convert back to same bytes as on the android phone? any ideas?