import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Url {
static byte[] SjwGetBuffer(String url) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try{
URL sjwurl = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
byte[] buf = new byte[1024];
sjwurl = new URL(url);
httpUrl = (HttpURLConnection) sjwurl.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
if(bis==null)return null;
while(true) {
int bytes_read = bis.read( buf );
if( bytes_read > 0 ) {
bos.write( buf, 0, bytes_read );
}else {
break;
}
} ;
bis.close();
httpUrl.disconnect();
}catch (Exception e) {
System.out.println("err!");
return null;
}
return bos.toByteArray();
}
public static void main(String[] args) throws Exception {
byte[] strBuf = SjwGetBuffer("http://www.baidu.com");
FileOutputStream fileStream = new FileOutputStream("c:baidu.html");
fileStream.write(strBuf);
fileStream.close();
}
}