JSP如何将图像从html输入存储到数据库中

时间:2015-01-25 16:49:24

标签: java html jsp

我试图编写一个将图像存储到数据库中的代码,然后使用rollnum将其显示在网页上以从数据库中检索它。我已尽力而为但仍然没有好处,我尝试阅读一些教程,但仍然没有好处,反正这里是我正在研究的代码。如果有人可以提供帮助,我将非常感激,在此先感谢。

<%@page import="java.sql.*" %>            
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Registration Page</title>
    <style>
        body{
            background-image: url('bg.png');
            background-repeat: repeat;
            font-family: Courier New;
            margin:auto;
            width: 1000px;
        }            
        .wrap{
        margin-top:10px;
        border:solid 1px #000;
        height:900px;            
        }
        .header{
            font-size: 50px;
            font-weight: bold;
            font-family: Courier New; 
            text-align: center;

        }
        .border{
            border-bottom: solid 1px #000;
            margin-left: 30px;
            margin-right: 30px;
        }
        .box{
            border: solid 1px #000;
            padding: 8px;
            margin: 30px;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <table>
            <form method="post">
                <br/>                                  
                <tr>
                <td>&nbsp;&nbsp;Roll Number:</td> <td><input type="text" name="rollnum"/></td>
                </tr>
                </div>
                <tr>
                    <td>&nbsp;&nbsp;First Name:</td> <td><input type="text" name="fname"/></td>
                </tr>                     
                <tr>
                    <td>&nbsp;&nbsp;Last Name:</td> <td><input type="text" name="lname"/></td>
                </tr>                                         
                <tr>
                    <td>&nbsp;&nbsp;Course Name:</td> <td><input type="text" name="course"/></td>
                </tr>                                         
                <tr>
                    <td>&nbsp;&nbsp;Gender:</td> <td>Male:<input type="radio" name="gender" value="Male"/> Female:<input type="radio" name="gender" value="Female"/></td>
                </tr>                                         
                <tr>
                    <td>&nbsp;&nbsp;Address:</td> <td><input type="text" name="addr"/></td>
                </tr>                                                             
                <tr>
                    <td>&nbsp;&nbsp; State:</td> <td><input type="text" name="state"/></td>
                </tr>                                         
                <tr>
                    <td>&nbsp;&nbsp; Photo:<input type="file" name="photo"/></td>
                </tr>                                                             
                <tr>
                    <td>&nbsp;&nbsp;<input type="submit" value="Submit" name="submit"/></td>
                </tr>                                                             

            </form>
        </table>                        
        <%
            try{
            if(request.getParameter("submit") != null){
            Connection conn;
            PreparedStatement prep;    
            String sql;
            String url = "jdbc:derby://localhost:1527/reg";                                        
            Class.forName("org.apache.derby.jdbc.ClientDriver");                
            conn = DriverManager.getConnection(url,"uname","pass");                


            sql = 
                "insert into student (rollnum,fname,lname,course,gender,addr,state,photo)"
                +"values(?,?,?,?,?,?,?,?)";                                

            ResultSet rs = null;

            prep = conn.prepareStatement(sql);

            prep.setInt(1, Integer.parseInt(request.getParameter("rollnum")));
            prep.setString(2, request.getParameter("fname"));
            prep.setString(3, request.getParameter("lname"));
            prep.setString(4, request.getParameter("course"));
            prep.setString(5, request.getParameter("gender"));
            prep.setString(6, request.getParameter("addr"));
            prep.setString(7, request.getParameter("state"));                
            //prep.setString(8, request.getParameter("photo"));
            Blob blob = rs.getBlob(request.getParameter("photo"));;
            prep.setBlob(8, blob);                
            int n = prep.executeUpdate();
            if(n>0){                                    
        %>
        <div class="box">Registration Successful!! <b><a href="login.jsp">Login Now!!</a></b></div>
        <%
            }   
            }
            }

            catch(ClassNotFoundException e){

            }
            catch(NumberFormatException n){

            }
        %>
    </div>
</body>

1 个答案:

答案 0 :(得分:0)

您可以这样:

  • 获取图片
  • 使用base64 rappresentation转换图像
  • 将base64存储到数据库
  • 当您需要将图像再次转换为base64到图像