首先盲写的一个传输文件的方法,但测试发现了一个非常不容易发现的问题,这里先说明一下。错误的代码如下: 1 package com.TCP.java; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 imp ...
首先盲写的一个传输文件的方法,但测试发现了一个非常不容易发现的问题,这里先说明一下。
错误的代码如下:
1 package com.TCP.java; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import java.io.OutputStream; 10 import java.net.InetAddress; 11 import java.net.ServerSocket; 12 import java.net.Socket; 13 14 import org.junit.Test; 15 16 //从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。 17 public class TestTCP3 { 18 //客户端 19 @Test 20 public void client(){ 21 Socket socket = null; 22 OutputStream os = null; 23 FileInputStream fis = null; 24 InputStream is = null; 25 try { 26 //创建一个Socket的对象 27 socket = new Socket(InetAddress.getByName("192.168.1.101"),9090); 28 //从本地获取一个文件发送给服务端 29 os = socket.getOutputStream(); 30 fis = new FileInputStream(new File("findLei.jpg")); 31 byte[] b = new byte[1024]; 32 int length; 33 while((length = fis.read(b)) != -1){ 34 os.write(b,0,length); 35 } 36 socket.shutdownOutput(); 37 //接收来自服务端的信息 38 is = socket.getInputStream(); 39 byte[] b1 = new byte[1024]; 40 int length1; 41 while((length1 = is.read(b1)) != -1){ 42 String str = new String(b1,0,length1); 43 System.out.println(str); 44 } 45 }catch (IOException e) { 46 e.printStackTrace(); 47 } 48 finally{ 49 //关闭相应的流和Socket对象 50 if(is != null){ 51 try { 52 is.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 if(fis != null){ 58 try { 59 fis.close(); 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64 if(os != null){ 65 try { 66 os.close(); 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 } 71 if(socket != null){ 72 try { 73 socket.close(); 74 } catch (IOException e) { 75 e.printStackTrace(); 76 } 77 } 78 } 79 } 80 81 //服务端 82 @Test 83 public void server(){ 84 ServerSocket ss = null; 85 Socket s = null; 86 InputStream is = null; 87 FileOutputStream fos = null; 88 OutputStream os = null; 89 try { 90 //创建一个ServerSocket的对象 91 ss = new ServerSocket(9090); 92 //调用accept()方法,返回一个Socket对象 93 s = ss.accept(); 94 //将从客户端发送的信息保存到本地 95 is = s.getInputStream(); 96 fos = new FileOutputStream("D:/Test"); 97 // Random random = new Random(); 98 // fos = new FileOutputStream("D:/Test/" + String.valueOf(random.nextInt(90)) + ".jpg"); 99 // fos = new FileOutputStream("find2.jpg");100 byte[] b = new byte[1024];101 int length;102 while((length = is.read(b)) != -1){103 fos.write(b, 0, length);104 }105 //发送接收成功的消息106 os = s.getOutputStream();107 os.write("接收成功".getBytes());108 } catch (FileNotFoundException e) {109 e.printStackTrace();110 } catch (IOException e) {111 e.printStackTrace();112 }113 finally{114 //关闭相应的流及ServerSocket,Socket对象115 if(os != null){116 try {117 os.close();118 } catch (IOException e) {119 e.printStackTrace();120 }121 }122 if(fos != null){123 try {124 fos.close();125 } catch (IOException e) {126 e.printStackTrace();127 }128 }129 if(is != null){130 try {131 is.close();132 } catch (IOException e) {133 e.printStackTrace();134 }135 }136 if(s != null){137 try {138 s.close();139 } catch (IOException e) {140 e.printStackTrace();141 }142 }143 if(ss != null){144 try {145 ss.close();146 } catch (IOException e) {147 e.printStackTrace();148 }149 }150 }151 }152 }
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:利用TCP 客户端
关键词:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。