2012-02-15 119 views
3

以下是試圖以gzip格式將數據存儲在表「TESTBYTEA」中的java代碼。我正在使用postgresql數據庫。 TESTBYTEA表有一列BYTEA類型的「數據」列。我想壓縮數據並存儲它。從數據庫讀取數據時,我想解壓並讀取它。但是我收到了一個異常「不是GZIP格式」。我們可以將數據以gzip格式存儲在postgresql中

public static void main(String[] args){ 
    insertBytes(connection);   
    readBytes(connection); 
    connection.close(); 
} 

public static void insertBytes(Connection connection) throws FileNotFoundException, IOException, SQLException{ 
    File file = new File("C:test.txt"); 
    FileReader fileReader = new FileReader(file); 
    char[] cbuf = new char[2000]; 
    int read = fileReader.read(cbuf); 
    String str = new String (cbuf); 
    byte[] bytes = gzip(str); 
    Statement statement = connection.createStatement(); 
    int result = statement.executeUpdate("INSERT INTO TESTBYTEA (data) VALUES ('\\\\x"+bytes+"')"); 
    System.out.println(result); 
} 


public static void readBytes(Connection connection) throws SQLException, IOException{ 
    Statement statement = connection.createStatement(); 
    ResultSet rs = statement.executeQuery("select data from testbytea"); 
    while(rs.next()){ 
     byte[] bs = rs.getBytes(1); 
     String str = gunzip(bs); 
     System.out.println(str); 
    } 
} 


private static String gunzip(byte[] bytes) throws IOException { 
    Reader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(bytes)), "US-ASCII"); 
    StringBuffer sbuf = new StringBuffer(); 

    char[] buffer = new char[32 * 1024]; 
    int nread; 
    while ((nread = reader.read(buffer)) >= 0) { 
     sbuf.append(buffer, 0, nread); 
    } 

    String s = sbuf.toString(); 

    reader.close(); 
    return s; 
} 

private static byte[] gzip(String s) throws IOException { 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

    GZIPOutputStream gzos = new GZIPOutputStream(baos); 
    Writer writer = new OutputStreamWriter(gzos, "US-ASCII"); 

    writer.write(s); 
    writer.flush(); 
    gzos.finish(); 

    byte[] bytes = baos.toByteArray(); 

    writer.close(); 
    return bytes; 
} 

但我收到以下異常

Exception in thread "main" java.io.IOException: Not in GZIP format 
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:141) 
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:56) 
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:65) 
    at postgresjdbc.PostgresJDBC.gunzip(PostgresJDBC.java:237) 
    at postgresjdbc.PostgresJDBC.readBytes(PostgresJDBC.java:230) 
    at postgresjdbc.PostgresJDBC.main(PostgresJDBC.java:208) 
Java Result: 1 

任何有關此幫助表示讚賞。

+0

BTW自動PostgreSQL的壓縮是足夠大的存儲外的線 – araqnid 2012-02-15 20:34:50

回答

3

你的問題是你如何插入字節:

"INSERT INTO TESTBYTEA (data) VALUES ('\\\\x"+bytes+"')" 

會產生類似

INSERT INTO TESTBYTEA (data) VALUES ('\\x[[email protected]') 

(基本上是byte[]引用在.toString()用於您的字節數組

你爲什麼不使用準備好的聲明?

PreparedStatement pstmt = connection.prepareStatement(
    "INSERT INTO TESTBYTEA (data) VALUES (?)"); 
pstmt.setBytes(1, bytes); 
pstmt.executeUpdate(); 

編輯:

不要忘記:

pstmt.close(); 
+1

這解決了我的問題文本值。萬分感謝。 – 2012-02-16 09:54:40

相關問題