2017-04-13 54 views
0

有誰知道我爲什麼會出現上述錯誤?我不明白爲什麼?請參閱下面的代碼,並告知我可以修復此錯誤的位置。目的是將多個文件上傳到一個位置。它用於單個文件,但它看起來像formdata或ajax請求只用於接受一個文件而不是多個。我不是在PHP中這樣做,只有javascript/java。請幫忙。post error需要MultipartFile []參數不存在

function makeProgress(number){ 
     var url = getRelativeURL("web/fileUpload");   
     var formData = new FormData(); 
     formData.append('number', number); 
     fls = document.getElementById("attachmentFileUploadInput").files; //length of files... 
     console.log(fls); 
     for(j=0;j<fls.length;j++){ 
      formData.append('files[]', fls[j]); //note files[] not files 
     } 
     //formData.append('file', $('input[type=file]')[0].files[0]); 
     console.log("form data " + formData); 
     $.ajax({ 
      url : url, 
      data : formData, 
      processData : false, 
      contentType : false, 
      type : 'POST', 
      success : function(data) { 
      FileUploadVisible(true); 
      $('#attachmentModal').modal('hide') 
      $(':input','#attachmentModal').val(""); 
      $("#pbarmain").hide(); 
      $("#pbar").hide(); 
      $("#actionPlanDiv").hide(); 
      setObjectEnabled('#Upload',false); 
      }, 
      error : function(err) { 
       FileUploadErrorVisible(true); 
      } 
    }); 

     } 

@Bean(name = "multipartResolver") 
public CommonsMultipartResolver commonsMultipartResolver(){ 
    CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(); 
    commonsMultipartResolver.setDefaultEncoding("utf-8"); 
    commonsMultipartResolver.setMaxUploadSize(5000000); // 5000000 -> 5MB 
    return commonsMultipartResolver; 

}}

@RequestMapping(value = { "/fileUpload" }, method = RequestMethod.POST) 
@ResponseBody 
public String uploadFile(@RequestParam("number") String number, @RequestParam("files") MultipartFile[] files, MultipartHttpServletRequest req, HttpServletResponse res) 
{  
    for (MultipartFile file : files) { 
    try { 
     File directory = new File(UPLOADED_FOLDER + number); 
       if (! directory.exists()){ 
        directory.mkdir(); 
        } 
      byte[] bytes = file.getBytes(); 
      Path path = Paths.get(UPLOADED_FOLDER + number + "//" + file.getOriginalFilename()); 
      Files.write(path, bytes); 
      logger.info("You have successfully uploaded '" + file.getOriginalFilename() + "'"); 
      return("File Uploaded"); 


    } catch (Exception e) { 
     res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
     logger.error("Failed to upload file '" + file.getOriginalFilename() + "'", e); 
     return("File Not Uploaded"); 
    } 
} 
    return "redirect:/fileUpload"; 
} 

} 
+0

我不熟悉AJ中的文件上傳AX或Java HTTP集成,但只是看看錯誤,它表明你應該有一個MultipartFile數組作爲參數somwhere,可能來自CommonsMultipartResolver?錯誤發生在哪裏? – dodo

+0

錯誤來自Spring日誌,當我在本地使用Tomcat服務器運行它時,當我在本地執行我的代碼時,我可以選擇多個文件,然後當我點擊上傳按鈕時,它打印控制檯日誌: console .log(「form data」+ formData); 和ajax請求後的錯誤。 – RA19

回答

0

,你必須使用..

@RequestMapping(method = RequestMethod.POST, headers = ("content- 
     type=multipart/*"), produces = "application/json", consumes = "file/*") 
public String uploadFile(@RequestParam("number") String number, @RequestParam("files") MultipartFile files) { 

,還可以使用的contentType:「多部分/格式數據轉換爲JavaScript代碼

 url : url, 
     data : formData, 
     processData : false, 
     contentType: 'multipart/form-data', 
     type : 'POST', 
     success : function(data) { 
+0

@RequestMapping(value = {「/ fileUpload」},method = RequestMethod.POST,headers =(「content-type = multipart/*」),產生=「application/json」,consumes =「file/*」) @ResponseBody 公共字符串uploadFile(@RequestParam( 「數字」)的字符串號碼,@RequestParam( 「文件」)MultipartFile []的文件,MultipartHttpServletRequest REQ,HttpServletResponse的RES) {\t \t \t 這 沒有工作,我增加了的contentType以及 – RA19

+0

無法解析多部分servlet請求;嵌套的異常是org.apache.commons.fileupload.FileUploadException:請求被拒絕,因爲沒有找到多部分邊界 – RA19

+0

嘗試此代碼'headers:{'Content-Type':'multipart/form-data'}'成爲ajax – Sharma

相關問題