问题描述
当我将文件从Postman rest客户端上传到服务器(部署在远程计算机上的spring应用程序)时,我可以上传文件而没有任何问题.
When I am uploading a file from Postman rest client to the server(spring application deployed on a remote machine), I am able to upload the file without any issue.
但是,当我尝试在angular.js中编写一个rest客户端并发送请求时,出现400错误请求错误.我知道这是由于客户端发送的内容和服务器期望的内容之间存在语法问题.
But when I try to write a rest client in angular.js, and send over the request, I get 400 Bad Request Error. I know it's because of some syntax issue between what is send from client and what server is expecting.
服务器端代码:
@CrossOrigin(来源="*")@RequestMapping(value ="/upload",method = RequestMethod.POST,消耗= {"multipart/form-data"})
@CrossOrigin(origins = "*")@RequestMapping(value="/upload", method=RequestMethod.POST, consumes = {"multipart/form-data"})
public @ResponseBody字符串handleFileUpload(@RequestParam("files")MultipartFile file){....}
public @ResponseBody String handleFileUpload(@RequestParam("files") MultipartFile file){....}
客户端代码:
$scope.uploadFiles = function () {
alert("inside");
var request = {
method: 'POST',
url: 'http://IP ADDRESS and PORT NUMBER/upload',
data: formdata,
headers: {
'Content-Type': undefined
}
};
$http(request)
.success(function (response) {
alert("success: "+response);
})
.error(function (err) {alert("error: "+err);
});
}
推荐答案
我已经在此处上传了使用Spring和AngularJS上传文件的代码:
I have uploaded the code here for file upload using Spring and AngularJS:
https://gist.github.com/abdulrafique/9219f7164fdf5dc6dfa8da110be6a04e
这篇关于无法将文件从Angular客户端上传到Spring Java服务器:说400错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!