使用角度工厂服务和$ resource将Multipart上传到Spring Rest

时间:2016-04-11 16:17:03

标签: angularjs spring rest spring-boot multipart

我使用AngularJS使用$ resource与RESTful Web服务(使用spring boot运行)进行通信。我尝试上传一些文件并在同一个多部分帖子请求中发送表单字段,但我收到以下错误:

  

"不支持的媒体类型"例外:   " org.springframework.web.HttpMediaTypeNotSupportedException"信息:   "内容类型' application / xml'不支持"路径:" / study /上传"   状态:415

这是我的观点:

<form method="post" enctype="multipart/form-data" ng-submit="submit()">
  <table>
    <tr><td>File to upload:</td><td><input type="file" name="file" ng-model="form.file"/></td></tr>
    <tr><td>Name:</td><td><input type="text" name="name" ng-model="form.name"/></td></tr>
    <tr><td></td><td><input type="submit" value="Upload"  /></td></tr>
  </table>
</form>

这是视图控制器:

$scope.submit=function(){
    GalleryService.upload({file: $scope.form.file, name: $scope.form.name}, function(data) {
         console.log("Success ... " + status);
    }, function(error) {
         console.log(error);
    });
}

gallery.service:

(function() {

&#39;使用严格的&#39;;

角     .module(&#39; ekellsApp&#39;)     .factory(&#39; GalleryService&#39;,GalleryService);

函数GalleryService($ resource,restApi){

return $resource(restApi.url + '/study/:action', {},{
  save: {method: 'POST', params: {action: 'save'}},
  getAll: {method: 'GET', params: {action: 'getAll'},isArray:true},
  upload: {method: 'POST', params: {action: 'upload'}, transformRequest: angular.identity,headers: { 'Content-Type': undefined }}


});

}

})();

这是REST控制器:

@RequestMapping(value = "/upload",method = RequestMethod.POST,headers = "content-type=multipart/*")
public String handleFileUpload(@RequestParam("name") String name,
                               @RequestParam("file") MultipartFile file,
                               RedirectAttributes redirectAttributes) {
    if (name.contains("/")) {
        redirectAttributes.addFlashAttribute("message", "Folder separators not allowed");
        return "redirect:upload";
    }
    if (name.contains("/")) {
        redirectAttributes.addFlashAttribute("message", "Relative pathnames not allowed");
        return "redirect:upload";
    }

    if (!file.isEmpty()) {
        try {
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File("user/bouhuila/" + name)));
            FileCopyUtils.copy(file.getInputStream(), stream);
            stream.close();
            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded " + name + "!");
        }
        catch (Exception e) {
            redirectAttributes.addFlashAttribute("message",
                    "You failed to upload " + name + " => " + e.getMessage());
        }
    }
    else {
        redirectAttributes.addFlashAttribute("message",
                "You failed to upload " + name + " because the file was empty");
    }

    return "redirect:upload";
}

1 个答案:

答案 0 :(得分:0)

我看不清楚

 GalleryService.upload

正在做,但看起来你并没有真正将表单发送到服务器,而只是一些字段。在这种情况下

enctype="multipart/form-data"
不会使用

,这可以解释你得到的错误。

查看浏览器中的开发人员工具,哪些请求实际发送到您的服务器以及它正在使用的内容类型..