6

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();

    }

}
7
  • Are you using the same Form for both the controllers? if not then paste both jsp form. Commented Mar 27, 2016 at 19:02
  • No they are in different form ..I tried in one and it was working there then I moved it to another where it is not working Commented Mar 27, 2016 at 19:03
  • Paste the one which is not working too. Commented Mar 27, 2016 at 19:04
  • I have came across some articles relating to that, but not working here is one mkyong.com/spring-mvc/… Commented Mar 27, 2016 at 19:04
  • I have pasted two controller code first one is working fine second is not working Commented Mar 27, 2016 at 19:05

1 Answer 1

30

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.