无法将 [java.lang.String] 类型的值转换为属性所需的类型 [org.springframework.web.multipart.MultipartFile]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36250863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:37:15  来源:igfitidea点击:

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

javaspringjspfile-handling

提问by Md Faisal

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

我正在从 jsp 保存一个图像文件并在控制器中重命名它

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:-

这是两种情况下相同的jsp代码:-

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

    }

}

采纳答案by Sanjay Rawat

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

enctype="multipart/form-data"<form:form...>标签里不见了。

Since your form doesn't had enctype="multipart/form-data"spring was taking <form:input type="file"..as Stringand throwing error when it cannot convert Stringto MultipartFilefor studentPhotoof type MultipartFilein Studentclass.

由于您的形式不有enctype="multipart/form-data"春天正在采取<form:input type="file"..String和投掷的错误,当它不能转换StringMultipartFilestudentPhoto类型MultipartFileStudent类。

Here's the complete source code.

这是完整的源代码