2013-08-19 34 views
0

我創建了下面的程序,當我按下提交按鈕時,程序正在拋出FileNotFound異常。由於JSP頁面無法找到完整的映像路徑,問題即將到來。我調試了JSP程序,發現HTML表單僅傳遞沒有路徑的圖像名稱,這就是爲什麼問題即將到來。任何人都可以解決此問題。無法將圖像存儲到數據庫

################## SQL Query ###################################### 

    CREATE TABLE IMAGEMAIN(ID INTEGER,IMAGE BLOB) ; 

################## HTML Form ###################### 

    <form name="frm" method="post" action="index.jsp"> 
    <input type="text" name="hint"> 
    <input type="file" name="user_file"> 
    <input type="submit"> 

################### JSP PAGE ######################## 

try 
{ 
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
    System.out.println("Connection loaded"); 
    Connection con = DriverManager.getConnection("jdbc:odbc:project","image","image"); 
    System.out.println("Connection created"); 
    String ll=request.getParameter("user_file"); 
    String lo=request.getParameter("hint"); 
    File imgfile = new File(ll); 

    FileInputStream fin = new FileInputStream(imgfile); 

    PreparedStatement pre = con.prepareStatement("insert into IMAGEMAIN (id,image) values(?,?)"); 
    pre.setString(1,lo); 
    pre.setBinaryStream(2,fin,(int)imgfile.length()); 
    pre.executeUpdate(); 
    pre.close(); 
} 

catch(Exception E) 
{ 
    out.println("the eror is "+ E); 
} 
+0

快速谷歌,這裏是你的解決方案:http://stackoverflow.com/questions/1142475/jsp-file-upload-with-apache-commons –

回答

0

FileInputStream(String)您使用構造函數需要一個文件名,它不會因爲工作的HTTP上傳的文件沒有這樣的作品像一個文件名 - 而不是你直接操作流。

根據此SO QA(jsp file upload issues),JSP不提供處理多部分HTTP請求的內置支持,因此如果不使用其他Java包(如Apache Commons FileUpload),則無法處理上載的文件。

所以我建議你安裝FileUpload,然後用它來訪問上傳的文件。我不熟悉它,但這裏有文檔:http://commons.apache.org/proper/commons-fileupload/using.html

+0

謝謝戴!我知道我需要包含Apache Commons FileUpload,但我沒有得到如何在我的代碼中包含/編寫此包。請協助 – vishal

+0

@vishal我無法真正幫助你 - 聽起來你是Java的初學者。在繼續使用JSP/Servlet之前,我建議你閱讀關於Java的一些文章和教程。 – Dai