首页 > 解决方案 > 在spring控制器中上传带有其他参数的文件

问题描述

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

我的帖子请求如下,

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

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

post请求如下,

$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 是 pojo 类,其中包含文件的 getter 和 setter 以及其他字段,例如,

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;
}

谢谢

标签: javaangularjsspringspring-mvc

解决方案


You using JSON parse and @ResponseBody and your custom model. just use FormData() and you need settings MultipartResolver in spring.

This is sample code for you :

Angular

<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>

Controller

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

    // using file.

    return "success";
}

推荐阅读