【问题标题】:MissingServletRequestPartException: Required request part 'file' is not presentMissingServletRequestPartException:所需的请求部分“文件”不存在
【发布时间】:2017-11-27 23:56:50
【问题描述】:

我一直在查看this,但似乎我的问题出在其他地方。我正在尝试上传文件。 input 目前定义为:

<input 
    type="file" 
    style="display: none;"
    name="file"     
    multiple    
    nv-file-select                  
    uploader="uploader">

这是执行上传的方式:

var uploader = $scope.uploader = new FileUploader({
    url: 'http://localhost:8080/rest-api/dl4j/we/uploadModel'
});

uploader.onAfterAddingFile = function($modelFile) {

    var fd = new FormData();        
    fd.append('file', $modelFile.file);

    $http.post($modelFile.url, fd, {
        headers: {
            'Content-Type': undefined
        },
        transformRequest: angular.identity          
    })
    .then(
        function (data) {
            alert("upload success");
        }, 
        function (data, status) {
            alert("upload error");
        }
     );
};

这是 Spring REST 端点:

@PostMapping(WordEmbeddingApiPaths.UPLOAD_MODEL)
@RequestMapping(method=RequestMethod.POST, headers={"Content-Type=multipart/form-data"})
public ResponseEntity<WordVectorListDto> uploadModel( 
        @RequestParam("file") MultipartFile file,
        RedirectAttributes redirectAttributes) {

    LOGGER.debug("POST uploadModel");

    return new ResponseEntity<WordVectorListDto>((WordVectorListDto)null, HttpStatus.OK); 
}

问题是,Spring 抛出了一个异常,告诉我参数 file 不存在:

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present

这是请求信息:

我怎样才能使这个文件上传工作?

【问题讨论】:

  • 你完成了吗?我的 Angular2 应用遇到了同样的问题

标签: angularjs spring


【解决方案1】:

我猜这可能与内容类型有关 在你的代码中我看到了这个:

$http.post($modelFile.url, fd, {
        headers: {
            'Content-Type': undefined
        },
        transformRequest: angular.identity          
    })

所以你定义了一个未定义的内容类型;你应该设置multipart/form-data 尝试把这个内容类型

希望对你有用

安杰洛

【讨论】:

  • 不,在这种情况下,内容类型设置正确 - 不确定,但我认为这是通过 Angular 以某种方式完成的。从请求中可以看出,Content-Type 标头已正确设置。
猜你喜欢
  • 2020-04-29
  • 2018-02-03
  • 2017-10-23
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 2019-08-23
  • 2019-09-08
相关资源
最近更新 更多