How To Upload A File Inward Balance Api

This is how I define the method:
 @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) ActionStatus uploadFile(@MultipartForm FileUploadForm form); 

And together with then the implementation:
 world ActionStatus uploadFile(FileUploadForm form) {  File file = novel File(getRootDir() + File.separator + form.getFilename());   endeavour {   if (!file.exists()) {    file.createNewFile();   }    FileOutputStream fop = novel FileOutputStream(file);    fop.write(form.getData());   fop.flush();   fop.close();    if (FilenameUtils.getExtension(file.getName()).equals("zip")) {    // unzip    // croak bring upward dir    String parentDir = file.getParent();    FileUtils.unzipFile(parentDir, novel FileInputStream(file));   }   } grab (Exception e) {   // handgrip exception  } } 

Here's the FileUploadForm class:
 world shape FileUploadForm {   @FormParam("uploadedFile")  @PartType(MediaType.APPLICATION_OCTET_STREAM)  someone byte[] data;   @FormParam("filename")  @PartType(MediaType.TEXT_PLAIN)  someone String filename;   world String getFilename() {   render filename;  }   world void setFilename(String filename) {   this.filename = filename;  }   world byte[] getData() {   render data;  }   world void setData(byte[] data) {   this.data = data;  }  } 
To upload a file you lot must define ii form-data variables inwards your form: filename (text) together with uploadedFile (File). In Chrome's plugin Postman, you lot tin dismiss gear upward it inwards Body tab. Postman is swell a tool for testing REST api.

*REST dependencies come upward from : javax.ws.rs.* together with org.jboss.resteasy.annotations.providers.multipart.*.
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.