Hello, I've been trying to write the client side request to upload a file to my 
service.
My service side code is

  /**
   * Upload a module, restricted to registered users only
   */
  @POST
  @Path("/module")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  @Produces(MediaType.TEXT_PLAIN)
  public Response addModule(MultipartFormDataInput input) throws Exception
{
  //Extracting the data …
    Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
    List<InputPart> userInput = uploadForm.get("user");
    List<InputPart> pwdInput = uploadForm.get("password");
    String userName = userInput.get(0).getBody(String.class, null);
     String pwd = pwdInput.get(0).getBody(String.class, null)));
    …..
    List<InputPart> inputParts = uploadForm.get("file");
    String moduleFileName = null;
    for (InputPart inputPart : inputParts) {
      Module module = null;
      try {
        MultivaluedMap<String, String> header = inputPart.getHeaders();
        moduleFileName = getFileNameFromHeader(header);

        // convert the uploaded file to input stream
        InputStream inputStream = inputPart.getBody(InputStream.class, null);

        byte[] fileData = IOUtils.toByteArray(inputStream);
     }
    //Do stuff with file here …..
    etc. etc….

}

 I can make this work as intended from a form, but I cannot figure out how to 
make
it work from my desktop app.

What I tried to do in the client is:

    ClientRequest request = new ClientRequest(BASEURL + URL_ROOT + "/module");
    MultipartFormDataOutput formData = new MultipartFormDataOutput();
    formData.addFormData("username", "bob", MediaType.TEXT_PLAIN_TYPE);
    formData.addFormData("password", "bobspwd", MediaType.TEXT_PLAIN_TYPE);
    File fileToUpload = new File("upload.zip");
    formData.addFormData("file", fileToUpload, 
MediaType.APPLICATION_OCTET_STREAM_TYPE);
    request.body(MediaType.MULTIPART_FORM_DATA, formData);
    ClientResponse response = request.post(Response.class);

But I get a Server 500 error back. In debug, I can see that the structure of 
the incoming MultipartFormDataInput input is not right,
generating NullPointerException errors.

But I cannot figure out how to set up the ClientRequest correctly.
Thanx
Gaby


--
Please consider the environment before printing this email.
NIWA is the trading name of the National Institute of Water & Atmospheric 
Research Ltd.

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnmore_123012
_______________________________________________
Resteasy-users mailing list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users

Reply via email to