文件上传
1、单文件上传
首先创建 Spring Boot 项目并添 spring-boot-starter-web 依赖
然后在 resource 录下的 static 目录中创建一个 upload.html 文件。
1.1、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
1.2、上传文件代码
@RestController
public class FileUploadController {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
@PostMapping("/upload")
public String upload(MultipartFile uploadFile, HttpServletRequest request) {
String realPath = "D:/IDEA/springboot-vue-test/uploadFile/";
String format = sdf.format(new Date());
File file = new File(realPath + format);
// 判断目录是否存在,不存在则创建目录
if (!file.isDirectory()) {
file.mkdirs();
}
// 获得上传文件名称
String oldName = uploadFile.getOriginalFilename();
// 随机生成文件名称
String newName = UUID.randomUUID().toString() +
oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
// 保存文件(路径必须存在)
uploadFile.transferTo(new File(file, newName));
// 返回访问url
String filePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + "/" + format + newName;
return filePath;
}catch (Exception e){
e.printStackTrace();
}
return "上传失败";
}
}
最后在浏览器中进行测试。
运行项目,在浏览器中输入http://loclhost:8080/upload.html
注意,所有步骤都是基于项目配置静态资源访问,不然访问不了upload.html,具体怎么配置可以看我主页教程!
2、多文件上传
首先创建 Spring Boot 项目并添 spring-boot-starter-web 依赖
然后在 resource 录下的 static 目录中创建一个 upload.html 文件。
2.1、uploads.html文件
<!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 />
<input type="submit" value="上传" />
</form>
</body>
</html>
这是一个很简单的文件上传页面,上传接口是upload 注意请求方法是 post , enctype是multipart/form-data
1.2、上传文件代码
@PostMapping("/uploads")
public Map uploads(MultipartFile[] uploadFiles, HttpServletRequest request) {
System.out.println(uploadFiles.length);
String realPath = "D:/IDEA/springboot-vue-test/uploadFile/";
String format = sdf.format(new Date());
Map<String, String> filePaths = new HashMap<>();
File file = new File(realPath + format);
// 判断目录是否存在,不存在则创建目录
if (!file.isDirectory()) {
file.mkdirs();
}
for (int i = 0; i < uploadFiles.length; i++) {
// 获得上传文件名称
String oldName = uploadFiles[i].getOriginalFilename();
// 随机生成文件名称
String newName = UUID.randomUUID().toString() +
oldName.substring(oldName.lastIndexOf("."), oldName.length());
try {
// 保存文件(路径必须存在)
uploadFiles[i].transferTo(new File(file, newName));
// 返回访问url
String filePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + "/" + format + newName;
filePaths.put("上传文件" + (i+1), filePath);
}catch (Exception e){
e.printStackTrace();
filePaths.clear();
filePaths.put("上传失败", "500");
return filePaths;
}
}
return filePaths;
}
最后在浏览器中进行测试。
运行项目,在浏览器中输入http://loclhost:8080/uploads.html
注意,所有步骤都是基于项目配置静态资源访问,不然访问不了uploads.html,具体怎么配置可以看我主页教程!