URL下载网络资源
URL统一资源定位符
五大部分:
协议://IP:端口/项目名/资源
package inet;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo1 {
    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123");
        System.out.println(url.getProtocol());//协议名
        System.out.println(url.getHost());//主机ip
        System.out.println(url.getPort());//端口
        System.out.println(url.getPath());//文件
        System.out.println(url.getFile());//文件全路径
        System.out.println(url.getQuery());//参数
    }
}

下载网络资源
package inet;
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 {
        //1.下载地址
        URL url = new URL("https://www.cnblogs.com/kakafa/p/15019311.html");
        //2.连接到这个资源 http连接
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        InputStream is= connection.getInputStream();
        FileOutputStream fos = new FileOutputStream("E:\\2021.TXT");
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        fos.close();
        is.close();
        connection.disconnect();//断开连接
    }
 }
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号