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. * 例题3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。
    10. * 并关闭相应连接。
    11. *
    12. * @author Dxkstart
    13. * @create 2021-06-05 17:35
    14. */
    15. public class TCPTest3 {
    16. @Test
    17. public void client(){
    18. Socket socket = null;
    19. OutputStream os = null;
    20. FileInputStream fis = null;
    21. try {
    22. //1.
    23. socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
    24. //2.
    25. os = socket.getOutputStream();
    26. //3.
    27. fis = new FileInputStream(new File("鸣人.jpg"));
    28. //4.
    29. byte[] buffer = new byte[1024];
    30. int len;
    31. while ((len = fis.read(buffer)) != -1){
    32. os.write(buffer,0,len);
    33. }
    34. //关闭数据的输出
    35. socket.shutdownOutput();//解决阻塞
    36. //5.接收来自于服务器端的数据,并显示到控制台上
    37. InputStream is = socket.getInputStream();
    38. ByteArrayOutputStream baos = new ByteArrayOutputStream();
    39. byte[] buffer1 = new byte[20];
    40. int len1;
    41. while ((len1 = is.read(buffer1)) != -1){
    42. baos.write(buffer1,0,len1);
    43. }
    44. System.out.println(baos.toString());
    45. } catch (IOException e) {
    46. e.printStackTrace();
    47. } finally {
    48. try {
    49. if(fis != null) {
    50. fis.close();
    51. }
    52. } catch (IOException e) {
    53. e.printStackTrace();
    54. }
    55. try {
    56. if(os != null) {
    57. os.close();
    58. }
    59. } catch (IOException e) {
    60. e.printStackTrace();
    61. }
    62. try {
    63. if(socket != null) {
    64. socket.close();
    65. }
    66. } catch (IOException e) {
    67. e.printStackTrace();
    68. }
    69. }
    70. }
    71. @Test
    72. public void server(){
    73. ServerSocket ss = null;
    74. Socket socket = null;
    75. InputStream is = null;
    76. FileOutputStream fos = null;
    77. try {
    78. //1.
    79. ss = new ServerSocket(9090);
    80. //2.
    81. socket = ss.accept();
    82. //3.
    83. is = socket.getInputStream();
    84. //4.
    85. fos = new FileOutputStream(new File("鸣人4.jpg"));
    86. //5.
    87. byte[] buffer = new byte[1024];
    88. int len;
    89. while ((len = is.read(buffer)) != -1){
    90. fos.write(buffer,0,len);
    91. }
    92. //6.服务端给与客户端反馈
    93. OutputStream os = socket.getOutputStream();
    94. os.write("你好美女,照片我已收到,非常漂亮!".getBytes());
    95. } catch (IOException e) {
    96. e.printStackTrace();
    97. } finally {
    98. try {
    99. if(fos != null) {
    100. fos.close();
    101. }
    102. } catch (IOException e) {
    103. e.printStackTrace();
    104. }
    105. try {
    106. if(is != null) {
    107. is.close();
    108. }
    109. } catch (IOException e) {
    110. e.printStackTrace();
    111. }
    112. try {
    113. if(socket != null) {
    114. socket.close();
    115. }
    116. } catch (IOException e) {
    117. e.printStackTrace();
    118. }
    119. try {
    120. if(ss != null) {
    121. ss.close();
    122. }
    123. } catch (IOException e) {
    124. e.printStackTrace();
    125. }
    126. }
    127. }
    128. }