Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 4B4F01337B54 for ; Thu, 10 Feb 2011 13:41:20 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 40828-02 for ; Thu, 10 Feb 2011 17:41:13 +0000 (UTC) X-Greylist: delayed 00:05:02.475186 by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.9]) by mail.postgresql.org (Postfix) with ESMTP id 8BD41133616B for ; Thu, 10 Feb 2011 13:41:12 -0400 (AST) Received: from tbz-emx-mobil.localnet ([85.183.3.198]) by mrelayeu.kundenserver.de (node=mrbap1) with ESMTP (Nemesis) id 0M1iIs-1Q89I224S2-00u5cz; Thu, 10 Feb 2011 18:36:08 +0100 From: "ml-tb" Reply-To: ml-tb@emagixx.de To: pgsql-jdbc@postgresql.org Subject: Re: Store a file in a bytea Date: Thu, 10 Feb 2011 18:36:06 +0100 User-Agent: KMail/1.13.5 (Linux/2.6.32-5-amd64; KDE/4.4.5; x86_64; ; ) References: <1297353712985-3379578.post@n5.nabble.com> In-Reply-To: <1297353712985-3379578.post@n5.nabble.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201102101836.06645.ml-tb@emagixx.de> X-Provags-ID: V02:K0:ukmiL38+JjKnwxU7gs/FsI9SVKoTHzVUu/0mN7y3qfV hPGGMeIzBvVlHIkvF9YiukPSIlm4BYhQ8Q1uw53EQU2ucdMhNF p1/5uQTFL0X+fE6tjrzwPIX1Y6pF0ck9rIgr4DXolCTrO2D0V/ uYRB1acepxfw+BEOt1UApMHEJntquEjCeH2V1vZ1dgSs29j4Bp p6wGzy3McMUKMpG34ab5Q== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201102/66 X-Sequence-Number: 21464 Hi, why do you not use ResultSet.getBinaryStream() to read the content? Bye Thomas Am Donnerstag, 10. Februar 2011 schrieb mmg: > 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.