JSP文件上传到数据库,本文件参考于哔哩哔哩:DT课堂原名颜群UID:326782412,希望对各位有帮助,参考作者链接:【DT课堂原名颜群的个人空间-哔哩哔哩】https://b23.tv/dAYw

1:jsp页面

<form action="UploadServlet" method="post" enctype="multipart/form-data" >
学号:<input name="userid"/><br>
姓名:<input name="username"/><br>
上传照片:<input type="file" name="spicture"/><br>
<input type="submit" value="注册">
</form>

2:servlet页面

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");
        System.out.println("其他字段");
        int id=0;
        String name = null;
        String filepath;
        //上传
        try {
            Boolean isMultipart=ServletFileUpload.isMultipartContent(request);
            if(isMultipart)//判断jsp页面是否有mutipart属性
            {
                FileItemFactory factory=new DiskFileItemFactory();
                ServletFileUpload upload=new ServletFileUpload(factory);
                //通过parseRequest解析form中的所有请求字段并保存到items集合中
                List<FileItem> items=upload.parseRequest(request);
                //遍历Items中的数据
                Iterator<FileItem> iter=items.iterator();
                while(iter.hasNext())
                {
                    FileItem item=iter.next();
                    String itemName=item.getFieldName();//getFieldName获取普通字段
                    //判断前台字段是普通form表单字段(userid,username)还是文件字段
                    if(item.isFormField())
                    {
                        if(itemName.equals("userid"))
                        {//根据name属性值item是(userid,username,spicture)
                             id=Integer.parseInt(item.getString("utf-8"));
                             System.out.println(id);
                        }else if(itemName.equals("username"))
                        {
                             name=item.getString("utf-8");
                        }else
                        {
                            System.out.println("其他字段");
                        }
                        
                    }else {
                        //spicture文件上传
                        //文件名
                        String filename=item.getName();//getName()获取文件名
                        //获取文件内容并上传
                        //定义文件路径
                        System.out.println(filename);
                        String path="E:\\jsp实验\\UpAndDown\\WebContent\\upload";
                        filepath=path+"\\"+filename;
                        System.out.println(filepath);
                        File file=new File(path,filename);
                        
                        try {
                            item.write(file);
                            Student student=new Student(id,name,filepath);
                            UserDao dao=new UserdaoImpl();
                            Boolean start=dao.Insert(student);
                            if(start=true)
                            {
                                System.out.println("成功");
                            }else
                            {
                                System.out.println("失败");
                            }
                            

                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    }
                    
                }
            }
        }catch(FileUploadException e)
        {
            e.printStackTrace();
        }

}

3:实体类

public class Student {
    private int userid;
    private String username;
    private String filename;
    public int getUserid() {
        return userid;
    }
    public void setUserid(int userid) {
        this.userid = userid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
    public Student(int userid, String username, String filename) {
        super();
        this.userid = userid;
        this.username = username;
        this.filename = filename;
    }
    
}

4:数据库操作

//DBconnection.DBcon()这是自己封装的一个链接数据库方法返回对象为Connection

我就不写了需要的评论区见

public class UserdaoImpl implements UserDao  {
    public Boolean Insert(Student student) {
        Boolean start=false;
        PreparedStatement pstmt=null;
        
        try {
            String sql="insert into student values(?,?,?)";
            pstmt=DBconnection.DBcon().prepareStatement(sql);
            pstmt.setInt(1, student.getUserid());
            System.out.println(student.getUserid());
            pstmt.setString(2, student.getUsername());
            pstmt.setString(3, student.getFilename());
            int count=pstmt.executeUpdate();
            if(count>0)
            {
                start=true;
                return start;
            }
            else
            {
                return start;
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            if(pstmt!=null)
            {
                try {
                    pstmt.close();
                    DBconnection.DBcon().close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return start;
    }
        

}
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值