2012-12-14 63 views
4

我一直在收到這個錯誤,當我做我的春天mvc代碼我想用spring mvc做圖像上傳我缺少什麼參數。使用Spring MVC和Html圖像上傳

org.springframework.web.util.NestedServletException: Request processing failed; 
    nested exception is java.lang.IllegalArgumentException: argument type mismatch 
java.lang.IllegalArgumentException: argument type mismatch 
... 

我的分發程序Servlet是

<context:component-scan base-package="com.ImageUploadSpring.Controller" /> 

<!-- <bean id="simpleHandler" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> --> 
    <bean 
     class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> 


    <bean id="multipartResolver" 
     class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props>    
       <prop key="/Upload.html">FileUpload</prop>     
      </props> 
     </property> 
    </bean> 
     <bean id="FileUpload" class="com.ImageUploadSpring.Controller.FileUpload"> 
     <property name="commandName" value="ImageUpload"/> 
     <property name="commandClass" value="com.ImageUploadSpring.Bean.UploadItem"/> 
     <property name="formView" value="ImageUpload"/> 
     <property name="successView" value="message"/> 
    </bean> 
<bean id="FileUpload" class="com.ImageUploadSpring.Controller.FileUpload"></bean> 
    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 

     <property name="prefix" value="/WEB-INF/view/" /> 
     <property name="suffix" value=".html" /> 
    </bean> 

控制器類是

public class FileUpload extends SimpleFormController{ 

@RequestMapping(value = "/Upload.html", method = RequestMethod.POST) 
protected ModelAndView onSubmit(HttpServletRequest request, 
     HttpServletResponse response, Object command, BindException errors,HttpSession session) { 
    System.out.println("inside submit method"); 
    try{ 

    UploadItem item=(UploadItem)command; 



    MultipartFile file = item.getFile(); 

    InputStream inputStream = null; 
    OutputStream outputStream = null; 
    if (file.getSize() > 0) { 
     inputStream = file.getInputStream(); 

     outputStream = new FileOutputStream("D:/UploadedFiles/Images/" 
       + file.getOriginalFilename()); 

     System.out.println(file.getOriginalFilename()); 

     int readBytes = 0; 
     byte[] buffer = new byte[8192]; 
     while ((readBytes = inputStream.read(buffer, 0, 8192)) != -1) { 

      outputStream.write(buffer, 0, readBytes); 
     } 
     outputStream.close(); 
     inputStream.close(); 
     session.setAttribute("uploadFile", "D:/UploadedFiles/Images/" 
       + file.getOriginalFilename()); 
    }  


    }catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return new ModelAndView("message"); 

} 
@Override 
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) 
throws ServletException { 

    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); 

} 

而我的HTML頁面

<form name="ImageUpload" action="/ImageUploadSpring/service/Upload.html" method="POST" enctype="multipart/form-data"> 
<div> 
Select images: 
<input type="text" id="box"/> 
<input type="file" id="UploadFile" name="UploadFile" onchange="CopyMe(this,'box');" accept="image/*" size="40" style="width: 91px;" multiple /> 
<br><br> 
<input type="submit" value="Upload" /><br><br> 
</div> 
</form> 

回答

0

在定義<input type="file">您所指定的名稱name="UploadFile"。而在您的UploadItem命令對象中,文件屬性是file(從item.getFile()中猜測)。你確定你正確地映射文件名嗎?

請參考this tutorial對Spring MVC的文件工作教程上傳

+0

我試着在UplodItem命令對象中更改名稱,但問題仍然存在,還有什麼我需要看 – ragemusker

1

試試這個:

protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="UploadFile") MultipartFile image, BindException errors,HttpSession session)