下载网络资源

package UDP;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//下载网络资源
public class URlDown {
    public static void main(String[] args) throws Exception {
        //下载地址
        URL url =new URL("https://www.baidu.com");//地址是你想下载的东西的路径
        //连接到这个资源
        HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
        //下载资源
        InputStream inputStream= urlConnection.getInputStream();
        FileOutputStream fos= new FileOutputStream("aa.jpg");//下载到本地起个名字(以jpg类型的图片为例)
        byte[]  buffer =new byte[1024];
        int len;
        while ((len=inputStream.read(buffer))!=-1){
        fos.write(buffer,0,len);
        }
        fos.close();
        inputStream.close();
        urlConnection.disconnect();//断开连接
    }

}
posted @ 2021-03-30 10:42  仅此忆念  阅读(67)  评论(0)    收藏  举报