How to upload a file in REST API
Tech-Today

How to upload a file in REST API


This is how I define the method:

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
ActionStatus uploadFile(@MultipartForm FileUploadForm form);

And then the implementation:

public ActionStatus uploadFile(FileUploadForm form) {
File file = new File(getRootDir() + File.separator + form.getFilename());

try {
if (!file.exists()) {
file.createNewFile();
}

FileOutputStream fop = new FileOutputStream(file);

fop.write(form.getData());
fop.flush();
fop.close();

if (FilenameUtils.getExtension(file.getName()).equals("zip")) {
// unzip
// get parent dir
String parentDir = file.getParent();
FileUtils.unzipFile(parentDir, new FileInputStream(file));
}

} catch (Exception e) {
// handle exception
}
}

Here's the FileUploadForm class:

public class FileUploadForm {

@FormParam("uploadedFile")
@PartType(MediaType.APPLICATION_OCTET_STREAM)
private byte[] data;

@FormParam("filename")
@PartType(MediaType.TEXT_PLAIN)
private String filename;

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public byte[] getData() {
return data;
}

public void setData(byte[] data) {
this.data = data;
}

}
To upload a file you must define 2 form-data variables in your form: filename (text) and uploadedFile (File). In Chrome's plugin Postman, you can set it in Body tab. Postman is great a tool for testing REST api.

*REST dependencies come from : javax.ws.rs.* and org.jboss.resteasy.annotations.providers.multipart.*.




- How To Download A File In Angular2
There are 2 ways I use to download a file with Angular2 or greater. For this example, we will use a Java REST service. The first approach would be taking advantage of the HTTP download, where the Angular side will just call a URL that will initiate the...

- How To Download A File In Soap Api
First we define the interface like this: @WebMethod ActionStatus downloadFile(@WebParam(name = "file") String file); Implementation: public ActionStatus downloadFile(String filePath) { MessageContext mc = webServiceContext.getMessageContext(); // see...

- How To Download A File Using Rest Api
Normally I create an interface when defining a REST api, this is useful when testing using Arquillian. To define a download file api we must define the method below in the interface. @GET @Path("/downloadFile") ActionStatus downloadFile(@QueryParam("file")...

- How To Zipped A File Or Directory In Java
There are times when we have to zipped a file or directory recursively in Java. This is how we do it: public static void zipFile(File file) throws IOException { byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream( file.getParent()...

- How To Download A File From The Server Using Javaee
The following snippet will accept a filename in the server's directory, set it as the content of the FacesContext for the user to download. public String downloadXMLInvoice(String fileName) { File file = new File(getXmlInvoiceDir().getAbsolutePath()...



Tech-Today








.