Play in action – Upload file

Play Framework - Build Modern & Scalable Web Apps with Java and Scala

                   In this article, we will see how to save student information along with the student profile pic. Let us design a form to collect student information.

Student Form With Profile Picture Upload

In the above form, we have text fields and “file” upload field to upload student picture. So the form will send a post request to the REST service with student information and the image. In the code, we are going to get the student information as JSON and the image from the request body. The sample code is given below.


/**
* This method saves the student.
* @return - The available students in map with JSON representation
*/
public Result saveStudent() {
    JsonNode json = Json.parse(request().body().asMultipartFormData().asFormUrlEncoded().get("json")[0]);
    MultipartFormData formData = request().body().asMultipartFormData();
    File logo = null;
    if (formData != null) {
       FilePart logoFilePart = formData.getFile("student_pic");
       if (logoFilePart != null) {
          logo = logoFilePart.getFile();
          Logger.debug("File Uploaded Name ::"+logoFilePart.getFilename());
    }
   }
   Logger.debug("json" + json.toString());
   Student st = new Student();
   st.setUserName(json.findPath("userName").textValue());
   st.setFirstName(json.findPath("firstName").textValue());
   st.setLastName(json.findPath("lastName").textValue());
   st.setAge(json.findPath("age").textValue());
   students.put(json.findPath("userName").textValue(), st);
   Logger.debug("The aviable students size is " + students.size());
   return ok(Json.toJson(students.values()));
 }

The sample request made to save the student information along with the profile pic is shown below.
Student Save Information Request

The source code created as part of the above example is available in GitHub.

Code to upload file using Play framework

Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: ,
Posted in Frameworks, Play

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

DZone

DZone MVB

Java Code Geeks
Java Code Geeks
%d bloggers like this: