hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gustavo Hexsel <ghex...@sagebrushcorp.com>
Subject RE: Problems displaying a PNG graphics file
Date Thu, 14 Jul 2005 22:00:57 GMT
  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

<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: 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


Mime
View raw message