1

i am getting multipart entity from android client as shown below.

       HttpClient httpClient = new DefaultHttpClient();

        HttpPost postRequest = new HttpPost(
        "http://localhost:9090/MBC_WS/rest/network/mobileUserPictureInsert1");


        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("message", new StringBody("hi moni"));

        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);

In jersey i am trying to retrieve message but getting only object.the code is:

 @Path("/mobileUserPictureInsert1")
 @POST
 @Consumes("multipart/*")

public String create(MultiPart multiPart){
     BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity();
     String message = bpe.toString();

here i AM getting some object ony not message value. what mistake i made.pl help me.

mmathan
  • 273
  • 1
  • 5
  • 13

1 Answers1

1

Yes the the right result. toString() will just use Object.toString(), which will result in

 getClass().getName() + '@' + Integer.toHexString(hashCode())

which is most likely what you're seeing. Unless BodyEntityPart overrides the toString(), which it doesn't. You should instead be getting the InputStream with BodyEntityPart.getInputStream(). Then you can do whatever with the InputStream.

A simple example:

@POST
@Consumes("multipart/*")
public String create(MultiPart multiPart) throws Exception {
    String message;
    try (BodyPartEntity bpe 
                = (BodyPartEntity) multiPart.getBodyParts().get(0).getEntity()) {
        message = getString(bpe.getInputStream());
    }
    return message;
}

private String getString(InputStream is) throws Exception {
    StringBuilder builder = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
        String line;
        while((line = reader.readLine()) != null) {
            builder.append(line).append("\n");
        }
    }
    return builder.toString();
} 

On another note: You are already using the Jersey multipart support, you can make life easier and just use its annotation support. For instance, you can just do

@POST
@Consumes("multipart/*")
public String create(@FormDataParam("message") String message){
    return message;
}

That is much easier. The @FormDataParam("message") gets the body name that you defined here:

reqEntity.addPart("message", new StringBody("hi moni"));

and converts to to String. As long as there's a MessageBodyReader available for the Content-Type of the body part, it should be able to be auto-converted.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • thanks peeskillet... the first method works out very well. but @FormDataParam is not working since in android i am not using form.just using multipartentity. getting following error while running that. – mmathan Jan 06 '15 at 04:19
  • That's odd. I actually tested this with your exact client code – Paul Samsotha Jan 06 '15 at 04:41
  • i m getting error while running the application itself. the error is:SEVERE: StandardWrapper.Throwable com.sun.jersey.spi.inject.Errors$ErrorMessagesException at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170) at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136) at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771) etc..... – mmathan Jan 06 '15 at 05:24
  • That's odd. That error is showing Jersey 1 classes. You tagged Jersey 2. Look at the very last link in my answer. At the bottom, you should see the Multipart dependency. Are you sure you are using Jersey 2? The `com.sun.jersey` classes are Jersey 1 – Paul Samsotha Jan 06 '15 at 05:28
  • sorry i m using jersey 1. – mmathan Jan 06 '15 at 05:41
  • When I have some time later I will test with Jersey 1 and give you an update. – Paul Samsotha Jan 06 '15 at 05:43
  • So I was able to reproduce the problem with Jersey 1. If you change the `@Consumes` to `MediaType.MULTIPART_FORM_DATA` it appears to work. If you look at the headers, the client request is actually being sent out as `multipart/form-data` – Paul Samsotha Jan 06 '15 at 11:11