网络编程(四)URL实现下载资源
2021/12/22 11:49:47
本文主要是介绍网络编程(四)URL实现下载资源,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.8、URL
https://www.cnblogs.com/qkshhan/
统一资源定位符:定位资源的,定位互联网上的某一个资源。
DNS域名解析 www.baidu.com xxx.x..x..x
协议://ip端口:端口/项目名/资源
package comip.study.lesson04;
import java.net.MalformedURLException;
import java.net.URL;
//http://localhost:8080/hansuo/secret.txt
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=kuangshen$passworld=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 comip.study.lesson04; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class UrlDown { public static void main(String[] args) throws IOException { //1.下载地址 URL url = new URL("http://localhost:8080/hansuo/secret.txt"); //连接到这个资源 http HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); FileOutputStream fos = new FileOutputStream("secret.txt"); byte[] buffer = new byte[1024]; int len; while ((len=inputStream.read(buffer))!=-1){ fos.write(buffer,0,len);//写出这个数据 } fos.close(); inputStream.close(); urlConnection.disconnect();//断开连接 } }
记得我们我们需要提前在此处创建好问哦们需要下载的东西。
可以访问到文件
没有默认资源index.jsp
下载成功!
这篇关于网络编程(四)URL实现下载资源的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南