Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id E97241337BC8 for ; Thu, 10 Feb 2011 12:24:01 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 70204-04-3 for ; Thu, 10 Feb 2011 16:23:53 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sam.nabble.com (sam.nabble.com [216.139.236.26]) by mail.postgresql.org (Postfix) with ESMTP id 88DE51337BC2 for ; Thu, 10 Feb 2011 12:23:53 -0400 (AST) Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.69) (envelope-from ) id 1PnYxt-0000zb-0M for pgsql-jdbc@postgresql.org; Thu, 10 Feb 2011 08:01:53 -0800 Date: Thu, 10 Feb 2011 08:01:52 -0800 (PST) From: mmg To: pgsql-jdbc@postgresql.org Message-ID: <1297353712985-3379578.post@n5.nabble.com> Subject: Store a file in a bytea MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.767 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01, URI_HEX=1.122 X-Spam-Level: X-Archive-Number: 201102/65 X-Sequence-Number: 21463 Hi all, I'm creating an application that will store files in a postresql database and which will retrieve them later on. I store my data in a bytea column. The problem I'm having is that when I retrieve the data later and write it out, the resulting file is not the same as the original. When I open the file it looks as though the ASCII codes were written out instead of the data itself (so it looks as if the ASCII was encoded again as ASCII, causing me to see ASCII codes when I open the file). I created a simple test program to demonstrate this: Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost/testdb", "test", "test"); conn.createStatement().executeUpdate("DELETE FROM testtable"); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO testtable(data) VALUES(?)"); File file = new File("c:/t.txt"); FileInputStream fis = new FileInputStream(file); pstmt.setBinaryStream(1, fis, (int) file.length()); pstmt.executeUpdate(); ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM testtable"); if (rs.next()) { byte[] data = rs.getBytes("data"); FileOutputStream fos = new FileOutputStream("c:/t2.txt"); fos.write(data); fos.close(); } rs.close(); conn.close(); There is one table: testtable with a data colum which is a bytea. I read the file c:/t.txt, store this as a stream then read it out again and write it to c:/t2.txt. When I open t2.txt, I see the ASCII codes instead of the actual text. I don't know what I'm doing wrong. I've tried reading it as a stream from the database, but that doesn't work either. Any help is much appreciated. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Store-a-file-in-a-bytea-tp3379578p3379578.html Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.