在spring controller中上传带有其他参数的文件

时间:2018-06-12 05:18:41

标签: java angularjs spring spring-mvc

我无法将角度发布请求中的多部分文件和两个文本字段发送到弹簧控制器。

我的帖子请求如下,

$scope.data = {
            "file": $scope.uploadFile,
            "doucmentType" : $scope.documentType,
            "otherDocumentType" : $scope.otherDocumentType
    }

其中$ scope.uploadFile是文件对象,doucmnetType和otherDocumentType是我希望用文件发送到spring控制器的两个文本字段。

发布请求如下,

$http.post(appPath + '/temp/uploadAttachments',JSON.stringify($scope.data) ).success(function(result) {

    });

我的弹簧控制器如下,

@RequestMapping(value = "uploadAttachments", method = RequestMethod.POST)
@ResponseBody
public String uploadAttachments(@RequestBody ReqParam reqParam) {

    JSONObject ret= new JSONObject();
    ret.put("result", true);
    return ret.toString();

}

其中ReqParam是包含文件的getter和setter的pojo类,其他字段如

private MultipartFile file;

private String doucmentType;

private String otherDocumentType;

public String getOtherDocumentType() {
    return otherDocumentType;
}

public void setOtherDocumentType(String otherDocumentType) {
    this.otherDocumentType = otherDocumentType;
}

public String getDoucmentType() {
    return doucmentType;
}

public void setDoucmentType(String doucmentType) {
    this.doucmentType = doucmentType;
}

public MultipartFile getFile() {
    return file;
}

public void setFile(MultipartFile file) {
    this.file = file;
}

由于

1 个答案:

答案 0 :(得分:1)

您使用JSON解析和@ResponseBody以及您的自定义模型。只需使用FormData(),您需要在春季设置MultipartResolver

这是您的示例代码:

<强>角

<body ng-app = "myApp">

  <div ng-controller = "myCtrl">
     <input type = "file" file-model = "myFile"/>
     <button ng-click = "uploadFile()">upload me</button>
  </div>

  <script>
     var myApp = angular.module('myApp', []);

     myApp.directive('fileModel', ['$parse', function ($parse) {
        return {
           restrict: 'A',
           link: function(scope, element, attrs) {
              var model = $parse(attrs.fileModel);
              var modelSetter = model.assign;

              element.bind('change', function(){
                 scope.$apply(function(){
                    modelSetter(scope, element[0].files[0]);
                 });
              });
           }
        };
     }]);

     myApp.service('fileUpload', ['$https:', function ($https:) {
        this.uploadFileToUrl = function(file, uploadUrl){
           var fd = new FormData();
           fd.append('file', file);

           $https:.post(uploadUrl, fd, {
              transformRequest: angular.identity,
              headers: {'Content-Type': undefined}
           })

           .success(function(){
           })

           .error(function(){
           });
        }
     }]);

     myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
        $scope.uploadFile = function(){
           var file = $scope.myFile;

           console.log('file is ' );
           console.dir(file);

           var uploadUrl = "/fileUpload";
           fileUpload.uploadFileToUrl(file, uploadUrl);
        };
     }]);

  </script>

</body>

<强>控制器

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public @ResponseBody String uploadFile(MultipartFile file, HttpServletRequest req)
        throws SQLException {

    // using file.

    return "success";
}