首先,我们需要添加SpringBoot Web和Multipart依赖,其pom.xml配置如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.websocket</groupId>
<artifactId>tomcat-websocket-api</artifactId>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
其中需要注意的是,multipart支持是在SpringBoot Web中默认开启的。接下来,我们分别讲解如何接收文件和对象。
一、接收文件
我们可以通过以下代码接收文件:
@PostMapping("/upload")
public @ResponseBody String upload(@RequestParam("file") MultipartFile file) {
try {
// 文件存储在应用根目录下的upload文件夹中
File dir = new File("upload");
if (!dir.exists()) {
dir.mkdirs();
}
file.transferTo(new File(dir.getAbsolutePath(), file.getOriginalFilename()));
return "Upload Success";
} catch (IOException e) {
e.printStackTrace();
return "Upload Failed";
}
}
在上面的代码中,我们首先通过@RequestParam注解标记要接收一个名为file的文件,然后将接收到的文件保存在应用根目录下的upload文件夹中。
二、接收对象
我们可以通过以下代码接收对象:
@PostMapping("/saveUser")
public @ResponseBody String saveUser(@RequestBody User user) {
// TODO: 保存用户到数据库中
return "Save Success";
}
public class User {
private String name;
private Integer age;
// 省略getter和setter
}
在上面的代码中,我们通过@PostMapping注解标记要接收一个POST请求,然后通过@RequestBody注解将请求体中的JSON数据转换为User对象。这样,我们就能够接收到一个包含name和age属性的User对象,并对其进行后续处理。
至此,我们就完成了SpringBoot中实现接收文件和对象的完整攻略讲解。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot中实现接收文件和对象 - Python技术站