2012-02-15 11 views
3

次は、テーブル "TESTBYTEA"にgzip形式でデータを格納しようとしているJavaコードです。私はpostgresqlデータベースを使用しています。 TESTBYTEAテーブルには、BYTEAタイプの1つの列「データ」があります。データを圧縮して保存したい。 DBから読んでいる間、私はそれを解凍して読んでほしい。しかし、GZIP形式ではないという例外が出ています。postgresqlにgzip形式のデータを格納することができます

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

ところでこれは私の問題を解決アウトオブライン – araqnid

答えて

3

あなたの問題は、あなたがバイトを挿入する方法である:。

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

(本質的 byte[]参照があなたのバイト配列のために .toString()に使用されている
INSERT INTO TESTBYTEA (data) VALUES ('\\x[[email protected]') 

のようなものが生成されます

準備された声明を使ってみませんか?

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

EDIT:

を忘れないでください:

pstmt.close(); 
+1

保存するのに十分な大きされているテキスト値を圧縮し、自動的にPostgreSQLの。ありがとう、トン。 –

関連する問題