UPL:统一资源定位符

    image.png
    image.png
    URL网络编程
    1.URL:统一资源定位符,对应着互联网的某一资源地址
    2.格式:
    http//localhost:8080/examples/鸣人.jpg?username=Tom
    协议 主机名 端口号 资源地址 参数列表

    1. package com.atguigu.java2;
    2. import java.net.MalformedURLException;
    3. import java.net.URL;
    4. /**
    5. * URL网络编程
    6. * 1.URL:统一资源定位符,对应着互联网的某一资源地址
    7. * 2.格式:
    8. * http//localhost:8080/examples/鸣人.jpg?username=Tom
    9. * 协议 主机名 端口号 资源地址 参数列表
    10. *
    11. * @author Dxkstart
    12. * @create 2021-06-05 18:28
    13. */
    14. public class URLTest {
    15. public static void main(String[] args) {
    16. try {
    17. // URL url = new URL("http//localhost:8080/examples/鸣人.jpg?username=Tom");
    18. 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=");
    19. //一个火狐浏览器上泰勒斯威夫特的图片
    20. // public String getProtocal() 获取该URL的协议名
    21. System.out.println(url.getProtocol());
    22. // public String getHost() 获取该URL的主机名
    23. System.out.println(url.getHost());
    24. // public String getProt() 获取该URL的端口名
    25. System.out.println(url.getPort());
    26. // public String getPath() 获取该URL的文件路径
    27. System.out.println(url.getPath());
    28. // public String getFile() 获取该URL的文件名
    29. System.out.println(url.getFile());
    30. // public String getQuery() 获取该URL的查询名
    31. System.out.println(url.getQuery());
    32. } catch (MalformedURLException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. }
    1. package com.atguigu.java2;
    2. import java.io.FileOutputStream;
    3. import java.io.IOException;
    4. import java.io.InputStream;
    5. import java.net.HttpURLConnection;
    6. import java.net.URL;
    7. /**
    8. * @author Dxkstart
    9. * @create 2021-06-05 18:45
    10. */
    11. public class URLTEst2 {
    12. public static void main(String[] args) {
    13. HttpURLConnection urlConnection = null;
    14. InputStream is = null;
    15. FileOutputStream fos = null;
    16. try {
    17. 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=");
    18. //一个火狐浏览器上泰勒斯威夫特的图片
    19. urlConnection = (HttpURLConnection)url.openConnection();
    20. urlConnection.connect();
    21. is = urlConnection.getInputStream();
    22. fos = new FileOutputStream("day10\\222.jpg");
    23. byte[] buffer = new byte[1024];
    24. int len;
    25. while ((len = is.read(buffer)) != -1){
    26. fos.write(buffer,0,len);
    27. }
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. } finally {
    31. try {
    32. if(is != null) {
    33. is.close();
    34. }
    35. } catch (IOException e) {
    36. e.printStackTrace();
    37. }
    38. try {
    39. if(fos != null) {
    40. fos.close();
    41. }
    42. } catch (IOException e) {
    43. e.printStackTrace();
    44. }
    45. try {
    46. if(urlConnection != null) {
    47. urlConnection.disconnect();//断开连接
    48. }
    49. } catch (Exception e) {
    50. e.printStackTrace();
    51. }
    52. }
    53. }
    54. }