This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Android and the fly likes how to upload image from android to restful webservice using jersey Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "how to upload image from android to restful webservice using jersey" Watch "how to upload image from android to restful webservice using jersey" New topic
Author

how to upload image from android to restful webservice using jersey

Suzzane Pinto
Ranch Hand

Joined: Feb 10, 2012
Posts: 213
hi..
i want to upload image to rest webservice(JAXRS) using jersey which will insert the image into my sql...
i hav successfully inserted image from rest webservice....but hav no idea of how to accept from android client...
my android code is
view plaincopy to clipboardprint?
public class AndroidPostImageActivity extends Activity {
Bitmap bm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
// bm = BitmapFactory.decodeResource(getResources(),
// R.drawable.forest);
bm = BitmapFactory.decodeFile("/sdcard/Blue hills.jpg");
executeMultipartPost();
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
}

public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://10.0.2.2:8080/RestWebserviceDemo/rest/person");
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
// File file= new File("/mnt/sdcard/forest.png");
// FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
//reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
}


need to take image as multipart data and insert in to database.
looking for suggestions
thankyou
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to upload image from android to restful webservice using jersey
 
Similar Threads
url error ,please help
how to create restful webservice using jersey to consume image from android client
how to get progress bar after selecting a view
Problem in receiving data in servlet from android client?
HttpClient and MultipartEntity vs. Jersey Multipart