首先我们要知道 MultipartFile 自动封装上传过来的文件。springboot已经帮我们封装好其功能,我们需要会调用它的方法(headerImg.transferTo)即可。
MultipartFile: 单文件上传
MultipartFile[ ] :多文件上传
这边要注意的是:下载下来图片保存的路径问题:切忌不要忘了后面的“/”或“\\”
windows:H:\\cache\\ 拼接上图片名;
MacOs: 绝对路径 拼接图片名。即"/Users/XXX/XXX/imagesboot/"
package com.liny.admin.controller;
import com.liny.admin.pojo.PictureUtil;
import com.liny.admin.utils.FileNameUtil;
import com.sun.java.swing.plaf.motif.MotifLabelUI;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* 测试文件上传
* <form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
*/
@Controller
@Slf4j
public class FormTestController {
@GetMapping("/form_layouts")
public String form_layouts(){
return "form/form_layouts";
}
/**
* MultipartFile 自动封装上传过来的文件
* @param email
* @param username
* @param headerImg
* @param photos
* @return
*/
@PostMapping("/upload")
public String upload(@RequestParam("email")String email,
@RequestParam("username") String username,
@RequestPart("headerImg") MultipartFile headerImg,
@RequestPart("photos") MultipartFile[] photos){
log.info("上传的信息:email={},username={},headerImg={},photos={}",
email,username,headerImg.getSize(),photos.length);
if(!headerImg.isEmpty()){
//保存到文件服务器,OSS服务器
String originalFilename = headerImg.getOriginalFilename();
try {
headerImg.transferTo(new File("H:\\cache\\"+originalFilename));
} catch (IOException e) {
e.printStackTrace();
}
}
if(photos.length > 0){
for (MultipartFile photo : photos) {
if(!photo.isEmpty()){
String originalFilename = photo.getOriginalFilename();
try {
photo.transferTo(new File("H:\\cache\\"+originalFilename));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "main";
}
}
前端代码块:
<form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputEmail1">邮箱</label>
<input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">名字</label>
<input type="text" name="username" class="form-control" id="exampleInputPassword1" placeholder="username">
</div>
<div class="form-group">
<label for="exampleInputFile">头像</label>
<input type="file" name="headerImg" id="exampleInputFile">
</div>
<div class="form-group">
<label for="exampleInputFile">生活照</label>
<input type="file" name="photos" multiple>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-primary">提交</button>
</form>
最后如果遇到文件过大问题,我们只需要在yml文件下设置下面这段代码即可,可适当修改
max-file-size: 单文件最大内存
max-request-size: 总文件内存
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 100MB