[Common Error] org.springframework.web.multipart.MultipartException: request is not a multipart request

Check request type and content type

MultipartException The error usually occurs when the request type is not  multipart/form-data or the content type is not the expected type. Therefore, to resolve this error, you need to ensure that the request type and content type are correct. Here are some steps to check:

  1. Check request type

Make sure that the HTTP method of the request sent to the server is  POST, and the Content-Type is  multipart/form-data. The details of the request can be viewed through browser developer tools or network monitoring tools.

  1. Check content type

Make sure the uploaded file is of the expected type, for example  image/jpeg, , image/png, text/plain etc.  The expected content type can be specified using @RequestParam the or  annotation in controller methods  . @RequestPartFor example:

java@PostMapping("/upload")
public String uploadFile(@RequestParam("file") @ContentType("image/jpeg") MultipartFile file) {
// 处理文件逻辑...
return "success";
}

Through the above steps, you can ensure that the request type and content type are correct.

If you try to upload a file and the request does not contain the correct Content-Type (for example, Content-Type is application/json), a "Current request is not a multipart request" error will be thrown.

Solution:

Make sure that when trying to upload a file, the request includes the correct Content-Type. Here is an example:

POST /upload HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4WxjSAPAF5DzN

Check form configuration and front-end code

In addition to checking the request type and content type, you also need to check that the form configuration and front-end code are correct. Here are some steps to check:

  1. Check form configuration

enctype Make sure the correct attributes  are set in the HTML form  multipart/form-data. For example:

<form method="POST" enctype="multipart/form-data" action="/upload">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
  1. Check front-end code

Make sure that the file to upload is correctly selected in the front-end code and that the correct file type and file size are set. For example:

html<input type="file" name="file" accept="image/*">

Through the above steps, you can ensure that the form configuration and front-end code are correct.

Guess you like

Origin blog.csdn.net/luansj/article/details/132620952