2014-03-30 56 views
3

從yeasterday我試圖通過ajax上傳文件到我的Spring應用程序。這是我的控制器:ajax彈出的文件上傳

@RequestMapping(value="/upload", method=RequestMethod.POST) 
@ResponseBody 
public String uploadFile( 
     @RequestParam("file") MultipartFile file, Principal principal) 
{ 
    if(principal != null) { 

    } 

    return ""; 
} 

形式的html代碼:

<form id="uploadImageForm" 
      name="uploadImageForm" 
      enctype="multipart/form-data"> 
     <div class='dialog' style='width:200px;'> 
      <div class='green_button' id='files' name='files'> 
       <div class='green_button_text' id="add_image"> 
        wybierz zdjęcie 
       </div> 
       <input type="file" name="file" id="fileInputHidden" style="display:none"/> 
      </div> 
<script> 
    $("#add_image").click(function(){ 
     $("input[id='fileInputHidden']").click(); 
    }); 
</script> 
     </div> 
    </form> 

腳本通過AJAX發送文件:

document.getElementById('fileInputHidden').addEventListener('change', this.onFileChange, false); 
}, 

this.onFileChange = function(evt) { 
    var file = evt.target.files[0]; 
    if(!file != null) { 
     if (!!file.type.match(/image.*/)) { 
      $(document.forms['uploadImageForm']).ajaxUpload({ 
       url: CONTEXT_URL + "/upload", 
       method: "POST", 
       success: function(response) { 
        $("#accept").css("opacity","1"); 
        $("#accept").click(function(){ 
         $("#ac").css("display","none"); 
        }); 
       } 
      }); 

     } else { 
      alert("Wybrany plik nie jest zdjęciem!"); 
     } 
    } 

在dispacher-servlet.xml中添加CommonsMultipartResolver豆。 當jQuery的AJAX發送POST請求,得到內部錯誤(500)和我的堆棧的樣子:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/jadenazlot] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured?] with root cause 
java.lang.IllegalArgumentException: Expected MultipartHttpServletRequest: is a MultipartResolver configured? 
    at org.springframework.util.Assert.notNull(Assert.java:112) 
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.resolveName(RequestParamMethodArgumentResolver.java:161) 
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:86) 
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79) 

我tryied到@RequestParam註釋改爲

String uploadFileHandler(@ModelAttribute("uploadImageForm") UploadedFile file) {/*...*/} 

上傳的文件是我自己的類吸氣劑和setter但是,那麼我得到空指針excepotions在file.getFile()

我做錯了什麼?

+0

您能否顯示您的multipartresolver定義? –

+1

我發現解決方案,在MultipartResolver我沒有設置maxUploadSize ... – kris14an

回答

11

MultipartResolver必須設置maxUploadSize。

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="maxUploadSize" value="50000000"/> 
</bean> 
+0

驚人的,非常感謝這篇文章。你救了我幾個小時。 –