简体   繁体   English

向Spring MVC Controller发送多部分请求时出现400错误的请求

[英]400 Bad Request When Send Multipart Request To Spring MVC Controller

I am attempting to send form data with Multipart data (Image) to Spring MVC controller, but its not calling Spring controller defined and showing 400 Bad Request error when http post call in chrome console. 我正在尝试将包含Multipart数据(图像)的表单数据发送到Spring MVC控制器,但未调用Spring控制器,并且在chrome控制台中进行http post调用时显示400 Bad Request错误。 I have referred all questions related to this in SO but i could not unlock the problem. 我已经在SO中提到了与此相关的所有问题,但是我无法解决问题。 Please find where i did mistake. 请找到我做错了的地方。

Html Code: HTML代码:

<input ng-model='file1' type="file"/>

AngularjS Code: AngularjS代码:

$scope.saveData = function (formObj) {
        $http({
            url: CONTEXT_PATH+'saveFile',
            method: "POST",
            headers: { 'Content-Type': undefined },
            transformRequest: function (data) {
                var formData = new FormData();
                formData.append("model", angular.toJson(data.model));
                formData.append("file", data.files);
                return formData;
            },
            data: { model: formObj, files: $scope.file1 }
        }).then(function (response) {
            alert(response);
        });
    };

Spring Controller Code: 弹簧控制器代码:

@RequestMapping(value = "/saveFile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public @ResponseBody String storeAd(@RequestPart("model") String adString, @RequestPart("file") MultipartFile file) throws IOException {
    System.out.println("adString > "+adString);
    return "OK";
}

Error in browser console: 浏览器控制台中的错误:

Failed to load resource: the server responded with a status of 400 (Bad Request)

Data Details in Browser: 浏览器中的数据详细信息:

Request Header:

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 24635
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvpAdhKSccplcX99W

Form Data:

model: {"emailId":"ss@ss.com",..}
file: undefined

Response Header:

Connection: close
Content-Length: 1058
Content-Type: text/html;charset=utf-8

No exception at Server Side. 服务器端也不例外。

Using below request, I was able to upload file to a controller similar to yours. 使用以下请求,我能够将文件上传到与您类似的控制器。

Form Data : 表格数据:

------WebKitFormBoundaryDhtDYn8BtyH0NdRW
Content-Disposition: form-data; name="file"; filename="dummy_file.xls"
Content-Type: application/vnd.ms-excel
------WebKitFormBoundaryDhtDYn8BtyH0NdRW--

Request Headers : 请求标题:

POST /demo/upload?data=dummy_data HTTP/1.1
Host: localhost:5005
Connection: keep-alive
Content-Length: 36563
accept: */*
Origin: http://localhost:5005
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like     Gecko) Chrome/75.0.3770.100 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryDhtDYn8BtyH0NdRW
Referer: http://localhost:5005/swagger-ui.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8

You should try changing Content-Type as above as well as add boundary. 您应该尝试如上所述更改Content-Type以及添加边界。

Do check out another solution at https://stackoverflow.com/a/39545774/1776132 请在https://stackoverflow.com/a/39545774/1776132上检查其他解决方案

Its working when i use @RequestParam instead of @RequestPart 当我使用@RequestParam而不是@RequestPart时,它的工作

@RequestMapping(value = "/saveFile")
public @ResponseBody String storeAd(@RequestParam ("model") String adString, @RequestParam ("file") MultipartFile file) throws IOException {
    System.out.println("adString > "+adString);
    return "OK";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM