#yyds干货盘点# springboot实现文件上传

Posted 灰太狼_cxh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#yyds干货盘点# springboot实现文件上传相关的知识,希望对你有一定的参考价值。

springboot实现文件上传

1.代码实现:

添加依赖

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

yml配置

spring:
servlet:
multipart:
max-file-size: 2MB
max-request-size: 2MB

file:
upload:
path: D:/test/

控制层

@Controller
@Slf4j
public class UploadController

@Value("$file.upload.path")
private String path;

@GetMapping("/")
public String uploadPage()
return "upload";


@PostMapping("/upload")
@ResponseBody
public String create(@RequestPart MultipartFile file) throws IOException
String fileName = file.getOriginalFilename();
String filePath = path + fileName;

File dest = new File(filePath);
Files.copy(file.getInputStream(), dest.toPath());
return "Upload file success : " + dest.getAbsolutePath();



前端页面

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>文件上传页面</title>
</head>
<body>
<h1>文件上传页面</h1>
<form method="post" action="/upload" enctype="multipart/form-data">
选择要上传的文件:<input type="file" name="file"><br>
<hr>
<input type="submit" value="提交">
</form>
</body>
</html>

2.实现效果:

运行项目,浏览器打开请求​​http://localhost:8080/​

#yyds干货盘点#

选择文件提交后

#yyds干货盘点#

以上是关于#yyds干货盘点# springboot实现文件上传的主要内容,如果未能解决你的问题,请参考以下文章

#yyds干货盘点# springboot使用@Async实现异步调用

#yyds干货盘点# springboot整合Oauth2,GateWay实现网关登录授权验证

#yyds干货盘点# springboot配置@Async异步任务的线程池

#yyds干货盘点#jackson学习之九:springboot整合(配置文件)

#yyds干货盘点#SpringBoot+flowable快速实现工作流,so easy!

#yyds干货盘点#SpringBoot2 + Redis + MySQL实现一个抢红包系统