使用URL下载网络上的资源
- 获取下载地址
- 连接资源
- 操作资源
- 关闭资源
public class Url {
public static void main(String[] args) throws IOException {
URL url = new URL("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("reflect\\baidu.png");
byte[] bytes = new byte[1024];
int len;
while ((len = is.read(bytes)) != -1){
fos.write(bytes,0,len);
}
fos.close();
is.close();
urlConnection.disconnect();
}
}