第十四章_网络编程-Day26-《Java学习知识库》

admin 2025-11-02 01:04:37 编程 来源:ZONE.CI 全球网 0 阅读模式
  • 1、昨日复习
  • 2、对象流
  • 3、随机存取文件流
  • 4、网络编程

    1、昨日复习

    1.说明流的三种分类方式流向:输入流、输出流数据单位:字节流、字符流流的角色:节点流、处理流2.写出4个IO流中的抽象基类,4个文件流,4个缓冲流InputStream FileXxx BufferedXxxOutputStreamReaderWriter

    InputStreamReader:父类Reader异常:XxxException XxxError

    RandomAccessFile3.字节流与字符流的区别与使用情境字节流:read(byte[] buffer) / read() 非文本文件字符流:read(char[] cbuf) / read() 文本文件4.使用缓冲流实现a.jpg文件复制为b.jpg文件的操作BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(“a.jpg”)));

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(“b.jpg”)));

    byte[] buffer = new byte[1024];int len;while((len = bis.read(buffer))!= -1){bos.write(buffer,0,len);}

    bos.close();bis.close();

    //此时的异常应该使用try-catch-finally处理。

    5.转换流是哪两个类,分别的作用是什么?请分别创建两个类的对象。InputStreamReader:将输入的字节流转换为输入的字符流。解码OutputStreamWriter:将输出的字符流转换为输出的字节流。编码

    InputStreamReader isr = new InputStreamReader(new FileInputStream(“a.txt”),”utf-8”);

    OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(“b.txt”),”gbk”);

    对后面学习的启示:客户端/浏览器 <—————->后台(java、go、Python、node.js、PHP)<—————->数据库要求前前后后使用的字符集都统一为:UTF-8

    2、对象流

    ObjectInputStream和OjbectOutputSteam  用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。  序列化:用ObjectOutputStream类保存基本类型数据或对象的机制  反序列化:用ObjectInputStream类读取基本类型数据或对象的机制  ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量


    对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。//当其它程序获取了这种二进制流,就可以恢复成原来的Java对象 序列化的好处在于可将任何实现了Serializable接口的对象转化为字节数据,使其在保存和传输时可被还原 序列化是 RMI(Remote Method Invoke – 远程方法调用)过程的参数和返回值都必须实现的机制,而 RMI 是 JavaEE 的基础。因此序列化机制是JavaEE 平台的基础 如果需要让某个对象支持序列化机制,则必须让对象所属的类及其属性是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一。否则,会抛出NotSerializableException异常 Serializable Externalizable

    1. package com.atguigu.java1;
    2. import org.junit.Test;
    3. import java.io.*;
    4. /*
    5. 对象留的使用:
    6. 1、ObjectInputStream和ObjectOutputStream
    7. 2、作用:用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
    8. 3、要想一个java对象是可序列化的,需要满足相应的要求。
    9. ①需要实现接口:Serializable
    10. ②凡是实现Serializable接口的类都有一个表示序列化版本标识符的全局常量,具体的值没有要求:private static final long serialVersionUID;
    11. 4、除了当前类需要实现Serializable接口之外,还必须保证其内部所有的属性
    12. 也必须是可序列化的。(默认情况下,基本数据类型也可以序列化)
    13. 补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
    14. */
    15. public class ObjectInputStreamTest {
    16. /*
    17. 序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
    18. 使用ObjectOutputStream实现
    19. */
    20. @Test
    21. public void test1() {
    22. ObjectOutputStream oos = null;
    23. try {
    24. oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
    25. oos.writeObject(new String("我爱北京天安门"));
    26. oos.flush();//刷新操作
    27. oos.writeObject(new Person("Tom",23));
    28. oos.flush();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }finally {
    32. try {
    33. oos.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. }
    39. /*
    40. 反序列化:将磁盘文件中的对象还原为内存中的一个java对象
    41. 使用ObjectInputStream来实现
    42. */
    43. @Test
    44. public void test2(){
    45. ObjectInputStream ois = null;
    46. try {
    47. ois = new ObjectInputStream(new FileInputStream("object.dat"));
    48. Object obj = ois.readObject();
    49. String str = (String) obj;
    50. Person p = (Person) ois.readObject();
    51. System.out.println(str);
    52. System.out.println(p);
    53. } catch (IOException e) {
    54. e.printStackTrace();
    55. } catch (ClassNotFoundException e) {
    56. e.printStackTrace();
    57. }finally {
    58. if (ois!=null) {
    59. try {
    60. ois.close();
    61. } catch (IOException e) {
    62. e.printStackTrace();
    63. }
    64. }
    65. }
    66. }
    67. }

    3、随机存取文件流

    1. package com.atguigu.java1;
    2. import org.junit.Test;
    3. import java.io.File;
    4. import java.io.FileNotFoundException;
    5. import java.io.IOException;
    6. import java.io.RandomAccessFile;
    7. /*
    8. RandomAccessFile的使用:
    9. 1、RandomAccessFile直接继承于java.lang.Object类,实现了DataInput和DataOutput接口
    10. 2、RandomAccessFile既可以作为一个输入流,又可以作为一个输出流
    11. 3、如果RandomAccessFile作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建
    12. 如果写出到的文件存在,则会对原有文件内容进行覆盖。(默认情况下,从头覆盖)
    13. 4、可以通过相关的操作,实现RandomAccessFile“插入”数据的效果
    14. */
    15. public class RandomAccessFileTest {
    16. @Test
    17. public void test1() {
    18. RandomAccessFile raf1 = null;
    19. RandomAccessFile raf2 = null;
    20. try {
    21. raf1 = new RandomAccessFile(new File("大家好.png"), "r");
    22. raf2 = new RandomAccessFile(new File("大家好1.png"), "rw");
    23. byte[] buffer = new byte[1024];
    24. int len;
    25. while ((len = raf1.read(buffer)) != -1) {
    26. raf2.write(buffer, 0, len);
    27. }
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. } finally {
    31. if (raf1 != null) {
    32. try {
    33. raf1.close();
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. if (raf2 != null) {
    39. try {
    40. raf2.close();
    41. } catch (IOException e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. }
    46. }
    47. @Test
    48. public void test2() {
    49. RandomAccessFile raf1 = null;
    50. try {
    51. raf1 = new RandomAccessFile("hello.txt", "rw");
    52. raf1.seek(new File("hello.txt").length());//指针调到脚标为3的位置
    53. raf1.write("xyz".getBytes());
    54. } catch (IOException e) {
    55. e.printStackTrace();
    56. } finally {
    57. if (raf1 != null) {
    58. try {
    59. raf1.close();
    60. } catch (IOException e) {
    61. e.printStackTrace();
    62. }
    63. }
    64. }
    65. }
    66. @Test
    67. public void test3() throws IOException {
    68. RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");
    69. raf1.seek(3);
    70. //保存指针3后面的数据
    71. StringBuilder builder = new StringBuilder((int) new File("hello.txt").length());
    72. byte[] buffer = new byte[20];
    73. int len;
    74. while ((len=raf1.read(buffer))!=-1){
    75. builder.append(new String(buffer,0,len));
    76. }
    77. //调回指针,写入:“xyz”
    78. raf1.seek(3);
    79. raf1.write("xyz".getBytes());
    80. raf1.write(builder.toString().getBytes());
    81. raf1.close();
    82. }
    83. }

    RandomAccessFile类为用户提供了两种构造方法:1、RandomAccessFile(File file, String mode)2、RandomAccessFile(String name, String mode)其实第二种构造方法也是new一个File出来再调用第一种构造方法,建议使用第一种构造方法,因为第一篇文章就说了File是IO的基础,有一个File不仅仅可以通过RandomAccessFile对文件进行操作,也可以通过File对象对文件进行操作。至于mode,Java给开发者提供了四种mode:

    模 式 作 用
    r 表示以只读方式打开,调用结果对象的任何write方法都将导致抛出IOException
    rw 打开以便读取和写入,如果该文件尚不存在,则尝试创建该文件
    rws 打开以便读取和写入,相对于”rw”,还要求对文件内容或元数据的每个更新都同步写入到底层存储设备
    rwd 打开以便读取和写入,相对于”rw”,还要求对文件内容的每个更新都同步写入到底层存储设备

    注意第二点”rw”模式,对rw模式的解释意味着Java并不强求指定的路径下一定存在某个文件,假如文件不存在,会自动创建RandomAccessFile中的方法RandomAccessFile中有如下一些常用方法:

    方 法 作 用
    void close() 重要,关闭此随机访问文件流并释放与该流关联的所有系统资源
    FileChannel getChannel() 返回与此文件关联的唯一FileChannel对象,NIO用到
    long getFilePointer() 返回此文件中的当前偏移量
    long length() 返回此文件的长度
    int read() 从此文件中读取一个数据字节
    int read(byte[] b) 将最多b.length个数据字节从此文件读入byte数组,返回读入的总字节数,如果由于已经达到文件末尾而不再有数据,则返回-1。在至少一个输入字节可用前,此方法一直阻塞
    int read(byte[] b, int off, int len) 将最多len个数据字节从此文件的指定初始偏移量off读入byte数组
    boolean readBoolean() 从此文件读取一个boolean,其余readByte()、readChar()、readDouble()等类似
    String readLine() 从此文件读取文本的下一行
    void seek(long pos) 重要,设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作
    int skipBytes(int n) 重要,尝试跳过输入的n个字节以丢弃跳过的字节,返回跳过的字节数
    void write(byte[] b) 将b.length个字节从指定byte数组写入到此文件中
    void write(byte[] b, int off, int len) 将len个字节从指定byte数组写入到此文件,并从偏移量off处开始
    void write(int b) 向此文件写入指定的字节
    void writeBoolean(boolean v) 按单字节值将boolean写入该文件,其余writeByte(int v)、writeBytes(String s)、writeChar(int v)等都类似

    QQ截图20220126154010.png

    4、网络编程

    网络基础 计算机网络: 把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大、功能强的网络系统,从而使众多的计算机可以方便地互相传递信息、共享硬件、软件、数据信息等资源。 网络编程的目的: 直接或间接地通过网络协议与其它计算机实现数据交换,进行通讯。 网络编程中有两个主要的问题: 如何准确地定位网络上一台或多台主机;定位主机上的特定的应用 找到主机后如何可靠高效地进行数据传输如何实现网络中的主机互相通信 通信双方地址 IP 端口号 一定的规则(即:网络通信协议。有两套参考模型) OSI参考模型:模型过于理想化,未能在因特网上进行广泛推广 TCP/IP参考模型(或TCP/IP协议):事实上的国际标准。QQ截图20220126165501.pngQQ截图20220126165711.png


    QQ截图20220126170520.png本地回路地址:127.0.0.1 对应着:localhostQQ截图20220126172737.png


    QQ截图20220126170533.png


    QQ截图20220126174223.pngQQ截图20220126174337.pngQQ截图20220126175521.pngQQ截图20220126175539.png什么是SocketSocket的概念很简单,它是网络上运行的两个程序间双向通讯的一端,既可以接收请求,也可以发送请求,利用它可以较为方便地编写网络上数据的传递。所以简而言之,Socket就是进程通信的端点,Socket之间的连接过程可以分为几步:1、服务器监听服务器端Socket并不定位具体的客户端Socket,而是处于等待连接的状态,实时监控网络状态2、客户端请求客户端Socket发出连接请求,要连接的目标是服务端Socket。为此,客户端Socket必须首先描述它要连接的服务端Socket,指出服务端Socket的地址和端口号,然后就向服务端Socket提出连接请求3、连接确认当服务端Socket监听到或者说是接收到客户端Socket的连接请求,它就响应客户端Socket的请求,建立一个新的线程,把服务端Socket的描述发给客户端,一旦客户端确认了此描述,连接就好了。而服务端Socket继续处于监听状态,继续接收其他客户端套接字的连接请求

    1. package com.atguigu.java2;
    2. import org.junit.Test;
    3. import java.io.*;
    4. import java.net.InetAddress;
    5. import java.net.ServerSocket;
    6. import java.net.Socket;
    7. /*
    8. 实现TCP的网络编程
    9. 例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
    10. */
    11. public class TCPTest1 {
    12. //客户端
    13. @Test
    14. public void client() {
    15. Socket socket = null;
    16. OutputStream os = null;
    17. try {
    18. //1、创建Socket对象,指明服务器短的IP和端口号
    19. InetAddress inet = InetAddress.getByName("127.0.0.1");
    20. socket = new Socket(inet, 9900);
    21. //2、获取一个输出流,用于输出数据
    22. os = socket.getOutputStream();
    23. //3、写出数据的操作
    24. os.write("你好,我是客户端mm".getBytes());
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. } finally {
    28. //4、资源关闭
    29. if (os != null) {
    30. try {
    31. os.close();
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. if (socket != null) {
    37. try {
    38. socket.close();
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. }
    43. }
    44. }
    45. //服务端
    46. @Test
    47. public void server() {
    48. ServerSocket ss = null;
    49. Socket socket = null;
    50. InputStream is = null;
    51. ByteArrayOutputStream baos = null;
    52. try {
    53. //1、创建服务器端的ServerSocket,指明自己的端口号
    54. ss = new ServerSocket(9900);
    55. //2、调用accept表明接收来自客户端的socket
    56. socket = ss.accept();
    57. //3、获取输入流
    58. is = socket.getInputStream();
    59. //不建议这样写,可能会有乱码
    60. /*byte[] buffer = new byte[20];
    61. int len;
    62. while ((len=is.read())!=-1){
    63. String str = new String(buffer,0,len);
    64. System.out.println(str);
    65. }*/
    66. //4、读取输入流中的数据
    67. baos = new ByteArrayOutputStream();
    68. byte[] buffer = new byte[100];
    69. int len;
    70. while ((len = is.read(buffer)) != -1) {
    71. baos.write(buffer, 0, len);
    72. }
    73. System.out.println(baos.toString());
    74. System.out.println("收到了来自于:"+socket.getInetAddress().getHostAddress()+"的数据");
    75. } catch (IOException e) {
    76. e.printStackTrace();
    77. } finally {
    78. if (baos != null) {
    79. try {
    80. baos.close();
    81. } catch (IOException e) {
    82. e.printStackTrace();
    83. }
    84. }
    85. if (is != null) {
    86. try {
    87. is.close();
    88. } catch (IOException e) {
    89. e.printStackTrace();
    90. }
    91. }
    92. if (socket != null) {
    93. try {
    94. socket.close();
    95. } catch (IOException e) {
    96. e.printStackTrace();
    97. }
    98. }
    99. if (ss != null) {
    100. try {
    101. ss.close();
    102. } catch (IOException e) {
    103. e.printStackTrace();
    104. }
    105. }
    106. }
    107. }
    108. }
    1. package com.atguigu.java2;
    2. import org.junit.Test;
    3. import java.io.*;
    4. import java.net.InetAddress;
    5. import java.net.ServerSocket;
    6. import java.net.Socket;
    7. import java.net.UnknownHostException;
    8. /*
    9. 实现TCP的网络编程
    10. 例子2:客户端发送文件给服务端,服务端将文件保存在本地。
    11. */
    12. public class TCPTest2 {
    13. @Test
    14. public void client(){
    15. Socket socket = null;
    16. OutputStream os = null;
    17. FileInputStream fis = null;
    18. try {
    19. //1、
    20. socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
    21. //2、
    22. os = socket.getOutputStream();
    23. //3、
    24. fis = new FileInputStream(new File("java学习路线图.png"));
    25. //4、
    26. byte[] buffer = new byte[1024];
    27. int len;
    28. while ((len = fis.read(buffer)) != -1) {
    29. os.write(buffer, 0, len);
    30. }
    31. } catch (IOException e) {
    32. e.printStackTrace();
    33. } finally {
    34. if (fis!=null){
    35. try {
    36. fis.close();
    37. } catch (IOException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. if (os!=null){
    42. try {
    43. os.close();
    44. } catch (IOException e) {
    45. e.printStackTrace();
    46. }
    47. }
    48. if (socket!=null){
    49. try {
    50. socket.close();
    51. } catch (IOException e) {
    52. e.printStackTrace();
    53. }
    54. }
    55. }
    56. }
    57. @Test
    58. public void server(){
    59. ServerSocket ss = null;
    60. Socket socket = null;
    61. InputStream is = null;
    62. FileOutputStream fos = null;
    63. try {
    64. ss = new ServerSocket(9090);
    65. socket = ss.accept();
    66. is = socket.getInputStream();
    67. fos = new FileOutputStream(new File("java学习路线图1.png"));
    68. byte[] buffer = new byte[1024];
    69. int len;
    70. while ((len = is.read(buffer))!=-1){
    71. fos.write(buffer,0,len);
    72. }
    73. } catch (IOException e) {
    74. e.printStackTrace();
    75. }finally {
    76. if (fos!=null){
    77. try {
    78. fos.close();
    79. } catch (IOException e) {
    80. e.printStackTrace();
    81. }
    82. }
    83. if (is!=null){
    84. try {
    85. is.close();
    86. } catch (IOException e) {
    87. e.printStackTrace();
    88. }
    89. }
    90. if (socket!=null){
    91. try {
    92. socket.close();
    93. } catch (IOException e) {
    94. e.printStackTrace();
    95. }
    96. }
    97. if (ss!=null){
    98. try {
    99. ss.close();
    100. } catch (IOException e) {
    101. e.printStackTrace();
    102. }
    103. }
    104. }
    105. }
    106. }
    1. package com.atguigu.java2;
    2. import org.junit.Test;
    3. import java.io.*;
    4. import java.net.InetAddress;
    5. import java.net.ServerSocket;
    6. import java.net.Socket;
    7. /*
    8. 例题3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。
    9. 并关闭相应的连接。
    10. */
    11. public class TCPTest3 {
    12. @Test
    13. public void client() {
    14. Socket socket = null;
    15. OutputStream os = null;
    16. FileInputStream fis = null;
    17. InputStream is = null;
    18. ByteArrayOutputStream baos = null;
    19. try {
    20. //1、
    21. socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
    22. //2、
    23. os = socket.getOutputStream();
    24. //3、
    25. fis = new FileInputStream(new File("java学习路线图.png"));
    26. //4、
    27. byte[] buffer = new byte[1024];
    28. int len;
    29. while ((len = fis.read(buffer)) != -1) {
    30. os.write(buffer, 0, len);
    31. }
    32. //关闭数据的输出
    33. socket.shutdownOutput();
    34. //接收来自服务器端的数据,并显示到控制台上
    35. is = socket.getInputStream();
    36. baos = new ByteArrayOutputStream();
    37. byte[] buffer2 = new byte[20];
    38. int len2;
    39. while ((len2 = is.read(buffer2)) != -1) {
    40. baos.write(buffer2, 0, len2);
    41. }
    42. System.out.println(baos.toString());
    43. } catch (IOException e) {
    44. e.printStackTrace();
    45. } finally {
    46. if (fis != null) {
    47. try {
    48. fis.close();
    49. } catch (IOException e) {
    50. e.printStackTrace();
    51. }
    52. }
    53. if (os != null) {
    54. try {
    55. os.close();
    56. } catch (IOException e) {
    57. e.printStackTrace();
    58. }
    59. }
    60. if (socket != null) {
    61. try {
    62. socket.close();
    63. } catch (IOException e) {
    64. e.printStackTrace();
    65. }
    66. }
    67. if (is != null) {
    68. try {
    69. is.close();
    70. } catch (IOException e) {
    71. e.printStackTrace();
    72. }
    73. }
    74. if (baos != null) {
    75. try {
    76. baos.close();
    77. } catch (IOException e) {
    78. e.printStackTrace();
    79. }
    80. }
    81. }
    82. }
    83. @Test
    84. public void server() {
    85. ServerSocket ss = null;
    86. Socket socket = null;
    87. InputStream is = null;
    88. FileOutputStream fos = null;
    89. OutputStream os = null;
    90. try {
    91. ss = new ServerSocket(9090);
    92. socket = ss.accept();
    93. is = socket.getInputStream();
    94. fos = new FileOutputStream(new File("java学习路线图2.png"));
    95. byte[] buffer = new byte[1024];
    96. int len;
    97. while ((len = is.read(buffer)) != -1) {
    98. fos.write(buffer, 0, len);
    99. }
    100. System.out.println("图片传输完成");
    101. //服务器端给客户端反馈
    102. os = socket.getOutputStream();
    103. os.write("你好,客户,照片我已经收到了,非常漂亮".getBytes());
    104. } catch (IOException e) {
    105. e.printStackTrace();
    106. } finally {
    107. if (fos != null) {
    108. try {
    109. fos.close();
    110. } catch (IOException e) {
    111. e.printStackTrace();
    112. }
    113. }
    114. if (is != null) {
    115. try {
    116. is.close();
    117. } catch (IOException e) {
    118. e.printStackTrace();
    119. }
    120. }
    121. if (socket != null) {
    122. try {
    123. socket.close();
    124. } catch (IOException e) {
    125. e.printStackTrace();
    126. }
    127. }
    128. if (ss != null) {
    129. try {
    130. ss.close();
    131. } catch (IOException e) {
    132. e.printStackTrace();
    133. }
    134. }
    135. if (os != null) {
    136. try {
    137. os.close();
    138. } catch (IOException e) {
    139. e.printStackTrace();
    140. }
    141. }
    142. }
    143. }
    144. }

    PPT36页,练习题可以试试。

    1. package com.atguigu.java2;
    2. import org.junit.Test;
    3. import java.io.IOException;
    4. import java.net.*;
    5. /*
    6. UDPd协议的网络编程
    7. */
    8. public class UDPTest {
    9. //发送端
    10. @Test
    11. public void test1() throws IOException {
    12. DatagramSocket socket = new DatagramSocket();
    13. String str = "我是UDP方式发送的导弹";
    14. byte[] data = str.getBytes();
    15. InetAddress inet = InetAddress.getLocalHost();
    16. DatagramPacket packet = new DatagramPacket(data,data.length,inet,9090);
    17. socket.send(packet);
    18. socket.close();
    19. }
    20. //接收端
    21. @Test
    22. public void test2() throws IOException {
    23. DatagramSocket socket = new DatagramSocket(9090);
    24. byte[] buffer = new byte[100];
    25. DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
    26. socket.receive(packet);
    27. System.out.println(new String(packet.getData(),0,packet.getLength()));
    28. socket.close();
    29. }
    30. }
    1. package com.atguigu.java2;
    2. import java.net.MalformedURLException;
    3. import java.net.URL;
    4. /*
    5. URL网络编程
    6. 1、URL(Uniform Resource Locator):统一资源定位符,它表示 Internet 上某一资源的地址。
    7. 2、格式:
    8. http://localhost:8080/example/beauty.jpg?username=Tom
    9. 协议 主机名 端口号 资源地址 参数列表
    10. */
    11. public class URLTest {
    12. public static void main(String[] args){
    13. try {
    14. URL url = new URL("http://localhost:8080/example/beauty.jpg?username=Tom");
    15. // public String getProtocol( ) 获取该URL的协议名
    16. System.out.println(url.getProtocol());//http
    17. // public String getHost( ) 获取该URL的主机名
    18. System.out.println(url.getHost());//localhost
    19. // public String getPort( ) 获取该URL的端口号
    20. System.out.println(url.getPort());//8080
    21. // public String getPath( ) 获取该URL的文件路径
    22. System.out.println(url.getPath());///example/beauty.jpg
    23. // public String getFile( ) 获取该URL的文件名
    24. System.out.println(url.getFile());///example/beauty.jpg?username=Tom
    25. // public String getQuery( ) 获取该URL的查询名
    26. System.out.println(url.getQuery());//username=Tom
    27. } catch (MalformedURLException e) {
    28. e.printStackTrace();
    29. }
    30. }
    31. }
    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.MalformedURLException;
    7. import java.net.URL;
    8. import java.net.URLConnection;
    9. public class URLTest1 {
    10. public static void main(String[] args){
    11. HttpURLConnection urlConnection = null;
    12. InputStream is = null;
    13. FileOutputStream fos = null;
    14. try {
    15. // URL url = new URL("http://localhost:8080/example/beauty.jpg");
    16. URL url = new URL("https://cn.bing.com/images/search?view=detailV2&ccid=JQ%2bVhVrs&id=FCAE6777DDD995B9CF11C3FEF6D5D753230E0706&thid=OIP.JQ-VhVrsScl0rLNrPtMlcQHaE8&mediaurl=https%3a%2f%2ftse1-mm.cn.bing.net%2fth%2fid%2fR-C.250f95855aec49c974acb36b3ed32571%3frik%3dBgcOI1PX1fb%252bww%26riu%3dhttp%253a%252f%252fwww.desktx.com%252fd%252ffile%252fwallpaper%252fscenery%252f20170120%252f387b1a5181ebeddbec90fd5f19e606ce.jpg%26ehk%3dFeb%252bz1leZKOVuTS20av3z7LKELRP0HH277cc6aSrAeI%253d%26risl%3d%26pid%3dImgRaw%26r%3d0&exph=1334&expw=2000&q=%e5%9b%be%e7%89%87&simid=608018209402674333&FORM=IRPRST&ck=77D525DA175DD85099F2804D96188A98&selectedIndex=0&ajaxhist=0&ajaxserp=0");
    17. urlConnection = (HttpURLConnection) url.openConnection();
    18. urlConnection.connect();
    19. is = urlConnection.getInputStream();
    20. fos = new FileOutputStream("Day26\\beauty2.jpg");
    21. byte[] buffer = new byte[1024];
    22. int len;
    23. while ((len = is.read(buffer))!=-1){
    24. fos.write(buffer,0,len);
    25. }
    26. System.out.println("下载完成");
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. } finally {
    30. if (fos!=null){
    31. try {
    32. fos.close();
    33. } catch (IOException e) {
    34. e.printStackTrace();
    35. }
    36. }
    37. if (is!=null){
    38. try {
    39. is.close();
    40. } catch (IOException e) {
    41. e.printStackTrace();
    42. }
    43. }
    44. if (urlConnection!=null){
    45. urlConnection.disconnect();
    46. }
    47. }
    48. }
    49. }

    尚硅谷宋红康第14章_网络编程.pdf

    以太坊cppgolang区别 编程

    以太坊cppgolang区别

    以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
    progolang 编程

    progolang

    Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
    golangn个发送者 编程

    golangn个发送者

    Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
    golang技能图谱 编程

    golang技能图谱

    从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
    评论:0   参与:  10