From: Russell Francis <rfrancis@ev.net>
To: Alfredo Rico <alfredorico@gmail.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: How to retieve binary data (bytea) without problem ?
Date: Thu, 29 Sep 2005 08:20:27 -0400
Message-ID: <433BDC0B.2040009@ev.net> (raw)
In-Reply-To: <89cc056d050928132714e17730@mail.gmail.com>
References: <89cc056d050928132714e17730@mail.gmail.com>
Alfredo Rico wrote:
> Hi friends, greetings :-)
>
> I'm using PostgreSQL v7.4.7 with JDBC driver version 8.0 Build 312 in a Java
> Web Deveploment.
>
> I have a table named 'attachedfiles' in which there is a column used to
> store binary data (bytea type) (pdf's files, doc, png, jpg, what ever...).
>
> I have stored, without troubles, binary data reaching 30 MB.
>
> To retrieve the binary data I'm using the following Java code:
>
> PreparedStatement ps = this.con.prepareStatement("SELECT contentfile from
> attachedfiles where filename = ? ", ResultSet.TYPE_FORWARD_ONLY,
> ResultSet.CONCUR_READ_ONLY);
> ps.setString(1,"UsingJDBC.pdf");
> bytes filebinary[] = null;
> ResultSet rs = ps.executeQuery();
> if (rs != null)
> {
> while (rs.next()) //Only one row
> {
> filebinary = rs.getBytes(1); //Obtain the file...
> }
> rs.close();
> }
> ps.close();
>
>
> Problem:
> If the binary data that I want to retrieve, is up to 12 MB, I obtain a
> java.lang.OutOfMemoryError: Java Heap Space.
Hi Alfredo,
I am not an expert and am relatively new to this list but perhaps a
different approach rather than using getBytes which loads the whole 12M
into memory would work. Have you tried something like this?
...
int bytes_read = 0;
byte[] buf = new byte[ 8192 ];
ServletOutputStream outs = servletRequest.getOutputStream();
InputStream ins = rs.getBinaryStream( 1 );
while( ( bytes_read = ins.read( buf ) ) != -1 )
{
outs.write( buf, 0, bytes_read );
}
outs.close();
ins.close();
...
This may be less memory intensive than pulling the whole binary field
into memory at once.
Cheers,
Russ
Attachments:rfrancis.vcf (text/x-vcard, 260B) download
begin:vcard
fn:Russell Francis
n:Francis;Russell
org:Electronic Vision
adr:;;5 Depot Street;Athens;OH;45701;USA
email;internet:rfrancis@ev.net
title:Software Engineer
tel;work:(740)-592-2433 x 18
x-mozilla-html:FALSE
url:http://www.ev.net
version:2.1
end:vcard
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: rfrancis@ev.net, alfredorico@gmail.com
Subject: Re: How to retieve binary data (bytea) without problem ?
In-Reply-To: <433BDC0B.2040009@ev.net>
References: <89cc056d050928132714e17730@mail.gmail.com> <433BDC0B.2040009@ev.net>
* 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