文件上传不适用于angular-file-upload和Spring

时间:2017-02-04 19:25:58

标签: angularjs spring http angular-file-upload

我正在努力使一个简单的文件上传成为可能,但Spring不想和我一起玩。

这是文件上传的端点 - 目前没有做很多事情:

@PostMapping(WordEmbeddingApiPaths.UPLOAD_MODEL)
@RequestMapping(method=RequestMethod.POST, consumes = {"multipart/form-data"})
public ResponseEntity<WordVectorListDto> uploadModel(
        @ModelAttribute("file") MultipartFile file,
        // @RequestBody Object obj,
        RedirectAttributes redirectAttributes) {

    LOGGER.debug("POST uploadModel");

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

我尝试了几件事,但最终都出现了不同的错误。我刚刚尝试使用@RequestBody,因为我认为这可能就是诀窍,但后来我得到一个例外:

Content type 'multipart/form-data;boundary=----WebKitFormBoundarycV8dFSvDV6U9OwJq' not supported

Content type 'multipart/form-data' not supported

取决于我刚尝试的内容。

如果我选择@RequestPart("file") MultipartFile file,我会看到

Required request part 'file' is not present

@RequestParam("file")类似。

我不知道这有多难,但我希望有人可以告诉我如何从客户端获取该文件。

下面你可以看到我发送给端点的请求:

此请求可以吗?

enter image description here

网络客户端:

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

uploader.onAfterAddingFile = function($modelFile) {

    console.info('onAfterAddingFile', $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");
        }
     );
};  

的index.html

<label class="btn btn-default btn-file" style="float:right;">
    Upload 
    <input 
        type="file" 
        style="display: none;"
        name="file"     
        multiple    
        nv-file-select                  
        uploader="uploader">
</label>

1 个答案:

答案 0 :(得分:1)

以下是我的意思:

 @RequestMapping(value="/uploadFile", method=RequestMethod.POST)
            public @ResponseBody String handleFileUpload(
                    @RequestParam("file") MultipartFile file){
                String name = "test11";
                if (!file.isEmpty()) {
                    try {
                        byte[] bytes = file.getBytes();
                        BufferedOutputStream stream =
                                new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                        stream.write(bytes);
                        stream.close();
                        return "You successfully uploaded " + name + " into " + name + "-uploaded !";
                    } catch (Exception e) {
                        return "You failed to upload " + name + " => " + e.getMessage();
                    }
                } else {
                    return "You failed to upload " + name + " because the file was empty.";
                }
            }

并且不要忘记注册多部分解析器:

@Bean
public MultipartResolver multipartResolver() {
    org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(10000000);
    return multipartResolver;
}

这里是html代码...看一下输入字段的名称/ ID ..                   要上传的文件:

    Name1: <input type="text" name="name">


    File2 to upload: <input type="file" name="file">

    Name2: <input type="text" name="name">


    <input type="submit" value="Upload"> Press here to upload the file!
</form>