【发布时间】: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