私はAngularJSを使って、$ resourceを使ってRESTfulなWebサービス(スプリングブートで実行)と通信しています。私はいくつかのファイルをアップロードし、同じマルチパートPOSTリクエスト内のフォームフィールドを送信しようとしているが、私は次のエラーを取得しています:
"Unsupported Media Type" exception: "org.springframework.web.HttpMediaTypeNotSupportedException" message: "Content type 'application/xml' not supported" path: "/study/upload" status: 415
ここに私のビューのフォームれる:
<form method="post" enctype="multipart/form-data" ng-submit="submit()">
<table>
<tr><td>File to upload:</td><td><input type="file" name="file" ng-model="form.file"/></td></tr>
<tr><td>Name:</td><td><input type="text" name="name" ng-model="form.name"/></td></tr>
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
</table>
</form>
ここでは図であり、コントローラ:
$scope.submit=function(){
GalleryService.upload({file: $scope.form.file, name: $scope.form.name}, function(data) {
console.log("Success ... " + status);
}, function(error) {
console.log(error);
});
}
gallery.service:
(function() {
'use strict';
角 .module( 'ekellsApp') .factory( 'GalleryService'、GalleryService);
関数GalleryService($リソース、restApi){
return $resource(restApi.url + '/study/:action', {},{
save: {method: 'POST', params: {action: 'save'}},
getAll: {method: 'GET', params: {action: 'getAll'},isArray:true},
upload: {method: 'POST', params: {action: 'upload'}, transformRequest: angular.identity,headers: { 'Content-Type': undefined }}
});
}
})();ここ
と
はRESTコントローラです:@RequestMapping(value = "/upload",method = RequestMethod.POST,headers = "content-type=multipart/*")
public String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (name.contains("/")) {
redirectAttributes.addFlashAttribute("message", "Folder separators not allowed");
return "redirect:upload";
}
if (name.contains("/")) {
redirectAttributes.addFlashAttribute("message", "Relative pathnames not allowed");
return "redirect:upload";
}
if (!file.isEmpty()) {
try {
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(new File("user/bouhuila/" + name)));
FileCopyUtils.copy(file.getInputStream(), stream);
stream.close();
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + name + "!");
}
catch (Exception e) {
redirectAttributes.addFlashAttribute("message",
"You failed to upload " + name + " => " + e.getMessage());
}
}
else {
redirectAttributes.addFlashAttribute("message",
"You failed to upload " + name + " because the file was empty");
}
return "redirect:upload";
}