2. File upload
2.1 multi file upload
-
form
<form action="${pageContext.request.contextPath}/file/upload2.action" method="post" enctype="multipart/form-data"> Select file: <input type="file" name="images" /> <br/> Select file: <input type="file" name="images" /> <br/> Select file: <input type="file" name="images" /> <br/> <input type="submit" value="upload"/> <br/> </form>
-
controller
@RequestMapping("/upload2") public String upload2(List<MultipartFile> images) throws Exception { for (MultipartFile image : images) { System.out.println("Upload file name:" + image.getOriginalFilename()); System.out.println("Upload file stream:" + image.getInputStream()); File file = new File("D:\\xml", image.getOriginalFilename()); FileUtils.copyInputStreamToFile(image.getInputStream(), file ); } return "book"; }
3. JSON
3.1 JSON usage process analysis
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-WMNTCjG3-1634171614638)(assets/image-20211013104249364.png)]
3.2 JSON data
-
What is JSON data?
- Is a lightweight data exchange format.
- Lightweight, does not rely on any framework, any language.
-
Data classification: object, array
-
object
- key must use double quotation marks
- value requires double quotation marks except for special types. (special types: numeric, Boolean, true/false)
{ "k":"v", "k2":"v2",.... }
-
array
[ element,Element 2,.... ]
-
3.3 introduction cases
3.3.1 objectives
- Target: request JSON data and respond to JSON data
- Case: user condition query,
- Request: query criteria User
- Response: query result list < user >
- Premise: the underlying jackson of spring mvc processes json data.
3.3.2 steps
- Steps:
- Import jackson related jar package
- Write JavaBean: User
- Write controller to receive request data @ RequestBody
- Write the controller to respond to the data @ ResponseBody
- postman test
3.3.3 realization
-
Import jackson related jar package
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-so4p6il4-1634171614642) (assets / image-2021101310574374. PNG)]
-
Write JavaBean: User
package com.czxy.mvcanno.domain; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; import java.util.List; /** * @author Uncle Tong * @email liangtong@itcast.cn */ public class User { private Integer id; private String username; private String password; public User() { } public User(Integer id, String username, String password) { this.id = id; this.username = username; this.password = password; } // getter and setter }
-
Write controller to receive request data @ RequestBody
package com.czxy.mvcanno.controller; import com.czxy.mvcanno.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; /** * @author Uncle Tong * @email liangtong@itcast.cn */ @Controller @RequestMapping("/json") public class JsonController { @RequestMapping("/list") public void list(@RequestBody User user) { System.out.println(user); } }
-
Write the controller to respond to the data @ ResponseBody
package com.czxy.mvcanno.controller; import com.czxy.mvcanno.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.ArrayList; import java.util.List; /** * @author Uncle Tong * @email liangtong@itcast.cn */ @Controller @RequestMapping("/json") public class JsonController { @RequestMapping("/list") @ResponseBody public List<User> list(@RequestBody User user) { System.out.println(user); //Response data List<User> list = new ArrayList<>(); list.add(new User(1,"jack","1234")); list.add(new User(2,"rose","6666")); list.add(new User(3,"tom","loverose")); return list; } }
-
postman test
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-KOb3CU7D-1634171614644)(assets/image-20211013111008685.png)]
3.4 common notes
- @JsonIgnore is used to ignore some attributes in Java beans during json serialization~
public class User { @JsonIgnore private List<String> hobby;
- @JsonFormat format date
public class User { @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date birthday;
Own summary
Today, I learned about multi file upload, which was not mentioned yesterday. Unlike single file upload, multi file upload is accepted by list < multipartfile >,
After traversal, you can perform corresponding operations on each file.
Then I began to learn JSON. First, what is JSON? JSON is a lightweight data exchange format that does not depend on any framework and language;
The data used for exchange can be divided into: object and array. The object is wrapped with {}. The key must use double quotation marks. Except for special types (number and Boolean), the value,
Double quotation marks must be used; The array is wrapped with []. Data can be written directly in the array, separated by commas
Finally, an introductory case is written. Taking user query as an example, Jackson at the bottom of spring mvc processes JSON data and returns the results after processing.
Good, another day from work!!!!