2010-10-07 18 views
2

這是最新的代碼。我想嘗試一些不同的東西,但我仍然得到相同的結果。使用Java,我上傳了一個圖像postgres數據庫大小是4000.但是,當我下載它的大小是8000

我正在使用postgres數據庫,我創建了一個名爲文件與數據類型bytea列。 圖像確實上傳到數據庫(當我查看錶格的數據時,我可以在那裏看到它)。

我試了幾張圖片。當我上傳它時,尺寸看起來是正確的,但是當我下載它時尺寸加倍。 (這打破了圖像)。

任何幫助,爲什麼這是行不通的,非常感謝。

public void uploadImage(File image, String imageName) throws SQLException {  
    try { 
     conn = connectionManager.ConnectToDb(); 

     String sql = "INSERT INTO images (name, file) "+ "VALUES(?,?)"; 
     System.out.println("your image name is " + imageName); 
     System.out.println("Uploaded image name is " + image);  

     PreparedStatement stmt = conn.prepareStatement(sql); 
     stmt.setString(1,imageName); 

     fis = new FileInputStream(image); 
     stmt.setBinaryStream(2, fis, (int) image.length()); 

     System.out.println("Image SIZE = " +image.length()); 

     stmt.execute(); 
     conn.commit(); 

    } catch (SQLException e) { 
     System.out.println("Could not insert into DB"); 
     e.printStackTrace();   
    } catch (FileNotFoundException e) { 
      System.out.println("Could not insert into DB"); 
      e.printStackTrace(); 
    } 
    finally { 
     //close DB Objects 
     try { 
      fis.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     conn.close(); 
    } 
} 

public void downloadImageFromDB(String imagename) throws SQLException 
{ 
    ConnectionManager connectionManager = new ConnectionManager(); 

    try { 
     conn = connectionManager.ConnectToDb(); 
     Statement downloadImageStatement = conn.createStatement(); 
     downloadImageStatement.executeQuery("SELECT file FROM images WHERE name = '"+imagename+"'"); 
     ResultSet rs = downloadImageStatement.getResultSet(); 
     int i=1; 
     InputStream in = null; 
     int returnValue = 0; 

     if(rs.next()) 
     { 
      String len1 = rs.getString("file"); 
      int len = len1.length(); 
      byte [] b = new byte[len]; 
      in = rs.getBinaryStream("file"); 
      int index = in.read(b, 0, len); 
      OutputStream outImej = new FileOutputStream("C:\\EclipseProjects\\StrutsHelloWorld\\"+imagename+".JPG"); 

      while (index != -1) 
      { 
      outImej.write(b, 0, index); 
      index = in.read(b, 0, len); 
      System.out.println("=========================="); 
      System.out.println(index); 
      System.out.println(outImej); 
      System.out.println("=========================="); 
      } 
      outImej.close(); 
     }else 
      { 
      returnValue = 1; 
      } 

     downloadImageStatement.close(); 
     conn.close(); 

    } catch (SQLException e) { 
     System.out.println("Could not get image from DB"); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally 
    { 
     // close db objects. 
     conn.close(); 
    } 
    //return imgData; 
} 
+0

圖像表的DDL是什麼樣的? – DerMike 2010-10-07 14:33:24

+0

寫入的文件有多大? ('InputStream.available(...)'有時會返回估計值) – DerMike 2010-10-07 14:37:17

+0

is.available()總是報告的大小是圖像第一個大小的兩倍,還是總是8000?我會用另一張圖片來測試它,看看在這種情況下,不正確的圖像大小恰好是預期大小的兩倍。 – 2010-10-07 14:39:18

回答

3

你不想用InputStream.available()來測量它的大小。 Here's a Q&A,它描述了available()實際上的作用 - 簡而言之,沒有多大用處。

一個(正確的)獲取實際大小的方法是使用File.length()在寫完之後檢查File的大小。

4

您正在將二進制數據存儲到Unicode文本字段中。將您的file列更改爲二進制,它應該可以工作。

+0

我正在使用postgresql。列文件數據類型是bytea – Biggs 2010-10-07 14:47:17

+0

Biggs:你看過'fos'生成的文件嗎?多久了?它的內容與原始文件相比如何? – Gabe 2010-10-07 15:36:20

1

我使用了最新版本的JDBC驅動程序,問題解決了。我正在使用PostgreSQL 9.0.2和JDBC4驅動程序9.0-801。

相關問題