2015-07-13 50 views
0

你好,我是在JSP編程新手。我試圖在PostgreSQL數據庫中插入一個圖像與字節字段。這是我的代碼到目前爲止。的setBinaryStream(INT,爲InputStream)尚未實現

JSP代碼

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
    <!DOCTYPE html> 
    <html> 
     <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      <title>JSP Page</title> 
     </head> 
     <body> 
      <h1>Test image</h1> 
      <form action="Upload" method="post" enctype="multipart/form-data"> 
       <input type="text" name="description" /> 
       <input type="file" name="file" /> 
       <input type="submit" /> 
      </form> 
     </body> 
    </html> 

servlet代碼

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
       throws ServletException, IOException { 
InputStream inputStream = null; 

     // obtains the upload file part in this multipart request 
     Part filePart = request.getPart("file"); 
     if (filePart != null) { 
      // debug messages 
      System.out.println(filePart.getName()); 
      System.out.println(filePart.getSize()); 
      System.out.println(filePart.getContentType()); 

      // obtains input stream of the upload file 
      inputStream = filePart.getInputStream(); 
     } 

     try { 
      Class.forName("org.postgresql.Driver"); 
      Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:8084/hgcirequestproject", "appdev2", "[email protected]"); 

      PreparedStatement ps = con.prepareStatement("insert into tbl_testimage (test) values (?)"); 
     ps.setBinaryStream(1, inputStream); 

      ps.executeQuery(); 
      out.println("success"); 
      con.close(); 
      ps.close(); 
     } catch (Exception e) { 
      System.out.println(e.toString()); 

     } 



    } 

} 

我得到了錯誤.setBinaryStream(INT,爲InputStream)尚未實現。 任何幫助將不勝感激。感謝

回答

相關問題