我正在尝试上传文件,该文件在我的本地系统上运行,但在服务器上不工作。

<form class="form-group row"  style="height:100px;" id="uploading" method="post" enctype="multipart/form-data">
  <div class="col-md-10" align="center">
    <div class="form-group row" align="center">
      <label class="col-md-2 form-control-label"> File to upload:</label>
      <div class="col-md-10" >
        <div class="input-group">
          <input type="file" class="filestyle" data-buttonName="btn-primary" name="upload" id="upload" accept="*"/>
        </div>
      </div>
    </div>
    <div class="form-group row" id="buttonzone">
      <div class="col-sm-14">
        <div class="input-group">
          <button type="submit"  class="btn btn-success"    id="upload"   style="margin-left: 96px;">
          <i class="fa fa-cloud-upload"></i> Upload</button>
          <button type="button"  class="btn btn-danger"  id="cancel"  ><i class="fa fa-ban"></i> Cancel</button>
        </div>
      </div>
    </div>
  </div>
</form>




$("form#uploading").submit(function(){
  var formData = new FormData($(this)[0]);
  $.ajax({
    url : '/uploadController/upload',
    type: 'POST',
    data: formData,
    async: false,
    beforeSend: beforeSendHandler,
    success: function (data){
      var msg=data.msg;
      var obj=data.obj;
      if(data.success == true){
        $('#successmsg').html(msg);
        $('.alert-success').show();
        $('.alert-danger').hide();
        setTimeout(function(){
          $(".alert-success").alert('close');
        }, 10000);
      }else{
        $('#errmsg').html(msg);
        $('.alert-danger').show();
        $('.alert-success').hide();
        setTimeout(function(){
          $(".alert-danger").alert('close');
        }, 10000);
      }
    },
    cache: false,
    contentType: false,
    processData: false
  });
  return false;
});


Java代码:

@RequestMapping(value = "/uploadController/upload",headers=("content-type=multipart/*"), method = RequestMethod.POST)
public @ResponseBody StatusResponse totxnsUpload(@RequestParam("upload") MultipartFile upload, HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, SecurityException{
  logger.debug(" file upload controller");
  //my logic here
  }


我在浏览器控制台中得到这个:

{
 "timestamp":1495781126083,
 "status":400,
 "error":"Bad Request",
 "exception":"org.springframework.web.bind.MissingServletRequestParameterException",
 "message":"Required MultipartFile parameter 'upload' is not present",
 "path":"/uploadController/upload"
}


但是它正在服务器之外工作,我不是问题所在。

最佳答案

@RequestParam("upload") MultipartFile upload中看到的参数“ upload”
是必填参数。如果它在某些系统中运行,则表示它正在获取名为“ upload”的参数。您的情况失败,因为请求中不存在它。

但是,您的表单中确实有一个名为upload的输入。但是我可以看到您正在尝试使用ajax发送表单数据。您可以在浏览器开发工具网络标签中看到请求吗?

还要在您的totxnsUpload方法中放置一个断点,看看是否收到两个表单提交请求(一个标准请求和一个带有ajax的请求)

出于调试目的,您可以在Java代码中将此替换@RequestParam(value = "upload", required = false) MultipartFile upload设置为上载参数为可选

话虽这么说。如果完全相同的代码在您的计算机上运行而不在服务器上运行,则可能需要配置上下文。看看这个How to use HttpServletRequest#getParts() in a servlet filter running on Tomcat?

10-05 21:18
查看更多