当前位置:网站首页>URL下载网络资源
URL下载网络资源
2022-04-21 19:41:00 【无名`】
package com.wuming.lesson04;
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/index.jsp?username=wuming&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 com.wuming.lesson04;
import java.io.FileOutputStream;
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 Exception {
//1.下载地址
URL url = new URL("https://p2.music.126.net/xOMA-0nOKNmweJ-vcfaBMQ==/109951167290093049.jpg?param=200y200");
//2.连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("109951167290093049.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();//断开连接
}
}
========
下载图片
在根目录路径下

下载音乐,用播放器播放

版权声明
本文为[无名`]所创,转载请带上原文链接,感谢
https://blog.csdn.net/wanggang182007/article/details/124283978
边栏推荐
- Routing of messages in exchange
- Daily CISSP certification common mistakes (April 20, 2022)
- Alternative JC-1, mito-id series mitochondrial membrane potential detection kit
- AAAI 2022 更细粒度的不实信息检测
- 知道创宇发布重磅战略方案,构建持续交火的实战化防御体系
- Distributed transaction Foundation
- 到底什么是外包?
- Feign source code analysis
- Redis基础
- getchar,putchar,EOF
猜你喜欢
随机推荐
MySQL de duplication query
杰理之复位IO维持电平使用说明【篇】
Alternative JC-1, mito-id series mitochondrial membrane potential detection kit
About the execution order of SQL statements
ParaView Glance 启动报错
自动控制原理第6章——控制系统的校正及综合(思维导图)
WLAN Qpower 介绍
到底什么是外包?
杰理之使用不可屏蔽中断的开关中断函数【篇】
使用CMake构建/在命令行上构建项目
AAAI 2022 更细粒度的不实信息检测
OpenCV之OpenCL介绍
使用CMake构建/CMake命令参考
配置PyTorch、TensorFlow 环境
Lenovo promises that 100% of all computer products will contain recycled plastics by 2025
【JZ47 礼物的最大价值】
静态链接与动态链接
Mysql database index interview questions (latest version)
Pat (advanced level) 1096 - continuous
学完这篇Charles抓包教程,我直接把fiddler卸载了









