计算机网络技术与JAVA网络编程URL编程-----JAVA入门基础教程-----计算机网络经典

import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class URLTest
{
    public static void main(String[] args)
    {
        //URL:统一资源定位符(种子),一个URL就定位着互联网上某个资源的地址
        //http:应用层协议,IP地址,端口号,资源地址,参数列表
        String string = "http://localhost:8080/examples/index.html";
        try
        {
            URL url = new URL(string);
            //获取协议名
            System.out.println(url.getProtocol());
            //获取主机名
            System.out.println(url.getHost());
            //获取端口号
            System.out.println(url.getPort());
            //获取文件路径
            System.out.println(url.getPath());
            //获取文件名
            System.out.println(url.getFile());
            //获取查询名
            System.out.println(url.getQuery());
        }
        catch (MalformedURLException e)
        {
            throw new RuntimeException(e);
        }
    }
    @Test
    public void Test()
    {
        HttpURLConnection httpURLConnection = null;
        BufferedInputStream bufferedInputStream = null;
        OutputStream outputStream = null;
        try
        {
            URL url = new URL("http://localhost:8080/examples/123.jpg");
            //建立与服务端的链接
            httpURLConnection = (HttpURLConnection)url.openConnection();
            //获取流
            bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream());
            outputStream = new BufferedOutputStream(new FileOutputStream(new File("D:\\123.jpg")));
            byte[] bytes = new byte[10240];
            int len;
            while((len = bufferedInputStream.read(bytes)) != -1)
            {
                outputStream.write(bytes,0,len);
            }
        }
        catch (MalformedURLException e)
        {
            throw new RuntimeException(e);
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
        finally
        {
            try
            {
                if (bufferedInputStream != null)
                {
                    bufferedInputStream.close();
                }
                if (outputStream != null)
                {
                    outputStream.close();
                }
                if (httpURLConnection != null)
                {
                    httpURLConnection.disconnect();
                }
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
}

import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class URLTest
{
public static void main(String[] args)
{
//URL:统一资源定位符(种子),一个URL就定位着互联网上某个资源的地址
//http:应用层协议,IP地址,端口号,资源地址,参数列表
String string = "http://localhost:8080/examples/index.html";
try
{
URL url = new URL(string);
//获取协议名
System.out.println(url.getProtocol());
//获取主机名
System.out.println(url.getHost());
//获取端口号
System.out.println(url.getPort());
//获取文件路径
System.out.println(url.getPath());
//获取文件名
System.out.println(url.getFile());
//获取查询名
System.out.println(url.getQuery());
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
}
@Test
public void Test()
{
HttpURLConnection httpURLConnection = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream = null;
try
{
URL url = new URL("http://localhost:8080/examples/123.jpg");
//建立与服务端的链接
httpURLConnection = (HttpURLConnection)url.openConnection();
//获取流
bufferedInputStream = new BufferedInputStream(httpURLConnection.getInputStream());
outputStream = new BufferedOutputStream(new FileOutputStream(new File("D:\\123.jpg")));
byte[] bytes = new byte[10240];
int len;
while((len = bufferedInputStream.read(bytes)) != -1)
{
outputStream.write(bytes,0,len);
}
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
finally
{
try
{
if (bufferedInputStream != null)
{
bufferedInputStream.close();
}
if (outputStream != null)
{
outputStream.close();
}
if (httpURLConnection != null)
{
httpURLConnection.disconnect();
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
}

你可能感兴趣的:(JAVA随手写,计算机网络经典,网络,java,开发语言,数据结构,intellij-idea)