所需的依赖
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
后端的servlet代码
@WebServlet("/upload.do")
public class UploadFileServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 判断请求中是普通表单还是带文件的表单
if (!ServletFileUpload.isMultipartContent(req)) {
return; //普通表单直接返回
}
// 创建上传文件的保存路径建议在 web-inf路径下,安全,用户无法直接访问上传文件
String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
File uploadDir = new File(uploadPath);
// 如果此文件夹不存在就创建
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
//缓存临时文件
// 如果文件超过预期大小,把文件设置为一个临时文件,过几天就自动删除
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
File tempDir = new File(tempPath);
// 如果此文件夹不存在就创建
if (!tempDir.exists()) {
tempDir.mkdir();
}
String msg =null;
try {
//使用 apache common-fileupload.jar 进行处理上传文件的流
// 处理文件上传流需要使用servletFileUpload,使用servletFileUpload需要先构造DiskFileItemFactory
DiskFileItemFactory factory = getDiskFileItemFactory(tempDir);
ServletFileUpload upload = getServletFileUpload(factory);
// 处理上传文件
msg = uploadParseRequest(req, uploadDir, upload);
} catch (FileUploadException e) {
msg="上传失败!";
e.printStackTrace();
}
req.setAttribute("msg",msg);
req.getRequestDispatcher("info.jsp").forward(req,resp);
}
private String uploadParseRequest(HttpServletRequest req, File uploadDir, ServletFileUpload upload) throws IOException, FileUploadException {
String msg = null;
// 处理上传文件
//servletFileUpload 把表单中的每一个input标签数据进行封装
// 从请求中解析成一个个FileItem
List<FileItem> fileItems = upload.parseRequest(req);
for (FileItem fileItem : fileItems) {
// 判断每一个input是否是普通的input的还是带文件的input
if (fileItem.isFormField()) {
String name = fileItem.getFieldName();
String value = fileItem.getString("utf-8");
System.out.println(name + ":" + value);
} else { //带文件的input
// ========================处理文件=============================
//获取文件名
String uploadFileName = fileItem.getName();
System.out.println("文件名:"+uploadFileName);
// 检查文件
if ("".equals(uploadFileName.trim()) || uploadFileName == null) {
continue;
}
// 从全路径的文件名中获取真正的文件名
String fileName = uploadFileName.substring(uploadFileName.lastIndexOf("/") + 1);
// 获取文件后缀 可以限制上传的文件类型
String fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
System.out.println("文件类型:"+fileExtName);
if (!"jpg".equals(fileExtName))
return msg = "文件类型不符!";
//=========================== 存放文件============================
// 使用uuid作为文件唯一文件目录 区别每一个上传文件的唯一性 防止多人上传相同文件不成功问题
String uuid = UUID.randomUUID().toString();
// 文件真实存放路径
String realPath = uploadDir + "/" + uuid;
File realPathDir = new File(realPath);
if (!realPathDir.exists())
realPathDir.mkdir();
//=========================== 文件传输============================
InputStream input = fileItem.getInputStream();
FileOutputStream out = new FileOutputStream(realPathDir + "/" + fileName);
byte[] buff = new byte[1024];
int len = 0;
while ((len = input.read(buff)) > 0) {
out.write(buff, 0, len);
out.flush();
}
input.close();
out.close();
// 删除临时文件
fileItem.delete();
msg = "成功上传!";
}
}
return msg;
}
private ServletFileUpload getServletFileUpload(DiskFileItemFactory factory) {
ServletFileUpload upload = new ServletFileUpload(factory);
// 监听文件的上传进度
upload.setProgressListener(
/***
* @Description
* 获取正在上传文件的进度监听器
* @Date 2021/1/17 16:19
* @param pBytesRead 正在读取
* @param pContentLength 总文件大小
* @param pItems 读取第几个文件
* @return void
*/
(long pBytesRead, long pContentLength, int pItems) -> {
double v = (pBytesRead * 1.00 / pContentLength) * 100;
System.out.println("上传进度:"+String.format("%.2f", v)+"% "+"总大小:" + pContentLength + "已上传:" + pBytesRead + "正在读取第" + pItems + "个文件");
});
upload.setHeaderEncoding("utf-8");
//单个文件限制大小 10M
upload.setFileSizeMax(1024 * 1024 * 10);
//总共能够上传的文件大小
upload.setSizeMax(1024 * 1024 * 10);
return upload;
}
private DiskFileItemFactory getDiskFileItemFactory(File tempDir) {
DiskFileItemFactory factory = new DiskFileItemFactory();
//通过工厂设置一个缓冲区当上传的文件大小超过设置的值,就存放到临时文件目录中
factory.setSizeThreshold(1024 * 1024);
// 临时文件存储目录
factory.setRepository(tempDir);
return factory;
}
}
前端代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
</head>
<body>
<form action="upload.do" enctype="multipart/form-data" method="post">
<p>上传用户<input type="text" name="user"></p>
<p><input type="file" name="file"></p>
<input type="submit"> | <input type="reset">
</form>
</body>
</html>
上传的文件是存放在项目war包下的web-inf中的