X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 87E57D9BA3 for ; Thu, 29 Sep 2005 10:52:51 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 24806-03 for ; Thu, 29 Sep 2005 13:52:47 +0000 (GMT) Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.198]) by svr1.postgresql.org (Postfix) with ESMTP id 86385D9BE6 for ; Thu, 29 Sep 2005 10:52:48 -0300 (ADT) Received: by xproxy.gmail.com with SMTP id s14so1667426wxc for ; Thu, 29 Sep 2005 06:52:53 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:references; b=T14pWgCEiqT7ISpw0j+LczZNanviL4w5Pg+ZPfmDYyEu2/yxlWVGNu8+DOFd729acbpaZNuix3AiU5HHCy2E3CI7eHj0QMoAWQMvc1cIHi7nH8mjOc2IgKUxyVvh6IUAusPIr6zc65j25TMIG/5nvgGhbnTpnY/LsWy+zZvFHFs= Received: by 10.70.131.4 with SMTP id e4mr501971wxd; Thu, 29 Sep 2005 06:52:53 -0700 (PDT) Received: by 10.70.41.14 with HTTP; Thu, 29 Sep 2005 06:52:53 -0700 (PDT) Message-ID: <89cc056d05092906521f72f367@mail.gmail.com> Date: Thu, 29 Sep 2005 09:52:53 -0400 From: Alfredo Rico Reply-To: Alfredo Rico To: Russell Francis Subject: Re: How to retieve binary data (bytea) without problem ? Cc: pgsql-jdbc@postgresql.org In-Reply-To: <433BDC0B.2040009@ev.net> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_3277_12490735.1128001973291" References: <89cc056d050928132714e17730@mail.gmail.com> <433BDC0B.2040009@ev.net> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.963 required=5 tests=[AWL=0.434, HTML_20_30=0.504, HTML_MESSAGE=0.001, RCVD_BY_IP=0.024] X-Spam-Level: X-Archive-Number: 200509/127 X-Sequence-Number: 13912 ------=_Part_3277_12490735.1128001973291 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi, Russell. Your suggest is very correct and in fact it works very well but only if the file size is under 10 MB, The problem happens just at moment to execute: InputStream ins =3D rs.getBinaryStream( 1 ); I don't know how to overcome this :-( On 9/29/05, Russell Francis wrote: > > 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 =3D this.con.prepareStatement("SELECT contentfile > from > > attachedfiles where filename =3D ? ", ResultSet.TYPE_FORWARD_ONLY, > > ResultSet.CONCUR_READ_ONLY); > > ps.setString(1,"UsingJDBC.pdf"); > > bytes filebinary[] =3D null; > > ResultSet rs =3D ps.executeQuery(); > > if (rs !=3D null) > > { > > while (rs.next()) //Only one row > > { > > filebinary =3D 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 =3D 0; > byte[] buf =3D new byte[ 8192 ]; > ServletOutputStream outs =3D servletRequest.getOutputStream(); > InputStream ins =3D rs.getBinaryStream( 1 ); > > while( ( bytes_read =3D ins.read( buf ) ) !=3D -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 > > > ------=_Part_3277_12490735.1128001973291 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi, Russell.
Your suggest is very correct and in fact it works very well but only if the= file size is under 10 MB,
The problem happens just at moment to execute:
InputStream ins =3D rs.getBinaryStream( 1 );

I don't know how to overcome this :-(


On 9/29/05, Russell Francis <rfrancis= @ev.net> wrote:
Alfredo Rico wrote:
> Hi friends, greetings :-)
>
> I'm u= sing 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 e= ver...).
>
> I have stored, without troubles, binary data reach= ing 30 MB.
>
> To retrieve the binary data I'm using the follow= ing Java code:
>
> PreparedStatement ps =3D this.con.prepareStatement("S= ELECT contentfile from
> attachedfiles where filename =3D ? ", R= esultSet.TYPE_FORWARD_ONLY,
> ResultSet.CONCUR_READ_ONLY);
> ps= .setString (1,"UsingJDBC.pdf");
> bytes filebinary[] =3D null;
>= ResultSet rs =3D ps.executeQuery();
> if (rs !=3D null)
> {> while (rs.next()) //Only one row
> {
> filebinary =3D rs.= getBytes (1); //Obtain the file...
> }
> rs.close();
> }
> p= s.close();
>
>
> Problem:
> If the binary data that= I want to retrieve, is up to 12 MB, I obtain a
> java.lang.OutOfMemo= ryError : Java Heap Space.

Hi Alfredo,

I am not an expert and am rela= tively new to this list but perhaps a
different approach rather than usi= ng getBytes which loads the whole 12M
into memory would work.  = ;Have you tried something like this?

...
int bytes_read =3D 0;
byte[] buf =3D new byte[ 8192 ];ServletOutputStream outs =3D servletRequest.getOutputStream();
InputStr= eam ins =3D rs.getBinaryStream( 1 );

while( ( bytes_read =3D ins.rea= d( buf ) ) !=3D -1 )
{
        outs.write( buf, 0= , bytes_read );
}

outs.close();
ins.close();
...

Thi= s may be less memory intensive than pulling the whole binary field
into = memory at once.

Cheers,
Russ



------=_Part_3277_12490735.1128001973291--