无法将[java.lang.String]类型的值转换为所需类型[org.springframework.web.multipart.MultipartFile]的属性 [英] Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile] for property

查看:6802
本文介绍了无法将[java.lang.String]类型的值转换为所需类型[org.springframework.web.multipart.MultipartFile]的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这里的jsp代码在两种情况下都相同: -

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

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

    }

}


推荐答案

您在< form:form ...>中缺少 enctype =multipart / form-data tag。

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

由于您的表单没有 enctype =multipart / form-data spring正在< form:input type =file.. as String 并抛出错误它无法将 String 转换为 MultipartFile ,用于 studentPhoto 类型 MultipartFile in Student class。

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.

这是完整的源代码

这篇关于无法将[java.lang.String]类型的值转换为所需类型[org.springframework.web.multipart.MultipartFile]的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆