gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:13:38 27 4
gpt4 key购买 nike

我正在从 jsp 保存图像文件并在 Controller 中重命名它

问题是同一段代码在 Controller 的一个部分工作,但在 Controller 的另一部分不起作用

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

这是 Controller 按预期工作的部分:-

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

}

}

下面是 Controller 不工作并显示错误的部分:-

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

Controller 代码

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

}

}

最佳答案

您失踪了enctype="multipart/form-data"在你的<form:form...>标签。

由于您的表单没有 enctype="multipart/form-data" Spring 来了<form:input type="file"..String并在无法转换时抛出错误 StringMultipartFile对于 studentPhoto类型 MultipartFileStudent类。

这是完整的source code .

关于java - 无法将类型 [java.lang.String] 的值转换为属性所需的类型 [org.springframework.web.multipart.MultipartFile],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com