首页 > 解决方案 > 将多个文件传递给 Spring Controller 时文件大小为零

问题描述

我正在尝试使用 AngularJS 和 Spring MVC 发布一个文件(单个文件或多个文件)以及一些 JSON 数据。我尝试如下所示:

JS:

(function () {
    'use strict';
    var myApp = angular.module('app');
    myApp.controller('filesWithJSONController', function ($scope, fileUploadService) {

        $scope.uploadFile = function () {
            var file = $scope.myFile;
            var uploadUrl = myApplnURL + '/showInfo/getInformationTest';", //Url of web service

               var fd=new FormData();
                angular.forEach($scope.files,function(file){
                       fd.append('file',file);
                  });
                 fd.append('properties', new Blob([JSON.stringify({
                "name": "root",
                "password": "root"                    
            })], {
                type: "application/json"
            }));
            promise = fileWithJSONService.sendInformation(fd,uploadUrl);

            promise.then(function (response) {
                $scope.serverResponse = response;
            }, function () {
                $scope.serverResponse = 'An error has occurred';
            })
        };
    });

})();

(function () {
    'use strict';
    var myApp = angular.module('app');
    myApp.service('fileWithJSONService', function ($http, $q) {

        this.sendInformation = function (fd, uploadUrl) {
            var deffered = $q.defer();
        var config = {
              headers : {
                'Content-Type': undefined
            }
        }
        $http.post(uploadUrl, fd, config).then(function (response) {
          console.log("response " + response);
        }, function (errResponse) {
            console.error('Error in request' + errResponse);
            deferred.reject(errResponse);
        });
            ...

弹簧控制器:

@Controller
@RequestMapping("/showInfo")
public class InfoController{
@RequestMapping(value = "/getInformationTest", method = RequestMethod.POST, consumes = {"multipart/form-data"})
    @ResponseBody
    public String sendInformationTest(@RequestPart("properties") ConnectionProperties properties,  @RequestPart("file") List<MultipartFile> multiPartFileList){

 System.out.println("In spring controller");
 //business logic
}

使用上面的代码,它multiPartFileList在 Spring Controller 中将大小显示为零。但是,如果我将代码更改为只获取一个文件而不是多个文件,则它会成功显示文件信息。有输入吗?

标签: javaangularspring-mvcmultipartform-data

解决方案


尝试:

var fd = new FormData();
fd.append('file', file);//replace with forEach
$http.post(uploadUrl, fd, {
   transformRequest: angular.identity,//overrides Angular's default serialization, leaving our data intact.
   headers: {'Content-Type': undefined}//lets the browser detect the correct Content-Type as multipart/form-data, and fill in the correct boundary.
})
.success(function(){})
.error(function(){});

后端 - 春天:

@RequestMapping(value ="/upload", method = RequestMethod.POST)
public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile[] files){
         //add the others params & logic
    }

推荐阅读