1 Reply Latest reply on Feb 17, 2012 9:05 AM by ziggy25

    RestEasy - How do i extract binary data from a multipart MultipartInput using multipart/mixed

    ziggy25

       

      I have a MultipartInput that has two parts

      • Part1 - XML string
      • Part2 - Binary data (an image)

      Here is an example of how i extract the data from the parts

       

      @POST
      @Path("/mixedMime")
      @Consumes("multipart/mixed")
      @ResponseStatus(HttpStatus.OK)
      public String mixedMime(@Context ServletContext servletContext, MultipartInput input) throws Exception{


        
      int index = 0;
        
      String xmlText;
        
      byte[] imageData;

        
      for (InputPart part : input.getParts()) {
            index
      ++;

           
      if(index==1){
             
      //extract the xml test
              xmlText
      = part.getBodyAsString()
           
      }

           
      if(index==2){
             
      //extract the image data
              imageData
      = part.get(???????);
           
      }

        
      }
      }

      How would i extract the image data (binary data) as shown above? I am using Jboss 7.0.2.

      The client sends the data using a standalone client - Note i am not using HTML forms. Here is the relevant code from the client. Basically i add the xml file from the file system as the first part. An an image as the second part.

          HttpClient httpclient = new DefaultHttpClient();
         
      HttpPost httppost = new HttpPost("http://localhost:8080/RestTest/rest/mixedmime");

         
      Scanner scanner =
            
      new Scanner(new File("myXmlFile.xml")).useDelimiter("\\Z");
            
      String messageHeader = scanner.next();
             scanner
      .close();


         
      FileBody bin = new FileBody(new File("dexter.jpg"));
         
      StringBody header = new StringBody(messageHeader.toString());

         
      MultipartEntity reqEntity = new MultipartEntity();
          reqEntity
      .addPart("header", header);
          reqEntity
      .addPart("payload", bin);
          httppost
      .setEntity(reqEntity);

         
      HttpResponse response = httpclient.execute(httppost);  

       

       

      Thanks