复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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内容的全部内容,更多相关获取URL内容内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复