package spring;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class TestURL {
public static void main(String[] args) throws Exception {
System.out.println(inputStream2String(getURLInputStream("http://www.baidu.com")));
}
/*
* 获取URL内容
* */
public static InputStream getURLInputStream(String site) throws Exception{
final URL url = new URL(site);
URLConnection con = url.openConnection();
con.setUseCaches(false);
InputStream stream = con.getInputStream();
return stream;
}
public static String inputStream2String (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
}
获取URL内容
最新推荐文章于 2025-05-22 00:15:00 发布