使用角度js

时间:2017-04-28 09:14:05

标签: java angularjs spring web-services rest

我试图通过提交文件作为输入和其他输入文本字段来调用帖子请求,我的代码在下面 -

Java方面 -

 @RequestMapping(value = "upload",consumes = {"multipart/form-data"}, 
 headers={"Content-Type=multipart/form-data"},  produces = {"multipart/form-
 data"},  method = RequestMethod.POST)
 public AjaxResponseData<String> upload(@RequestBody RegisterModel 
 registerModel,@RequestParam(value="file_source", required = false) 
 MultipartFile file) {

 }

角度方 -

$scope.upload= function() {
    $http({
        method: 'POST',
        headers: {
            'Content-Type': 'multipart/form-data',
            'Accept': 'multipart/form-data',
        },
        data: {"Name": $scope.name,
            "address": $scope.address,
            "id": $scope.id,
            "phoneNumber": $scope.phone,
            "faxNumber": $scope.fax,
            "email": $scope.email,
            "note": $scope.note,
            "file": $scope.file_source},
            url: '/tps/register/upload'
        }) .then(function(response) { }
    }

每当我尝试使用错误代码415调用其不支持的媒体类型时。

2 个答案:

答案 0 :(得分:0)

415错误代表 Unsupported Media Type

  

请求实体具有服务器或资源所执行的媒体类型   不支持。

您正在向服务器发送JSON,但您已定义:

'Content-Type': 'multipart/form-data'

将其更改为

'Content-Type': 'application/json'

服务器端相同:

consumes = {"multipart/form-data"}

要:

consumes = {"application/json"}

答案 1 :(得分:0)

您可以参考此代码进行文件上传,因为我在我的代码中使用它。 java code:

 @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
    public URL uploadFileHandler(@RequestParam("name") String name,
                                 @RequestParam("file") MultipartFile file) throws IOException {

/******* Printing all the possible parameter from @RequestParam *************/

        System.out.println("*****************************");

        System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
        System.out.println("file.getContentType()" + file.getContentType());
        System.out.println("file.getInputStream() " + file.getInputStream());
        System.out.println("file.toString() " + file.toString());
        System.out.println("file.getSize() " + file.getSize());
        System.out.println("name " + name);
        System.out.println("file.getBytes() " + file.getBytes());
        System.out.println("file.hashCode() " + file.hashCode());
        System.out.println("file.getClass() " + file.getClass());
        System.out.println("file.isEmpty() " + file.isEmpty());

        /*************Parameters to b pass to s3 bucket put Object **************/
        InputStream is = file.getInputStream();
        String keyName = file.getOriginalFilename();
}

角色代码

var form = new FormData();
form.append("file", "image.jpeg");

var settings = {
 "async": true,
 "crossDomain": true,
 "url": "http://url/",
 "method": "POST",
 "headers": {
   "cache-control": "no-cache"
 },
 "processData": false,
 "contentType": false,
 "mimeType": "multipart/form-data",
 "data": form
}

$.ajax(settings).done(function (response) {
 console.log(response);
});