[英]Spring not accepting list of multipart files: java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile
[英]Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.MultipartFile] for property
我正在從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();
}
}
您在<form:form...>
標記中缺少enctype="multipart/form-data"
。
由於您的表單沒有enctype="multipart/form-data"
Spring將<form:input type="file"..
作為String
並在無法將Student
類中MultipartFile
類型的studentPhoto
的String
轉換為MultipartFile
時拋出錯誤。
這是完整的源代碼 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.