Package COM.HTTP5;
Import Java.io.InputStream;
Import Org.apache.commons.io.IOUtils;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import Org.apache.http.client.methods.HttpGet;
Import Org.apache.http.util.EntityUtils;
public class Imageshow {public static String getimg (httpclient http,string url) {try{httpget get=new httpget (URL);
HttpResponse Hr=http.execute (GET);
Httpentity he=hr.getentity ();//鍝 堝 搱 if (he!=null) {String charset=entityutils.getcontentcharset (HE);
InputStream is=he.getcontent ();
Return ioutils.tostring (Is,charset);
}}catch (Exception e) {e.printstacktrace ();
return null;
public static byte[] getimage (httpclient http,string url) {try{httpget hg=new httpget (URL);
HttpResponse Hr=http.execute (Hg);
Httpentity he=hr.getentity ();
if (he!=null) {InputStream is=he.getcontent ();
Return Ioutils.tobytearray (IS);
} catch (Exception e) {e.printstacktrace (); } return null;
}
}
Package COM.HTTP5;
Import Java.io.FileOutputStream;
Import Org.apache.commons.io.FilenameUtils;
Import Org.apache.commons.io.IOUtils;
Import org.apache.http.client.HttpClient;
Import org.apache.http.impl.client.DefaultHttpClient;
Import Org.htmlparser.Parser;
Import Org.htmlparser.filters.NodeClassFilter;
Import Org.htmlparser.tags.ImageTag;
Import org.htmlparser.util.NodeList; public class Imagedownload {/** * @param args */public static void main (string[] args) {httpclient http=new De
Faulthttpclient ();
try{String html=imageshow.getimg (http, "http://www.baidu.com/"); Parser p=new Parser ();//Create parser p.setinputhtml (HTML);//Parse HTML nodelist nl=p.parse (new Nodeclassfilter (Imagetag.class))
;//Select Image label for (int i=0;i<nl.size (); i++) {Imagetag image= (imagetag) nl.elementat (i);
String Imageurl=image.getimageurl ()//Get Picture src attribute value string url= "http://www.baidu.com" +IMAGEURL;
System.out.println (URL); String jpg=filenameutils.getname (URL);//Picture name byte[] Im=imageshoW.getimage (Http,url);
Ioutils.write (IM, new FileOutputStream ("e:/temp/" +jpg));
}}catch (Exception e) {e.printstacktrace ();
}
}
}