2014-02-14 75 views
0

我是新來jsp.I插入團塊圖像數據庫有一個表名爲employee現在我想插入圖片使用一個名稱,如datatase request parameter.The表已經有nameageid,phonenumber,emailpassword作爲fields.image是我現在創建的新字段。當更新圖像字段與新的blob圖像時,它不顯示錯誤,但圖像沒有插入。這裏是我的嘗試:使用文件上傳在HTML/JSP

// My form to fileupload 

    <form name="frm" action="Image.jsp" method="post"> 
     <center>Name:<input type="text" name="name"><br></center><br> 

    <center><br><br>&nbsp;&nbsp;<input type="submit" value="Submit"></center> 
    </form> 

    //(Image.jsp)My pgogram to get file from client and store in database 

    <%@ page import="java.sql.*" %> 
    <%@ page import="org.apache.commons.fileupload.*"%> 
    <%@ page import="org.apache.commons.io.output.*"%> 
    <%@ page import="org.apache.commons.fileupload.servlet.*"%> 
    <%@ page import="org.apache.commons.fileupload.disk.*"%> 
    <%@ page import="java.io.*"%> 
<%@ page import="java.util.*"%> 
    <html> 
     <body> 
    <% 
     Connection connection = null; 
     String connectionURL = "jdbc:mysql://localhost:3306/praveen"; 
     PreparedStatement pstatement = null; 
     FileInputStream inputStream = null; 
     int insertQuery = 0; 

     byte[] b = null; 

     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     connection = DriverManager.getConnection(connectionURL, "root", "bulbultara"); 

     String sql = "update employee set image=? where name=? "; 
     pstatement = connection.prepareStatement(sql); 
     pstatement.setString(2, request.getParameter("name")); 
     DiskFileItemFactory factory = new DiskFileItemFactory(); 

     ServletFileUpload sfu = new ServletFileUpload(factory); 
     List items = sfu.parseRequest(request); 

     Iterator iter = items.iterator(); 

     while (iter.hasNext()) { 
     FileItem item = (FileItem) iter.next(); 
     if (!item.isFormField()) { 
     b = item.get(); 
      } 
      } 

     pstatement.setBytes(1, b); 
     insertQuery = pstatement.executeUpdate(); 
     pstatement.close(); 
     connection.close(); 

     %> 

     <a href="Login.jsp"><input type="submit" value="Login"></a> 
    </body> 
    </html> 
+0

你知道'servlet'和'hibernate'。閱讀關於他們的東西,他們會讓你更輕鬆一些嗎? – KNU

+0

你可能想看看它:http://www.codejava.net/coding/upload-files-to-database-servlet-jsp-mysql – KNU

回答

0
<% @ page import="java.io.*" %> 
    <% @ page import="java.sql.*" %> 
    <% @ page import="java.util.zip.* "%> 
    <% 
    String saveFile=""; 
    String contentType=request.ContentType(); 
    if((contentType != null) &&(contentType.indexOf("multipart/form-data")>=0)) 
     { 
     DataInputStream in= new DataInputStream(request.getInputStream()); 
     int formDataLength=request.getContentLength(); 
     byte dataBytes[]= new byte[formDataLength]; 
     int byteread=0; 
     int totalbyteread=0; 
     while(totalbyteread<formDataLength) 
     { 
     byteread=in.read(dataBytes,totalbyteread,formDataLength); 
     totalbyteread =byteread+1; 
     } 
     String file= new String(dataBytes); 
     saveFile=file.subString(file.indexOf("filename=\"")+10); 
     saveFile= saveFile.subString(0,saveFile.indexOf("\n")); 
     saveFile=saveFile.subString(saveFile.lastIndexOf("\\")+1,saveFile.indexOf("\"")); 

     int lastIndex=contentType.lastIndexOf("="); 
     String boundry=contentType.subString(lastIndex+1,contentType.length()); 
     int pos; 
     pos=file.indexOf("filename=\""); 
     pos=file.indexOf("n\",pos)+1; 
     pos=file.indexOf("n\",pos)+1; 
     pos=file.indexOf("n\",pos)+1; 
     pos=file.indexOf("n\",pos)+1; 
     int boundrylocation=file.indexOf(boundry,pos)-4; 
     int startpos=((file.subString(0,pos)).getBytes()).length; 
     int endpos=((file.subString(0,boundryloction)).getBytes()).length; 
     File ff= new File(saveFile); 
     FileOutputStream fileout= new FileOutputStram(ff); 
     fileout.write(dataBytes,startpos,(endpos-startpos)); 
     fileout.flush(); 
     fileout.close(); 
     %> 
     <Br><table border="2"><tr><td><b>You have successfully upload the file:</b> 
    <%out.println(saveFile);%></td></tr></table> 
    <% 
    Connection connection = null; 
    String connectionURL = "jdbc:mysql://localhost:3306/praveen"; 
    PreparedStatement pstatement = null; 
    FileInputStream inputStream = fis; 
    ResultSet rs=null; 
    try 
    { 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    connection = DriverManager.getConnection(connectionURL, "root", "bulbultara"); 
    File f= new File(saveFile); 
    pstatement=connection.prepareStatement("insert into employee(name of image cloumn) values(?)"); 
     pstatement.setBinaryStream(1, (InputStream)fis, (int)(f.length())); 
    int s = pstatement.executeUpdate(); 
     if(s>0){ 
      System.out.println("Uploaded successfully !"); 
     } 
     else{ 
     System.out.println("Error!"); 
     } 
      } 
     catch(Exception e){e.printStackTrace();} 
     } 
    } 

    %> 
+0

感謝它的工作 – praveen