无法将[java.lang.String]类型的值转换为属性的必需类型[org.springframework.web.multipart.MultipartFile]
我正在从jsp保存一个图像文件,并在控制器中重命名它 问题是同一段代码在控制器的一个部分工作,而在控制器的另一个部分不工作 以下是两种情况下相同的jsp代码:-无法将[java.lang.String]类型的值转换为属性的必需类型[org.springframework.web.multipart.MultipartFile],java,spring,jsp,file-handling,Java,Spring,Jsp,File Handling,我正在从jsp保存一个图像文件,并在控制器中重命名它 问题是同一段代码在控制器的一个部分工作,而在控制器的另一个部分不工作 以下是两种情况下相同的jsp代码:- <div class="form-group "> <label for="photo">Photo:</label> <form:input type="file"
<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 = "/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”
由于您的表单没有
enctype=“multipart/form data”
spring所使用的因此,您是否对这两个控制器使用相同的表单?如果没有,那么粘贴两个jsp表单。不,它们的形式不同。我试过一个,它在那里工作,然后我把它移到另一个不工作的地方粘贴一个也不工作。我遇到了一些与此相关的文章,但这里不工作是一个我粘贴了两个控制器代码第一个工作正常第二个不工作
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();
}
}