SpringMVC file upload

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

SpringMVC file upload

Json return and receive
@RequestBody
@ResponseBody

How to use @ResponseBody
1. Add a jar package that processes json
2. Support mvc annotations in spring's xml file
3. Then write @ResponseBody on the class or the surrounding
@RestController is a simplification of the annotation, which is equivalent to The combination of @ResponseBody and @Controller, placed on the class

image

image

image

Special reminder, the annotation of mvc must be added to the xml file in spring

image

Indicates read and write support for json, otherwise it cannot be read.

@RequestBody
passes in a json from the client to our controller

image

image

File upload
1. Add jar package in maven fileupload package

2. Set the file in the spring xml file and upload the configuration information. Remember to write one id name, and it is MultipartResolver

3. Upload code
Note: Uploading files does not rely on the suffix to determine the file type, and the actual file type is obtained through ContentType.

image

Static resources Resource
static files are loaded through the defaultservlet, which is specially used in the web server to process static files.
Problem: When you configure the central servlet in web.xml and configure the access character as "/", static resource requests will be sent to your servlet.
He will find the specific action for processing, and if not found, a 404 error will be reported.

Solution:
You need to configure mvc in spring's xml file. If the access character of the central servlet configuration in your web.xml file is not /, you can ignore this operation

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325390110&siteId=291194637