Android-image upload issue to a REST server: Image format not supported -
i'm trying upload image android app rest server. images uploaded ,on server machine, photo viewer or image app can't open uploaded images. i'm using glassfish 3 in netbeans 7 on server machine. i'm not using maven , i'd prefer non-moven solution.
here's code use uploading images android in 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 code receive images on server machine.
@post @path("/imgs/") @consumes(mediatype.multipart_form_data) public response saveimage(@formdataparam("file") inputstream ){ 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 following jars (not @ once)in server web 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 mime type multipart/form-data , multipart/* in addbinarybody. still, images can't opened.
when add @formdataparam() ( content disposition ) displays following error:
severe: missing dependency 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 post of resource, class com.proj.test.restw, not recognized valid resource method.
when remove content disposition parameter, works fine image can't opened.
thanks in advance
try replacing "image/jpg" "image/jpeg"