【问题标题】:File size is zero when passing multiple files to the Spring Controller将多个文件传递给 Spring Controller 时文件大小为零
【发布时间】:2018-06-21 21:13:50
【问题描述】:

我正在尝试使用 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
}

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

【问题讨论】:

    标签: java angular spring-mvc multipartform-data


    【解决方案1】:

    尝试:

    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
        }
    

    【讨论】:

    • 我什至想将 JSON 对象与文件一起传递。上面提到的代码将起作用,当我一次传递多个文件和 JSON 对象时它不起作用,如我上面的 post js 代码所示。当我传递JSON对象和多个文件信息时,spring控制器中multiPartFileList的大小为零..
    猜你喜欢
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多