0

I need to create a JAX-RS 2.0 client that posts a file and a couple of parameters using MULTIPART_FORM_DATA content type. (Don't need the service, just the client) I’ve seen some examples that depend on an specific implementation, like Jersey or RESTEasy, but I’d like not to bind my code to any... in particular, to Apache CXF (I am using WAS Liberty Profile). Any ideas on how to do it? Do I have to stick to some specific classes? If so, how can I do it using Apache CXF 3.0 (Liberty uses CXF for JAX-RS 2.0)

Thanks

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
icordoba
  • 1,834
  • 2
  • 33
  • 60

2 Answers2

1

[I currently cannot comment under the already written answer]

If someone is searching for the maven dependency of IMultipartBody from the answer of Anatoly:

<dependency>
    <groupId>com.ibm.websphere.appserver.api</groupId>
    <artifactId>com.ibm.websphere.appserver.api.jaxrs20</artifactId>
    <version>1.0.39</version>
    <scope>provided</scope>
</dependency>

Thanks to andymc12 from https://github.com/OpenLiberty/open-liberty/issues/11942#issuecomment-619996093

Aaron Dietz
  • 133
  • 1
  • 6
0

You can use this example how to implement it by using jax-rs 2.0 feature: https://www.ibm.com/support/knowledgecenter/SSD28V_8.5.5/com.ibm.websphere.wlp.nd.doc/ae/twlp_jaxrs_multipart_formdata_from_html.html this is almost working example (some statements should be wrapped in try-catch block, but you'll see when'll post it to IDE.

package com.example.jaxrs;
@POST
@Consumes("multipart/form-data")
@Produces("multipart/form-data")

public Response postFormData(IMultipartBody multipartBody) {
  List <IAttachment> attachments = multipartBody.getAllAttachments();
         String formElementValue = null; 
         InputStream stream = null;
         for (Iterator<IAttachment> it = attachments.iterator(); it.hasNext();) {
              IAttachment attachment = it.next();
              if (attachment == null) {
                  continue;
              }
              DataHandler dataHandler = attachment.getDataHandler();
              stream = dataHandler.getInputStream();
              MultivaluedMap<String, String> map = attachment.getHeaders();
              String fileName = null;
              String formElementName = null;
              String[] contentDisposition = map.getFirst("Content-Disposition").split(";");
              for (String tempName : contentDisposition) {
                  String[] names = tempName.split("=");
                  formElementName = names[1].trim().replaceAll("\"", "");
                  if ((tempName.trim().startsWith("filename"))) {
                      fileName = formElementName;
                  }
              }
              if (fileName == null) {
                  StringBuffer sb = new StringBuffer();
                  BufferedReader br = new BufferedReader(new InputStreamReader(stream));
                  String line = null;
                  try {
                      while ((line = br.readLine()) != null) {
                          sb.append(line);
                      }
                  } catch (IOException e) {
                      e.printStackTrace();
                  } finally {
                      if (br != null) {
                          try {
                              br.close();
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                      }
                  }
                  formElementValue = sb.toString();
                  System.out.println(formElementName + ":" + formElementValue);
              } else {
               //handle the file as you want
               File tempFile = new File(fileName);
               ...
              }
         }
         if (stream != null) {
             stream.close();
         }
         return Response.ok("test").build();
}
Anatoly
  • 5,056
  • 9
  • 62
  • 136
  • 2
    where do i import `IMultipartBody ` from? i cant find any documentation on it – AbtPst May 09 '17 at 18:18
  • 2
    @AbtPst the package is "com.ibm.websphere.jaxrs20.multipart" - more info here: https://www.ibm.com/support/knowledgecenter/en/SSAW57_liberty/com.ibm.websphere.javadoc.liberty.doc/com.ibm.websphere.appserver.api.jaxrs20_1.0-javadoc/com/ibm/websphere/jaxrs20/multipart/IMultipartBody.html – Andy McCright Sep 10 '20 at 14:23