java string to class,无法将[java.lang.String]类型的值转换为所需类型[org.springframework.web.multipart.MultipartFil...

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

here is the jsp code which is same in both cases:-

Photo:

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 roles = new ArrayList();

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

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

}

}

解决方案

You were missing enctype="multipart/form-data" in your

tag.

Since your form doesn't had enctype="multipart/form-data" spring was taking

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值