如何通过jsp表单在mysql数据库blob列中上传图像

时间:2014-12-06 09:11:46

标签: jsp servlets

mysql表有两列:id varchar(30)和图像Blob。

这是我的jsp表格:

<form method="post" action="uploadimgs.jsp">
        <table border="0">
            <tr>
                <td>Hotel Id </td>
                <td><input type="text" value="A001" name="id" size="50"/></td>
            </tr>
            <tr>
                <td>Photo: </td>
                <td><input type="file" name="photo" size="50"/></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="Save">
                </td>
            </tr>
        </table>
    </form>

这是后端页面:

<% String id=request.getParameter("id");
    String img=request.getParameter("photo");
      File file = new File(img);  
      FileInputStream in = new FileInputStream(file);
     try
            {

    Class.forName("com.mysql.jdbc.Driver");

    Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/esoft","root","");
            PreparedStatement ps1=conn.prepareStatement("insert into hotimges values(?,?)");
            ps1.setString(1,id);
        ps1.setBinaryStream(3, in, (int) file.length());
        int a=ps1.executeUpdate();
            if(a>0)
                    {
                 response.sendRedirect("hotelsimg.jsp");

            }
          }
        catch(Exception e)
          { 
            out.println(e);
        }     %> 

执行此代码时,第四行出错了。 请建议我解决。

0 个答案:

没有答案
相关问题