基本上传后台
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String uploadPre(String name, MultipartFile photo)throws Exception {
if (photo!=null){
System.out.println("文件上传:"+name);
System.out.println("文件上传photoname:"+photo.getName());
System.out.println("文件上传photoContentType:"+photo.getContentType());
System.out.println("文件上传photoSize:"+photo.getSize());
System.out.println("文件上传inputStream:"+photo.getInputStream());
}
return "upload_file"; // 此处只返回一个路径, 该路径没有设置后缀,后缀默认是*.html
}
基本上传前台
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot模版渲染</title>
<link rel="icon" type="image/x-icon" href="/images/mldn.ico"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="name"/><br/>
照片:<input type="file" name="photo"/><br/>
<button type="submit" value="上传" th:width="100px"/>
</form>
</body>
</html>
基本上传结果
文件上传:世界和平
文件上传photoname:photo
文件上传photoContentType:image/jpeg
文件上传photoSize:136900
文件上传inputStream:java.io.FileInputStream@4bf1a42a
上传限制
因为普通的文件上传有大小显示,所以我们使用手动配置
Bean配置 (前后台结果和基础上传没有变化)
@Configuration
public class Upload {
@Bean
public MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory configFactory =new MultipartConfigFactory();
configFactory.setMaxFileSize(DataSize.parse("10MB"));
configFactory.setMaxRequestSize(DataSize.parse("100MB"));
configFactory.setLocation("/");
return configFactory.createMultipartConfig();
}
}
后面的图片服务器,因为比较麻烦就没有传