I'm trying to upload an image from my Android app to a rest server. The images does get uploaded but ,on the server machine, the photo viewer or another image app can't open the uploaded images. I'm using glassfish 3 in netbeans 7 on the server machine. I'm not using maven and i'd prefer a non-moven solution.
Here's the code i use for uploading the images from android in a Runnable
@Override
public void run() {
// TODO Auto-generated method stub
try{
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
Bitmap original = BitmapFactory.decodeFile(path);
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 77, out);
byte[] data = out.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data,"image.JPG");
MultipartEntityBuilder mpeb = MultipartEntityBuilder.create();
mpeb.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
mpeb.addBinaryBody("file", data, ContentType.create("image/jpg"), "image.jpeg");
HttpPost request = new HttpPost(url);
request.setEntity(mpeb.build());
request.addHeader("Content-type","multipart/form-data");
HttpResponse response = httpClient.execute(request,httpContext);
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode()== 201){
Log.w("ced", "pic success");
}else{
Log.w("ced", "pic failed ,code "+response.getStatusLine().getStatusCode());
}
}catch(Exception e){
Log.e("error",e.getMessage());
}
}
And the code to receive the images on the server machine.
@POST
@Path("/imgs/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response saveImage(@FormDataParam("file") InputStream is
){
try {
FileOutputStream os = new FileOutputStream(new File("C:/Users/files/Downloads/"+"img.jpg"));
Utils.copyFileStream(is, os);
os.flush();
os.close();
return Response.status(201).entity("reached").build();
} catch (FileNotFoundException ex) {
Logger.getLogger(RestW.class.getName()).log(Level.SEVERE, null, ex);
}catch(IOException e){
Logger.getLogger(RestW.class.getName()).log(Level.SEVERE, null, e);
}
return Response.status(500).entity("error").build();
}
I've included the following jars (not all at once)in the server web but still nothing's changed: httpmime-4.4.1.jar httpcore-4.4.1.jar jersey-server-1.18.jar jersey-servlet-1.18.jar mimepull-1.9.3.jar jersey-media-multipart-2.0.jar jersey-core-1.18.jar
I've tried changing the mime type to multipart/form-data and multipart/* in addBinaryBody. Still, the images can't be opened.
When i add another @FormDataParam() ( for content disposition ) it displays the following error:
SEVERE: Missing dependency for method public javax.ws.rs.core.Response SEVERE: Method, public javax.ws.rs.core.Response com.proj.test.RestW.saveImage(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), annotated with POST of resource, class com.proj.test.RestW, is not recognized as valid resource method.
When i remove the content disposition parameter, it works fine but the image can't be opened.
Thanks in advance
Here's the code that worked for me
private void sendPics(ArrayList<String> paths){
for(int i = 0;i<paths.size();i++){
String p = paths.get(i);
try{
Bitmap original = BitmapFactory.decodeFile(p);
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] data = out.toByteArray();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "multipart/form-data");
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.write(data);
dataOutputStream.flush();
dataOutputStream.close();
int c = connection.getResponseCode();
connection.disconnect();
}catch(Exception e){
Log.e("cedError",e.getMessage());
}
}
}
}