Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile] for property

Md Faisal picture Md Faisal · Mar 27, 2016 · Viewed 23.7k times · Source

I am saving an image file from jsp and renaming it in the controller

The problem is that same piece of code is working in one part of the controller and not working in another part of the controller

here is the jsp code which is same in both cases:-

<div class="form-group ">
                                <label for="photo">Photo:</label>
                                <form:input type="file" class="filestyle" path="studentPhoto"
                                    id="studentPhoto" placeholder="Upload Photo"
                                    required="required" />
                            </div>

Here is the part of the controller where it is working as expected:-

@RequestMapping(value = "/student", params = "add", method = RequestMethod.POST)
    public String postAddStudent(@ModelAttribute @Valid Student student,
            BindingResult result, Model model) throws IOException {

        if (result.hasErrors()) {
            System.out.println(result.getAllErrors().toString());

            model.addAttribute("examination_names", ExaminationName.values());

            ArrayList<Role> roles = new ArrayList<Role>();
            roles.add(Role.STUDENT);
            model.addAttribute("roles", roles);

            return "student/add";
        } else {

            System.out.println("Inside postAddStudent");
            System.out.println(student);
            student = studentService.save(student);

            String PROFILE_UPLOAD_LOCATION = servletContext.getRealPath("/")
                    + File.separator + "resources" + File.separator
                    + "student_images" + File.separator;

            BufferedImage photo = ImageIO.read(new ByteArrayInputStream(student
                    .getStudentPhoto().getBytes()));
            File destination = new File(PROFILE_UPLOAD_LOCATION
                    + student.getId() + "_photo" + ".jpg");
            ImageIO.write(photo, "jpg", destination);

            return "redirect:student?id=" + student.getId();

        }

    }

Below is the part of controller where it is not working and says error:-

Failed to convert property value of type java.lang.String to required type org.springframework.web.multipart.MultipartFile for property studentPhoto; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile] for property studentPhoto: no matching editors or conversion strategy found

ControllerCode

@RequestMapping(value = "/examForm", params = "edit", method = RequestMethod.POST)
public String postEditExamForm(@ModelAttribute @Valid Student student,
        BindingResult result, Model model) throws IOException {

    String PROFILE_UPLOAD_LOCATION = servletContext.getRealPath("/")
            + File.separator + "resources" + File.separator
            + "student_images" + File.separator;

    if (result.hasErrors()) {
        model.addAttribute("flags", Flag.values());
        return "examForm/edit";

    } else {

        Student updatedStudent = studentService.findOne(student.getId());

        updatedStudent.setDisqualifiedDescription(student
                .getDisqualifiedDescription());
        student = studentService.update(updatedStudent);


        BufferedImage photo = ImageIO.read(new ByteArrayInputStream(student
                .getStudentPhoto().getBytes()));
        File destination = new File(PROFILE_UPLOAD_LOCATION
                + student.getId() + "_photo" + ".jpg");
        ImageIO.write(photo, "jpg", destination);


        return "redirect:examForm?id=" + updatedStudent.getId();

    }

}

Answer

Sanjay Rawat picture Sanjay Rawat · Mar 29, 2016

You were missing enctype="multipart/form-data" in your <form:form...> tag.

Since your form doesn't had enctype="multipart/form-data" spring was taking <form:input type="file".. as String and throwing error when it cannot convert String to MultipartFile for studentPhoto of type MultipartFile in Student class.

Here's the complete source code.