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 A9F45D9C1A for ; Thu, 29 Sep 2005 09:20:29 -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 81623-08 for ; Thu, 29 Sep 2005 12:20:24 +0000 (GMT) Received: from mail.ev.net (unknown [132.235.215.234]) by svr1.postgresql.org (Postfix) with SMTP id 189A9D9C27 for ; Thu, 29 Sep 2005 09:20:25 -0300 (ADT) Received: (qmail 66448 invoked by uid 98); 29 Sep 2005 08:24:49 +0000 Received: from 192.168.1.64 by mail.ev.net (envelope-from , uid 98) with qmail-scanner-1.25 (clamdscan: 0.87/1082. Clear:RC:1(192.168.1.64):. Processed in 0.126948 secs); 29 Sep 2005 08:24:49 -0000 X-Qmail-Scanner-Mail-From: rfrancis@ev.net via mail.ev.net X-Qmail-Scanner: 1.25 (Clear:RC:1(192.168.1.64):. Processed in 0.126948 secs) Received: from unknown (HELO ?192.168.1.64?) (192.168.1.64) by mail.ev.net with SMTP; 29 Sep 2005 08:24:49 +0000 Message-ID: <433BDC0B.2040009@ev.net> Date: Thu, 29 Sep 2005 08:20:27 -0400 From: Russell Francis User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050404) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Alfredo Rico Cc: pgsql-jdbc@postgresql.org Subject: Re: How to retieve binary data (bytea) without problem ? References: <89cc056d050928132714e17730@mail.gmail.com> In-Reply-To: <89cc056d050928132714e17730@mail.gmail.com> X-Enigmail-Version: 0.89.6.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: multipart/mixed; boundary="------------020503080506060804050800" X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200509/125 X-Sequence-Number: 13910 This is a multi-part message in MIME format. --------------020503080506060804050800 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------020503080506060804050800 Content-Type: text/x-vcard; charset=utf-8; name="rfrancis.vcf" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="rfrancis.vcf" 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 --------------020503080506060804050800--