gpt4 book ai didi

java - 在spring Controller 中上传带有其他参数的文件

转载 作者:行者123 更新时间:2023-12-02 11:07:52 25 4
gpt4 key购买 nike

我无法将多部分文件和两个文本字段从 Angular 发布请求发送到 spring Controller 。

我的帖子请求如下,

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

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

post请求如下,

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

});

我的 Spring Controller 如下,

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

谢谢

最佳答案

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

这是给您的示例代码:

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

关于java - 在spring Controller 中上传带有其他参数的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50809715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com