Hi Michael, As Gustavo has said you need to read the response stream until it returns -1. This indicates that all data has been read. You have two options.
- buffer the content manually: byte[] temp = new byte[1024]; int length = 0; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); InputStream is = get.getResponseBodyAsStream(); while ((length = is.read(temp)) != -1) { buffer.write(temp, 0, length); } ImageIcon icon = new ImageIcon(buffer.toByteArray()); - or: ImageIcon icon = new ImageIcon(get.getResponseBody()); Mike On 7/14/05, Michael Schwager <[EMAIL PROTECTED]> wrote: > 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 > > <code> > 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(); > } > } > </code> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]