题目

  1. 单文件上传
  2. 多文件上传

实现

单文件

网页选择单个文件,然后上传

上传成功,服务器下新建了文件夹并将文件放入

多文件

网页选择88个文件,然后上传

上传成功,服务器下新建了文件夹并将文件放入

工程包

代码

单文件

单文件Servlet

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 * 单文件上传
 */

//配置servlet uri-pattern和可接受Multipart/form-data文件上传
@WebServlet("/upload")
@MultipartConfig
public class upload3 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//字符编码设置
		response.setContentType("text/html;charset=utf-8");
		
		//设置要上传的路径
		String path=this.getServletContext().getRealPath("/upload");
			
			//为避免每次都上传到同一个文件夹,设置每天新建一个以日期命名的文件夹来接收上传的文件
			LocalDate now=LocalDate.now();
			int year=now.getYear();
			int month=now.getMonthValue();
			int day=now.getDayOfMonth();
			//拼接当前的日期,转为String类型
			String folder=year+""+month+""+day+"";
			path=path+"/"+folder;	
			//判断,如果文件夹路径不存在,则新建文件夹
			File filePath=new File(path);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}
			
			//接收上传的文件
			Part part=request.getPart("photo");
			//判断文件是否超过2M,如果超过提示用户
			if(part.getSize()>(1024*1024*2)) {
				response.getWriter().write("文件名超过2M,请压缩后再上传");
			}
			//得到上传的文件名
			String name=part.getSubmittedFileName();
			//文件上传位置和名称
			String fileNamePath=path+"/"+name;
			//判断文件是否存在,如果不存在,写入,否则提示用户
			File fileExistsPath=new File(fileNamePath);
			if(!fileExistsPath.exists()) {
			part.write(path+"/"+name);		
			}else {
				response.getWriter().write("文件名已存在,请重命名后再上传");
			}

	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				doGet(request, response);
	}

}

单文件JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/upload" method="post" enctype="multipart/form-data">
<input name="photo" type="file"><br>
<input type="submit" value="upload">
</form>

</body>
</html>

多文件

多文件Servlet

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 * 多文件上传
 */
//配置servlet uri-pattern和可接受Multipart/form-data文件上传
@WebServlet("/multiUpload")
@MultipartConfig
public class upload4 extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//字符编码设置
		response.setContentType("text/html;charset=utf-8");
		
		//设置要上传的路径
		String path=this.getServletContext().getRealPath("/upload");
			
			//为避免每次都上传到同一个文件夹,设置每天新建一个以日期命名的文件夹来接收上传的文件
			LocalDate now=LocalDate.now();
			int year=now.getYear();
			int month=now.getMonthValue();
			int day=now.getDayOfMonth();
			//拼接当前的日期,转为String类型
			String folder=year+""+month+""+day+"";
			path=path+"/"+folder;	
			//判断,如果文件夹路径不存在,则新建文件夹
			File filePath=new File(path);
			if (!filePath.exists()) {
				filePath.mkdirs();
			}
			
			//接收上传的文件
			List<Part> parts=(List<Part>)request.getParts();
			//遍历多个文件
			for(Part part:parts) {
				// 5.获取原文件名
				/*
				 * 经过过request请求信息的分析, 原文件名在头信息的Content-Disposition中 所以获取Content-Disposition的值如下
				*form-data; name="photo"; filename="待上传文件.txt"
				*/
				String header = part.getHeader("Content-Disposition");
				int start = header.lastIndexOf("=");
				String fileName = header.substring(start + 1)
						.replace("\"", "");
				//文件上传位置和名称
				String fileNamePath=path+"/"+fileName;
				//判断文件是否存在,如果不存在,写入,否则提示用户
				File fileExistsPath=new File(fileNamePath);
				if(!fileExistsPath.exists()) {
				part.write(path+"/"+fileName);		
				}else {
					response.getWriter().write("文件名已存在,请重命名后再上传");
				}
			}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			doGet(request, response);
	}
}

多文件JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/multiUpload" method="post" enctype="multipart/form-data">
<input name="photo" type="file" multiple="multiple"><br>
<input type="submit" value="upload">
</form>

</body>
</html>