photo:
file:
dir: E:\IdeaProjects\maven\springboot\ems-thymeleaf2\photo
@Value("${photo.file.dir}")
private String realPath;
@PostMapping("/save")
public String save(Employee employee, MultipartFile img) throws IOException {
String originalFilename = img.getOriginalFilename();
String fileNamePrefix = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf('.'));
String newFileName = fileNamePrefix + fileNameSuffix;
img.transferTo(new File(realPath,newFileName));
employee.setPhoto(newFileName);
employeeService.save(employee);
return "redirect:/employee/lists";
}
- 文件上传:1.表单提交方式必须是post 2.表单enctype属性必须为 multipart/form-data
<form th:action="@{/employee/save}" method="post" enctype="multipart/form-data">
spring:
web:
resources:
static-locations: classpath:/static/,file:${photo.file.dir}
spring:
mvc:
date-format: yyyy-MM-dd
- 需要拼接上@{/}才可以当作链接去访问
头像
<img th:src="@{/}+${employee.photo}" width="60">
|