2016-03-27 58 views
1

我从节约JSP的图像文件,并在控制器无法将类型的值java.lang.String中]所需类型[org.springframework.web.multipart.MultipartFile]财产

的重命名问题是,同一段代码是工作在控制器的一个组成部分,并在控制器

这里的另一部分不工作是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 

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

    } 

} 
+0

您是否对两个控制器使用相同的表单?如果不是那么粘贴两个jsp表单。 –

+0

不,他们是在不同的形式..我尝试在一个,它在那里工作,然后我把它移到另一个它不工作 –

+0

粘贴一个不工作太。 –

回答

5

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

因为你的形式不具有enctype="multipart/form-data"春天正在采取<form:input type="file"..String和投掷错误,当它不能在Student类转换StringMultipartFileMultipartFilestudentPhoto

以下是完整的source code

相关问题