2015-10-14 79 views
0

代號插入圖像指定的文件:java.io.FileNotFoundException:系統找不到-while數據庫

package com; 

import java.io.*; 
import java.sql.*; 

public class Sample { 

public static void main(String a[]){ 
    Connection con = null; 
    PreparedStatement ps = null; 
    InputStream is = null; 
    try { 
     Class.forName("oracle.jdbc.driver.OracleDriver"); 
     con = DriverManager.getConnection("jdbc:oracle:thin:@172.26.132.40:1521:orclilp","aja60core","aja60core"); 
     ps = con.prepareCall("insert into SAMPLE values (?,?,?)"); 
     ps.setString(1, "Himanshu"); 
     ps.setString(2, "Gupta"); 
     is = new FileInputStream(new File("ajax-logo1.jpg")); 
     ps.setBinaryStream(3, is); 
     int count = ps.executeUpdate(); 
     System.out.println("Count: "+count); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally{ 
     try{ 
      if(is != null) is.close(); 
      if(ps != null) ps.close(); 
      if(con != null) con.close(); 
     } catch(Exception ex){} 
    } 
} 
} 

而且我發現了以下錯誤:

java.io.FileNotFoundException: ajax-logo1.jpg (The system cannot find the file specified) 
at java.io.FileInputStream.open(Native Method) 
at java.io.FileInputStream.<init>(FileInputStream.java:138) 
at com.Sample.main(Sample.java:19) 
+1

重複http://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists – Satya

回答

0

FileNotFoundException java解釋器找不到文件時發生異常

Signals that an attempt to open the file denoted by a specified pathname has failed.

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

確保fil E在您的應用程序中存在(其中的.class文件,而不是在.java源文件)

那是一個常見的錯誤

+0

我已經把該圖像與.class文件,但它仍然不是工作 –

+0

你可以發佈你的項目的文件結構嗎? – MaVRoSCy

+0

In eclipse src - > com(package) - > Sample.java + ajax-logo1.jpg bin - > com(package) - > Sample.class + ajax-logo1.jpg –

0

保持在Sample.java位於同一目錄中的「ajax-logo1.jpg」文件(不推薦)或在文件構造函數中提供「ajax-logo1.jpg」文件的實際/相對路徑。

+1

不正確,除非'Sample.class'與'Sample.java'不在同一個目錄中,否則不應該。 – Albert

+0

完全同意..... – ravi

0

當您簡單地編寫文件的名稱(如您在代碼示例中所做的那樣)時,Java將在您的項目的根目錄中查找該文件,即如果您的項目文件位於目錄databaseProject和目錄結構是這樣的:

`databaseProject 
|->src 
|->pom.xml (for maven builds) 
... ` 

等 Java程序,默認情況下,會考慮你的工作目錄是databaseProject和查找文件,除非你給出一個絕對或相對(從PWD)路徑到你的文件。

一個好的方法是將所有的圖像放在一個文件夾中,例如

`databaseProject 
|->src 
|->images 
    |->ajax-logo1.jpg 
    |-> .... 
|->pom.xml (for maven builds) 
... ` 

然後使用路徑:"/images/<image_name>"來訪問圖像。

+0

這也不起作用 –

+0

什麼是不工作,把文件放在一個單獨的目錄?或將其放置在項目的根目錄中? –