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

行是知之始,知是行之成。这篇文章主要讲述#yyds干货盘点# springboot实现文件上传相关的知识,希望能为你提供帮助。
【#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="https://www.songbingjia.com/android/提交">
< /form>
< /body>
< /html>

2.实现效果:
运行项目,浏览器打开请求??http://localhost:8080/??

选择文件提交后


    推荐阅读