public class UrlDemo {
public static void main(String[] args) throws Exception{
//1.创建URL对象
URL url = new URL("...");
//2.连接资源 Http
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//3.获取流
InputStream is = connection.getInputStream();
//4.下载到本地
FileOutputStream fos = new FileOutputStream(new File("jay.jpg"));
byte[] bytes = new byte[1024];
int len;
while ((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
//5.关闭
fos.close();
is.close();
connection.disconnect();
}
}
Java网络编程---URL下载网络资源
最新推荐文章于 2024-04-11 08:30:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)