通过form表单上传文件,后台接收的方法

 

上传文件时,form表单应加上参数  enctype="multipart/form-data

 

 后台接收:

  1 package com.jredu.servlet;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 import javax.servlet.ServletException;
  9 import javax.servlet.annotation.WebServlet;
 10 import javax.servlet.http.HttpServlet;
 11 import javax.servlet.http.HttpServletRequest;
 12 import javax.servlet.http.HttpServletResponse;
 13 
 14 import org.apache.commons.fileupload.FileItem;
 15 import org.apache.commons.fileupload.FileUploadException;
 16 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 17 import org.apache.commons.fileupload.servlet.ServletFileUpload;
 18 
 19 import com.jredu.model.User;
 20 import com.jredu.service.UserService;
 21 import com.jredu.service.Impl.UserServiceImpl;
 22 
 23 @WebServlet("/register.action")
 24 public class Register extends HttpServlet {
 25 
 26     /**
 27      * 
 28      */
 29     private static final long serialVersionUID = 1057467000241502504L;
 30 
 31     @Override
 32     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
 33             throws ServletException, IOException {
 34         // TODO Auto-generated method stub
 35         doGet(req, resp);
 36     }
 37 
 38     @Override
 39     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
 40             throws ServletException, IOException {
 41         // TODO Auto-generated method stub
 42         UserService service = new UserServiceImpl();
 43         // 创建磁盘工厂 缓冲区和磁盘目录
 44         DiskFileItemFactory factory = new DiskFileItemFactory();
 45         // 设置文件上传的大小限制
 46         ServletFileUpload upload = new ServletFileUpload(factory);
 47         upload.setFileSizeMax(1024 * 1024 * 10);// 最大上传10M
 48         upload.setHeaderEncoding("utf-8");// 设置编码格式
 49         try {
 50             String name = null;
 51             // 注册获取前台的值
 52             String value = null;
 53             String newName = null;
 54             ArrayList<Object> list = new ArrayList<Object>();
 55             @SuppressWarnings("unchecked")
 56             List<FileItem> files = upload.parseRequest(req);
 57             for (FileItem fileItem : files) {
 58                 // 判断当前的数据时文件还是普通的表单
 59                 // 是文件
 60                 if (fileItem.isFormField()) {
 61                     name = fileItem.getFieldName();// 获取属性的名字
 62                     value = fileItem.getString("utf-8");// 获取属性的值
 63                     list.add(value);
 64                 } else {
 65                     // 是表单
 66                     // 获取文件上传的文件名
 67                     name = fileItem.getName();
 68 
 69                     // 定义一个新的文件来接收
 70                     newName = System.currentTimeMillis() + name;// 以当前的总秒数来命名防止图片名称相同而覆盖
 71                     File file = new File("D:\\image\\" + newName);
 72 
 73                     // 图片的存储路径
 74                     String headImg = "img/" + newName;
 75                     list.add(headImg);
 76                     // 从缓冲区写入磁盘
 77                     fileItem.write(file);
 78                     fileItem.delete();
 79                 }
 80 
 81             }
 82 
 83             // 获取到全部的用户列表
 84             List<User> uList = service.allUser();
 85 
 86             // 将新注册的用户存入数据
 87             boolean flag = service.register(list);
 88 
 89             if (flag) {
 90                 System.out.println("注册成功");
 91 
 92                 // 跳转到登陆界面
 93                 req.getRequestDispatcher("/page/login.jsp").forward(req, resp);
 94 
 95             } else {
 96                 System.out.println("注册失败");
 97             }
 98 
 99         } catch (FileUploadException e) {
100             // TODO Auto-generated catch block
101             e.printStackTrace();
102         } catch (Exception e) {
103             // TODO Auto-generated catch block
104             e.printStackTrace();
105         }
106 
107     }
108 
109 }

 

posted @ 2018-04-16 11:32  ztt_tttt  阅读(20852)  评论(1编辑  收藏  举报