Return-Path: Delivered-To: apmail-jakarta-httpclient-user-archive@www.apache.org Received: (qmail 2638 invoked from network); 14 Jul 2005 22:01:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 14 Jul 2005 22:01:03 -0000 Received: (qmail 79687 invoked by uid 500); 14 Jul 2005 22:01:02 -0000 Delivered-To: apmail-jakarta-httpclient-user-archive@jakarta.apache.org Received: (qmail 79671 invoked by uid 500); 14 Jul 2005 22:01:02 -0000 Mailing-List: contact httpclient-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: "HttpClient User Discussion" Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-user@jakarta.apache.org Received: (qmail 79658 invoked by uid 99); 14 Jul 2005 22:01:02 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jul 2005 15:01:02 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [12.108.131.35] (HELO topexm01.sbc) (12.108.131.35) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 14 Jul 2005 15:00:58 -0700 Received: by topexm01.sbc with Internet Mail Service (5.5.2653.19) id ; Thu, 14 Jul 2005 17:00:57 -0500 Message-ID: From: Gustavo Hexsel To: 'HttpClient User Discussion' Subject: RE: Problems displaying a PNG graphics file Date: Thu, 14 Jul 2005 17:00:57 -0500 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="iso-8859-1" X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N You have to iterate through all the stream, not read it only once. The reason why it's probably working is because the getResponseBodyAsString might be buffering it for you... []s Gus -----Original Message----- From: Michael Schwager [mailto:mschwage@gmail.com] Sent: July 14, 2005 3:58 PM To: httpclient-user@jakarta.apache.org Subject: Problems displaying a PNG graphics file Hi, Java: 1.4.2 HttpClient: 3.0 rc3 If I want to display a PNG file as an ImageIcon, it appears I need to do this with HttpClient: HostConfiguration host = client.getHostConfiguration(); host.setHost(new URI("http://www.example.com", true)); GetMethod method=new GetMethod("/Images/image_file.png"); //notice this code here. It seems to be useless to me... method.getResponseBodyAsString(); //here's what I really want... InputStream IS=method.getResponseBodyAsStream(); BufferedInputStream BIS=new BufferedInputStream(IS); returnCode=BIS.read(inputBytes); icon=new ImageIcon(inputBytes); If I don't put in the getResponseBodyAsString(); I will get a PNGImageDecoder exception. Any ideas? My full source code follows. Thanks. -Mike Schwager import java.awt.Container; import java.awt.Dimension; import java.io.*; import java.io.ByteArrayInputStream; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.auth.*; //import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.*; import javax.swing.*; public class Http extends JFrame { final int MAXBYTES=100000; /** * */ public Http() { super(); ImageIcon icon=null; byte[] inputBytes=new byte[MAXBYTES]; byte[] secondaryBuffer=new byte[10]; byte[] pngBytes; HttpClient client = new HttpClient(); //ByteArrayInputStream inputS=new ByteArrayInputStream(inputBuffer); HostConfiguration host = client.getHostConfiguration(); GetMethod method=new GetMethod("/1.4/Images/image_file.png"); try { host.setHost(new URI("http://www.example.com", true)); HttpState state=client.getState(); Credentials credentials=new UsernamePasswordCredentials("ms07767", "guest"); AuthScope authScope=new AuthScope(host.getHost(), host.getPort()); state.setCredentials(authScope, credentials); int returnCode=client.executeMethod(method); System.err.println("Return code: " + returnCode + " bad status code: " + HttpStatus.SC_UNAUTHORIZED); System.out.println(method.getStatusLine()); // THE ICON WILL ONLY WORK IF I RUN THIS METHOD. // OTHERWISE, IT WILL ERROR OUT WITH // a PNGImageDecoder exception: crc corruption method.getResponseBodyAsString(); InputStream IS=method.getResponseBodyAsStream(); BufferedInputStream BIS=new BufferedInputStream(IS); returnCode=BIS.read(inputBytes); icon=new ImageIcon(inputBytes); } catch (Exception e) { System.out.println(e); } finally { method.releaseConnection(); } Container cp=getContentPane(); JDesktopPane dtp=new JDesktopPane(); JButton button=new JButton(icon); cp.add(button); //dtp.add(button); setVisible(true); pack(); show(); //dtp.setPreferredSize(new Dimension(680,480)); } public static void main(String[] args) { Http http=new Http(); } } --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: httpclient-user-help@jakarta.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: httpclient-user-help@jakarta.apache.org