UPL:统一资源定位符
URL网络编程
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式:
http//localhost:8080/examples/鸣人.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
package com.atguigu.java2;
import java.net.MalformedURLException;
import java.net.URL;
/**
* URL网络编程
* 1.URL:统一资源定位符,对应着互联网的某一资源地址
* 2.格式:
* http//localhost:8080/examples/鸣人.jpg?username=Tom
* 协议 主机名 端口号 资源地址 参数列表
*
* @author Dxkstart
* @create 2021-06-05 18:28
*/
public class URLTest {
public static void main(String[] args) {
try {
// URL url = new URL("http//localhost:8080/examples/鸣人.jpg?username=Tom");
URL url = new URL("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E6%B3%B0%E5%8B%92%E6%96%AF%E5%A8%81%E5%A4%AB%E7%89%B9&hs=2&pn=0&spn=0&di=133650&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=1024190199%2C957797391&os=2779457430%2C3666039057&simid=4179486584%2C499975717&adpicid=0&lpn=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=0&oriquery=%E6%B3%B0%E5%8B%92%E6%96%AF%E5%A8%81%E5%A4%AB%E7%89%B9&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fgss0.baidu.com%2F7Po3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F060828381f30e9240f3bf75d4a086e061d95f73b.jpg%26refer%3Dhttp%3A%2F%2Fgss0.baidu.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1625482740%26t%3D1b6c35b17723bf9eef5a4891d5161d4d&fromurl=ippr_z2C%24qAzdH3FAzdH3Fzit1w5_z%26e3Bkwt17_z%26e3Bv54AzdH3Fq7jfpt5gAzdH3Fm8b0ccccbl0dc9ml0d_z%26e3Bip4s&gsm=1&islist=&querylist=");
//一个火狐浏览器上泰勒斯威夫特的图片
// public String getProtocal() 获取该URL的协议名
System.out.println(url.getProtocol());
// public String getHost() 获取该URL的主机名
System.out.println(url.getHost());
// public String getProt() 获取该URL的端口名
System.out.println(url.getPort());
// public String getPath() 获取该URL的文件路径
System.out.println(url.getPath());
// public String getFile() 获取该URL的文件名
System.out.println(url.getFile());
// public String getQuery() 获取该URL的查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
package com.atguigu.java2;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author Dxkstart
* @create 2021-06-05 18:45
*/
public class URLTEst2 {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E6%B3%B0%E5%8B%92%E6%96%AF%E5%A8%81%E5%A4%AB%E7%89%B9&hs=2&pn=0&spn=0&di=133650&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&ie=utf-8&oe=utf-8&cl=2&lm=-1&cs=1024190199%2C957797391&os=2779457430%2C3666039057&simid=4179486584%2C499975717&adpicid=0&lpn=0&ln=30&fr=ala&fm=&sme=&cg=&bdtype=0&oriquery=%E6%B3%B0%E5%8B%92%E6%96%AF%E5%A8%81%E5%A4%AB%E7%89%B9&objurl=https%3A%2F%2Fgimg2.baidu.com%2Fimage_search%2Fsrc%3Dhttp%3A%2F%2Fgss0.baidu.com%2F7Po3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F060828381f30e9240f3bf75d4a086e061d95f73b.jpg%26refer%3Dhttp%3A%2F%2Fgss0.baidu.com%26app%3D2002%26size%3Df9999%2C10000%26q%3Da80%26n%3D0%26g%3D0n%26fmt%3Djpeg%3Fsec%3D1625482740%26t%3D1b6c35b17723bf9eef5a4891d5161d4d&fromurl=ippr_z2C%24qAzdH3FAzdH3Fzit1w5_z%26e3Bkwt17_z%26e3Bv54AzdH3Fq7jfpt5gAzdH3Fm8b0ccccbl0dc9ml0d_z%26e3Bip4s&gsm=1&islist=&querylist=");
//一个火狐浏览器上泰勒斯威夫特的图片
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("day10\\222.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(urlConnection != null) {
urlConnection.disconnect();//断开连接
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}