2016-03-27 57 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

相關問題