一 ajax上传
1 前端页面
新建springboot项目(使用thymeleaf模板) – 编写页面 – 引入jquery
index页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>主页</title>
// 引入jquery
<script type="text/javascript" th:src="@{/static/js/jquery-3.6.0.min.js}"></script>
</head>
<body>
<h3>导入Excel</h3>
// 选择文件
<input id="fileobj" type="file" name="uploadFile" value="请选择文件">
// 点击事件
<a onclick="uploadFile2();" style="cursor: pointer; display: inline-block;background-color: aqua">JQuery-Ajax上传</a>
</body>
<script>
function uploadFile2() {
let fileobj = $("#fileobj")[0].files[0];
console.log(fileobj);
let form = new FormData();
form.append("uploadFile", fileobj);
$.ajax({
type: 'POST',
url: '/customUser/importUser',
data: form,
// 告诉jquery要传输data对象
processData: false,
// 告诉jquery不需要增加请求头对于contentType的设置
contentType: false,
success: function (arg) {
console.log(arg)
}
})
}
</script>
</html>
2 后台处理
新建控制器 – 编写接口
@RequestMapping("/customUser")
@Controller
public class CustomUserController {
@PostMapping("/importUser")
@ResponseBody
public String importUser(@RequestParam("uploadFile") MultipartFile multipartFile, HttpServletRequest req) {
// 为方便测试,上传的文件将保存在项目运行目录下的uploadFile文件夹
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
System.out.println("realPath : " + realPath);
// 并且在 uploadFile 文件夹中通过日期对上传的文件归类保存
// 比如:/uploadFile/2019/06/06/32091e5f-c9e9-4506-9567-43e724f1fe37.png
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 (IOException e) {
e.printStackTrace();
}
return "上传文件失败";
}
}
二 form表单上传
1 单文件上传
前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<!--<form action="/upload" method="post" enctype="multipart/form-data">-->
<form action="/uploadOne" method="post" enctype="multipart/form-data">
<input type="file" name="uploadFile" value="请选择文件">
<input type="submit" value="上传">
</form>
</body>
</html>
后台处理(同ajax上传,略)
2 多文件上传
前端页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>多文件上传</title>
</head>
<body>
<form action="/uploads" method="post" enctype="multipart/form-data">
// 重点 multiple
<input type="file" name="uploadFiles" value="请选择文件" multiple>
<input type="submit" value="上传">
</form>
</body>
</html>
后台处理
@ResponseBody
@PostMapping("/uploads")
public String uploads(MultipartFile[] uploadFiles, HttpServletRequest req) {
String result = "";
// 遍历所有的文件
for (MultipartFile uploadFile : uploadFiles) {
//上传的文件将保存再项目运行目录下的uploadFile文件夹
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
System.out.println("realPath : " + realPath);
// 并且在 uploadFile 文件夹中通过日期对上传的文件归类保存
// 比如:/uploadFile/2019/06/06/32091e5f-c9e9-4506-9567-43e724f1fe37.png
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;
result += filePath + "<br>";
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}