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);
}
}
}
}文章来源地址https://www.toymoban.com/news/detail-606228.html
文章来源:https://www.toymoban.com/news/detail-606228.html
到了这里,关于计算机网络技术与JAVA网络编程URL编程-----JAVA入门基础教程-----计算机网络经典的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!