当前位置:网站首页>URL的使用下载资源
URL的使用下载资源
2022-07-26 14:03:00 【看小虫子】
##URL的使用
package UdpCommuncation;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL {
public static void main(String[] args) throws MalformedURLException {
URL url=new URL("http://localhost:8080/helloworld/index.jsp?useername=yan=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());//参数
}
}
使用URL下载资源
需要打开Tomcat
package UdpCommuncation;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Down {
public static void main(String[] args) throws IOException {
//1,下载地址
URL url=new URL("https://m704.music.126.net/20220725150910/04899645bb2ebc8ef5dbf6bcb1e07814/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/15384022047/73a2/a864/8dbb/1bdbc100f5143a96c0c34c5698e61732.m4a?authSecret=00000182341951661b4b0aaba0c8b0c8");
//2,连接到这个资源 HTTP
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("mi.m4a");
byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}
边栏推荐
- @A thousand lines of work, ride the cloud together!
- Understand the meaning of length in MySQL data types
- [oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer
- Completable future practical usage
- JS, e.pagex, pagey modal box drag
- 向路由组件传递参数
- How can red star Macalline design cloud upgrade the traditional home furnishing industry in ten minutes to produce film and television level interior design effects
- Prediction and value evaluation of technology fusion relationship based on multiple features
- Pytoch learning notes (I) installation and use of common functions
- Mobile dual finger scaling event (native), e.originalevent.touches
猜你喜欢

循环队列(c语言实现)

The shell script in Jenkins fails to execute but does not exit by itself

@A thousand lines of work, ride the cloud together!

Jenkins 中 shell 脚本执行失败却不自行退出

.net6 encounter with the League of heroes - create a game assistant according to the official LCU API

12437 words, take you to explore the principle of RPC communication
![[oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer](/img/7e/4f652c4d3d72ad563ddbc1c1f1f50b.png)
[oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer

uni-app从创建到运行到微信开发者工具

Leetcode question type priority queue (TOPK question)

Tdsql-c serverless: help start-ups achieve cost reduction and efficiency increase
随机推荐
Pass parameters to the routing component
Multi task text classification model based on tag embedded attention mechanism
敏捷开发与DevOps的对比
[oauth2] VIII. Configuration logic of oauth2 login -oauth2loginconfigurer and oauth2clientconfigurer
MySQL sets auto increment for existing primary keys
Segmentation fault (core dumped)
C language Snake linked list and pointer practice
MLX90640 红外热成像仪测温传感器模块开发笔记(六)
初识Opencv4.X----图像透视变换
Go multithread communication, control coordination and main thread shutdown (sync.waitgroup)
基于双层主题模型的技术演化分析框架及其应用
Why does WPS refuse advertising?
Re bet overseas: Alibaba, jd.com and SF again fight for "internal power"
基于SPO语义三元组的疾病知识发现
Red * is added to the input box to indicate mandatory items
MySql的DDL和DML和DQL的基本语法
Disease knowledge discovery based on spo semantic triples
Redis的数据操作
C language_ Structure pointer variable introduction
【论文阅读】GRAW+:A Two-View Graph Propagation Method With Word Coupling for Readability Assessment