2017-09-06 24 views
0

如何在Postgresql中插入碘照片? 我有這些照片在服務器上,我不能在Postgresql數據庫中插入lo_importPSQLException:錯誤:無法打開服務器文件「path/photo.jpg」:沒有這樣的文件或目錄

private static void insertPhoto(String nom,int pos) throws SQLException { 
    Statement stmt = connection.createStatement(); 

    String path = "http://10.0.0.84/stade_photo/"+pos+".jpg"; 
    System.out.println(path); 
    String sql = "UPDATE stadephenologique SET photo= " 
      + "lo_import('"+path+"') WHERE nom='"+nom.replaceAll("'", "''")+"'"; 
    stmt.executeUpdate(sql); 
    stmt.close(); 
    connection.commit(); 
    System.out.println(nom); 

} 

這是個例外:

Opened database successfully http://10.0.0.84/stade_photo/1.jpg Exception in thread "main" org.postgresql.util.PSQLException: ERROR: could not open server file " http://10.0.0.84/stade_photo/1.jpg ": No such file or directory

+1

'lo_import'只能訪問文件存儲在運行Postgres的服務器上。它無法讀取遠程計算機上的文件,也無法通過http協議讀取文件 –

回答

0

我已經改變照片BYTEA的類型和它的工作對我來說這個功能

private static void insertPhoto(String nom,int pos) throws SQLException, FileNotFoundException { 
    String path = "stade_photo/"+pos+".jpg"; 
    System.out.println(path+" nom="+nom); 

    PreparedStatement pstmt = connection.prepareStatement("UPDATE stadephenologique SET photo = ? WHERE nom = ?"); 
    File file = new File(path); 
    FileInputStream in = new FileInputStream(file); 
    try 
    { 
     pstmt.setBinaryStream(1, in, (int) file.length()); 
     pstmt.setString(2, nom); 
     pstmt.executeUpdate(); 
     connection.commit(); 
    } 
    catch (Exception ee) 
    { 
     System.out.println("Exception is:- " + ee); 
    } 
} 
相關問題