• 通过form表单上传文件(多文本框,多图片的),后台接收的方法


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

     后台接收时的代码:将表单数据封装进 list集合中

    package com.jredu.servlet;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    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 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 com.jredu.model.User;
    import com.jredu.service.UserService;
    import com.jredu.service.Impl.UserServiceImpl;
    
    @WebServlet("/register.action")
    public class Register extends HttpServlet {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1057467000241502504L;
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(req, resp);
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            // TODO Auto-generated method stub
            UserService service = new UserServiceImpl();
            // 创建磁盘工厂 缓冲区和磁盘目录
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // 设置文件上传的大小限制
            ServletFileUpload upload = new ServletFileUpload(factory);
            upload.setFileSizeMax(1024 * 1024 * 10);// 最大上传10M
            upload.setHeaderEncoding("utf-8");// 设置编码格式
            try {
                String name = null;
                // 注册获取前台的值
                String value = null;
                String newName = null;
                ArrayList<Object> list = new ArrayList<Object>();
                @SuppressWarnings("unchecked")
                List<FileItem> files = upload.parseRequest(req);
                for (FileItem fileItem : files) {
                    // 判断当前的数据时文件还是普通的表单
                   
                    if (fileItem.isFormField()) { // 是文件
                        name = fileItem.getFieldName();// 获取属性的名字
                        value = fileItem.getString("utf-8");// 获取属性的值
                        list.add(value);
                    } else {// 是表单
                        // 获取文件上传的文件名
                        name = fileItem.getName();
    
                        // 定义一个新的文件来接收
                        newName = System.currentTimeMillis() + name;// 以当前的总秒数来命名防止图片名称相同而覆盖
                        File file = new File("D:\image\" + newName);
    
                        // 图片的存储路径
                        String headImg = "img/" + newName;
                        list.add(headImg);
                        // 从缓冲区写入磁盘
                        fileItem.write(file);
                        fileItem.delete();
                    }
    
                }
    
                // 获取到全部的用户列表
                List<User> uList = service.allUser();
    
                // 将新注册的用户存入数据
                boolean flag = service.register(list);
    
                if (flag) {
                    System.out.println("注册成功");
    
                    // 跳转到登陆界面
                    req.getRequestDispatcher("/page/login.jsp").forward(req, resp);
    
                } else {
                    System.out.println("注册失败");
                }
    
            } catch (FileUploadException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    

      

  • 相关阅读:
    elastic-job详解(二):作业的调度
    elastic-job详解(一):数据分片
    定时任务的分布式调度
    HBase多条件及分页查询的一些方法
    TP6多应用模式配置
    Swoole WebSocket 服务端如何主动推送消息?
    mysql(多级分销)无限极数据库设计方法
    django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
    Centos7安装并配置Python3环境
    short url短链接原理
  • 原文地址:https://www.cnblogs.com/cjeandailynotes/p/10857163.html
Copyright © 2020-2023  润新知