尝试使用jsp servlet将图像上载到数据库时出错

时间:2018-05-15 05:24:26

标签: servlets

这是表格标签

<form action="Slider" method="post" enctype="multipart/form-data">
                                    <input type="file" name="img" size="50"> 
                                    <input type = "submit" value = "Change Poster"  class="btn btn-primary waves-effect">
                                    <input type="hidden" name="id" value="1">
                                    </form> 

这是servlet代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Connection con=null;
     PreparedStatement st=null;     



     InputStream inputStream = null;
     try
     {
         Class.forName("com.mysql.jdbc.Driver");
         ReadProperty rd=new ReadProperty();
         con=rd.getConnection();
         String id=request.getParameter("id");
         System.out.println("id is"+id);
        // obtains the upload file part in this multipart request
            Part filePart = request.getPart("img");
            System.out.println(filePart);

            if (filePart != null) {
                // prints out some information for debugging
                System.out.println("image name"+filePart.getName());
                System.out.println(filePart.getSize());
                System.out.println(filePart.getContentType());

                // obtains input stream of the upload file
                inputStream = filePart.getInputStream();
                System.out.println("input stream"+inputStream);
            }
            st=con.prepareStatement("update slider set img=? where id='"+id+"'");
            if(inputStream!=null)
            {
                st.setBlob(1, inputStream);
            }
            int result=st.executeUpdate();
                    if(result>0)
                    {
                        System.out.println("file uploaded successfully...");
                    }


     }

     catch(Exception e)
     {
         System.out.println(e);
     }
    // TODO Auto-generated method stub
}

0 个答案:

没有答案
相关问题