02-05:springboot文件的上传

时间:2022-11-07 04:29:12

1.在static 下建立upload.html文件

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="fileleUploadController" method="post" enctype="multipart/form-data" >
上传文件:<input type="file" name="filename" /><br/>
<input type="submit"/>
</form>
</body>
</html>

2.编写Controller类

/**
* spring Boot 文件上传
*
* @author Administrator
*
*/
@RestController // 表示 该类下方法的返回值,会自动的做json格式转换:@Controller + @ResponseBody
public class FileUploadController { @RequestMapping("/fileleUploadController")
// 参数的名称需要和HTML中的参数的名称相同(不相同的话,需要做相应的转换)
public Map<String, Object> fileUpload(MultipartFile filename) throws Exception {
System.out.println("文件的名称:" + filename.getOriginalFilename());
filename.transferTo(new File("e:/" + filename.getOriginalFilename()));
Map<String, Object> map = new HashMap<>();
map.put("msg", "ok");
return map;
} }

3.设置文件的上传大小默认是10mb

spring.http.multipart.maxFileSize=200MB
spring.http.multipart.maxRequestSize=200MB

4.访问localhost:8080/upload.html