pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: mmg <mathias.degroof@gmail.com>
To: pgsql-jdbc@postgresql.org
Subject: Store a file in a bytea
Date: Thu, 10 Feb 2011 08:01:52 -0800 (PST)
Message-ID: <1297353712985-3379578.post@n5.nabble.com> (raw)


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.



view thread (4+ messages)  latest in thread

Message-ID: <1297353712985-3379578.post@n5.nabble.com>
Permalink:  ../1297353712985-3379578.post@n5.nabble.com/
Also on:    postgresql.org/message-id/1297353712985-3379578.post@n5.nabble.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: mathias.degroof@gmail.com
  Subject: Re: Store a file in a bytea
  In-Reply-To: <1297353712985-3379578.post@n5.nabble.com>
  References: <1297353712985-3379578.post@n5.nabble.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox