单文件上传
1.首先创建一个SpringBoot项目并添加 spring-boot-starter-web依赖,然后在resources目录下的static目录中创建一个upload.html文件,内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>单文件上传</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value="请选择文件">
<input type="submit" value="上传">
</form>
</body>
</html>
一个很简单的文件上传页面,上传接口是 /upload,注意请求方法是post,enctype是 multipart/form-data
接着创建文件上传接口,代码如下:
@RestController
public class FileUploadController {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
/**
* 单文件上传
* @param uploadFile 单个文件
* @param req
* @return 文件url
*/
@PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest req){
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()){ //如果当前目录不存在
folder.mkdirs(); //进行创建新目录
}
String oldName = uploadFile.getOriginalFilename(); //旧名字
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."),oldName.length()); //新名字
try {
uploadFile.transferTo(new File(folder,newName)); //保存文件
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
return filePath; //返回生成上传文件的访问路径
} catch (Exception e){
e.printStackTrace();
}
return "上传失败!";
}
}
然后运行项目,在浏览器测试输入:http://localhost:8080/upload.html 进行文件上传,如图:
单击上传按钮进行上传文件,上传成功后会返回上传文件的访问路径,如下图:
然后访问该路径就可以看到刚才上传的图片了,如下图所示:
我们也可以对图片上传进行一些配置,代码如下:
#是否开启文件上传支持
spring.servlet.multipart.enabled=true
#文件写入磁盘的阈值
spring.servlet.multipart.file-size-threshold=0
#上传文件的临时保存位置
spring.servlet.multipart.location=E:\\TempImg
#上传的单个文件的最大大小
spring.servlet.multipart.max-file-size=5MB
#多文件上传时文件的总大小
spring.servlet.multipart.max-request-size=10MB
#文件是否延迟解析,默认为false
spring.servlet.multipart.resolve-lazily=false
多文件上传
多文件上传与单文件上传代码基本一致,代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多文件上传</title>
</head>
<body>
<form action="/uploads" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFiles" value="请选择文件" multiple><br/>
<input type="file" name="uploadFiles" value="请选择文件" multiple><br/>
<input type="file" name="uploadFiles" value="请选择文件" multiple><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
控制层代码如下:
@RestController
public class FileUploadController {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
/**
* 多文件上传
* @param uploadFiles 多文件数组
* @param req
* @return 多文件url
*/
@PostMapping("/uploads")
public String uploads(MultipartFile[] uploadFiles,HttpServletRequest req){
List<String> list = new ArrayList<>(); //生成多条文件的存储路径
if (uploadFiles.length > 0){
for (MultipartFile file:uploadFiles){
MultipartFile uploadFile = file;
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
String format = sdf.format(new Date());
File folder = new File(realPath + format);
if (!folder.isDirectory()){ //如果当前目录不存在
folder.mkdirs(); //进行创建新目录
}
String oldName = uploadFile.getOriginalFilename(); //旧名字
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."),oldName.length()); //新名字
try {
uploadFile.transferTo(new File(folder,newName)); //上传文件
String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile/" + format + newName;
list.add(filePath);
} catch (Exception e){
e.printStackTrace();
}
}
return list.toString();
}else if (uploadFiles.length == 0){
return "请选择文件!";
}
return "上传失败!";
}
}