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(@Multipa...

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.*.

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: How to upload a file in REST API
How to upload a file in REST API
toztech
https://toztech.blogspot.com/2017/09/how-to-upload-file-in-rest-api.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2017/09/how-to-upload-file-in-rest-api.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content