一.首先要导入JAR包
1.导入如下图所示的jar包
链接如下:
https://pan.baidu.com/s/11DXbOhVO9E3-YvSaM9_7zg
提取码:4cvb
二.在你的代码WAR包下面创建一个upload文件夹
2.1我的路径是:保存文件的路径E:IDEACodeoutartifacts2020_1_20webproject_war_explodedupload
三.上代码
3.1大概思路你要了解一下子
用jsp页面将文件选中然后点击提交后会上传,则就有一个upload.jsp和一个UpLoadServlet
3.2,jsp代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>文件上传</title> </head> <body> <form action="${pageContext.request.contextPath}/UploadServlet" method="post" enctype="multipart/form-data"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 文件:<input type="file" name="file" ><br> <input type="submit"> </form> </body> </html>
3.3Servlet代码
package web_servlet; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.UUID; @WebServlet("/UploadServlet") public class UploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.解析表单数据 //创建一个工厂类 DiskFileItemFactory factory = new DiskFileItemFactory(); //2.获取文件上传对象 ServletFileUpload sfu = new ServletFileUpload(factory); //3.获取File try { List<FileItem> items = sfu.parseRequest(request); for (FileItem item : items) { if (item.isFormField()) {//普通数据 System.out.println(item.getFieldName() + "------" + item.getString()); } else {//文件数据 System.out.println("文件大小" + item.getSize()); // System.out.println("文件字段" + item.getString()); //如果是文件,需要把文件保存到本地 //1.指定文件保存的路径 //注意:添加一个日期目录 /** * SimpleDateFormat =new SimpleDateFormat("yyyMMdd"); * String dateStr=sdf.format(new Date()); */ SimpleDateFormat sdf=new SimpleDateFormat("yyyMMdd"); String dateStr=sdf.format(new Date()); String path = request.getServletContext().getRealPath("upload"+"\"+dateStr); System.out.println("保存文件的路径" + path); //2.创建一个文件夹 File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); System.out.println("创建了文件夹"); } else { System.out.println("文件夹exits"); } //3.将上传的文件进行保存 //3.1设定文件保存的名字 //获取后缀名 String suffix = item.getName().split("\.")[1]; String filename = UUID.randomUUID().toString() + "." + suffix; //3.2拼接一个文件保存路径 String fileSavePath = path + "\" + filename; System.out.println(fileSavePath); //3.3保存 try { item.write(new File(fileSavePath)); } catch (Exception e) { e.printStackTrace(); } } } } catch (FileUploadException e) { e.printStackTrace(); } } }
四.总结
4.1大概就是这样,只要知道有这么个jar包就行了,你自己用I/O技术也可以实现,但是性能方面会有所不同
//创建一个工厂类
DiskFileItemFactory factory = new DiskFileItemFactory();
//2.获取文件上传对象
ServletFileUpload sfu = new ServletFileUpload(factory);
上面这两行大概就是核心代码,大家可以了解一下