【问题标题】:Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile] for property无法将类型 [java.lang.String] 的值转换为属性所需的类型 [org.springframework.web.multipart.MultipartFile]
【发布时间】:2016-07-15 00:50:09
【问题描述】:

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

问题是同一段代码在控制器的一个部分工作,而在控制器的另一部分不工作

这是两种情况下相同的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>

这是控制器按预期工作的部分:-

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

        }

    }

以下是控制器不工作并显示错误的部分:-

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

控制器代码

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

    }

}

【问题讨论】:

  • 两个控制器是否使用相同的表单?如果没有,则粘贴两个 jsp 表单。
  • 不,它们的形式不同..我试过一个,它在那里工作,然后我把它移到另一个不工作的地方
  • 粘贴那个也不起作用的。
  • 我遇到了一些与此相关的文章,但不是在这里工作的是mkyong.com/spring-mvc/…
  • 我已经粘贴了两个控制器代码第一个工作正常第二个不工作

标签: java spring jsp file-handling


【解决方案1】:

您的&lt;form:form...&gt; 标签中缺少enctype="multipart/form-data"

由于您的表单没有enctype="multipart/form-data",spring 将&lt;form:input type="file".. 视为String 并在无法将String 转换为MultipartFilestudentPhoto 类型为MultipartFile 时抛出错误Student类。

这是完整的source code

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 2021-06-13
    • 2017-05-12
    • 2018-08-21
    • 2017-04-21
    • 2021-01-23
    • 2018-10-13
    • 2017-06-14
    • 2019-06-20
    相关资源
    最近更新 更多