1 案例分析
1 文件上传原理分析
2 代码实现
1 form.html
vf<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/WEB32/fileuploadServlet2" method="post" enctype="multipart/form-data"> <input type="text" name="username"><br> <input type="file" name="filename"><br> <input type="submit" value="上传文件"> </form> </body> </html>
2 FileuploadServlet代码
public class FileuploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //接受上传文件 //1、创建磁盘文件项工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //2、创建文件上传的核心类 ServletFileUpload upload = new ServletFileUpload(factory); //3、解析request---获得文件项集合 List<FileItem> parseRequest = upload.parseRequest(request); //4、遍历文件项集合 for(FileItem item : parseRequest){ //5、判断普通表单项/文件上传项 boolean formField = item.isFormField();//是否是一个普通表单项 if(formField){ //普通表单项 String fieldName = item.getFieldName(); String fieldValue = item.getString(); System.out.println(fieldName+":"+fieldValue); }else{ //文件上传项 //获得上传文件的名称 String fileName = item.getName(); //获得上传文件的内容 InputStream in = item.getInputStream(); //将in中的数据拷贝服务器上 String path = this.getServletContext().getRealPath("upload"); OutputStream out = new FileOutputStream(path+"/"+fileName); int len = 0; byte[] buffer = new byte[1024]; while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } in.close(); out.close(); } } } catch (FileUploadException e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 FileuploadServlet2代码
public class FileuploadServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //1、创建磁盘文件项工厂 //作用:设置缓存文件的大小 设置临时文件存储的位置 String path_temp = this.getServletContext().getRealPath("temp"); //DiskFileItemFactory factory = new DiskFileItemFactory(1024*1024, new File(path_temp)); DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024*1024); factory.setRepository(new File(path_temp)); //2、创建文件上传的核心类 ServletFileUpload upload = new ServletFileUpload(factory); //设置上传文件的名称的编码 upload.setHeaderEncoding("UTF-8"); //ServletFileUpload的API boolean multipartContent = upload.isMultipartContent(request);//判断表单是否是文件上传的表单 if(multipartContent){ //是文件上传的表单 //***解析request获得文件项集合 List<FileItem> parseRequest = upload.parseRequest(request); if(parseRequest!=null){ for(FileItem item : parseRequest){ //判断是不是一个普通表单项 boolean formField = item.isFormField(); if(formField){ //username=zhangsan String fieldName = item.getFieldName(); String fieldValue = item.getString("UTF-8");//对普通表单项的内容进行编码 System.out.println(fieldName+"----"+fieldValue); //当表单是enctype="multipart/form-data"时 request.getParameter相关的方法 //String parameter = request.getParameter("username"); }else{ //文件上传项 //文件的名 String fileName = item.getName(); //获得上传文件的内容 InputStream in = item.getInputStream(); String path_store = this.getServletContext().getRealPath("upload"); OutputStream out = new FileOutputStream(path_store+"/"+fileName); IOUtils.copy(in, out); in.close(); out.close(); //删除临时文件 item.delete(); } } } }else{ //不是文件上传表单 //使用原始的表单数据的获得方式 request.getParameter(); } } catch (FileUploadException e) { e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3 文件上传知识点