URL 通信
PPG007 ... 2021-12-28 Less than 1 minute
# URL 通信
public void test() throws IOException {
//构造一个URL对象
URL url = new URL("http://localhost:8080/33bae299.png");
//开启连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
//获取输入流
InputStream inputStream = connection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream("D:\\Javaweb\\Java-Basic\\Net\\src\\url.png");
byte[] buffer = new byte[1024];
int len;
//保存到文件
while ((len=inputStream.read(buffer))!=-1){
fileOutputStream.write(buffer,0,len);
}
//关闭资源
fileOutputStream.close();
inputStream.close();
connection.disconnect();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20