post error必需的MultipartFile []参数不存在

时间:2017-04-13 17:41:40

标签: javascript java arrays ajax jquery-file-upload

有谁知道我为什么会收到上述错误?我不明白为什么?请参阅下面的代码并告知我可以在哪里修复此错误。目的是将多个文件上传到某个位置。它曾用于单个文件,但看起来formdata或ajax请求仅用于接受一个文件而不是多个文件。我不是在PHP中这样做,只是javascript / java。请帮忙。

    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";
}

}

1 个答案:

答案 0 :(得分: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:&#39; multipart / form-data到javascript代码

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