Java实现socket简单通讯传输doc文件或图片

    需要通过socket通讯传输word文件,其中word文件中有部分文字与图片,所以就是IO流读取文件再另外一端读写文件打印出来;

  1、发送端直接向接收端发送字符流通讯,如下是源码所示:

 1 package com.yss.util;
 2 
 3 import java.io.DataOutputStream;
 4 import java.io.File;
 5 import java.io.FileInputStream;
 6 import java.net.InetSocketAddress;
 7 import java.net.Socket;
 8 
 9 public class ClientTcpSend {
10     public static void main(String[] args) {
11         int length = 0;
12         byte[] sendBytes = null;
13         Socket socket = null;
14         DataOutputStream dos = null;
15         FileInputStream fis = null;
16 
17         try {
18             try {
19                 socket = new Socket();
20                 socket.connect(new InetSocketAddress("127.0.0.1", 33456),
21                                10 * 1000);
22                 dos = new DataOutputStream(socket.getOutputStream());
23                 File file = new File("C:\\任务\\2018年8月\\ceshi.docx");
24                 fis = new FileInputStream(file);
25                 sendBytes = new byte[1024];
26                 while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
27                     dos.write(sendBytes, 0, length);
28                     dos.flush();
29                 }
30             } finally {
31                 if (dos != null)
32                     dos.close();
33                 if (fis != null)
34                     fis.close();
35                 if (socket != null)
36                     socket.close();
37             }
38         } catch (Exception e) {
39             e.printStackTrace();
40         }
41     }
42 }

2、接收方通过监听一个固定的端口接收传输过来的内容,再打印在一个新建文件,已实现两个文件的转换:

 1 package com.yss.util;
 2 
 3 import java.io.DataInputStream;
 4 import java.io.File;
 5 import java.io.FileOutputStream;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 public class ServerTcpListener implements Runnable {
10     public static void main(String[] args) {
11         try {
12             final ServerSocket server = new ServerSocket(33456);
13             Thread th = new Thread(new Runnable() {
14                 public void run() {
15                     while (true) {
16                         try {
17                             System.out.println("开始监听...");
18                             Socket socket = server.accept();
19                             System.out.println("有链接");
20                             receiveFile(socket);
21                         } catch (Exception e) {
22                         }
23                     }
24                 }
25             });
26 
27             th.run(); //启动线程运行
28         } catch (Exception e) {
29             e.printStackTrace();
30         }
31     }
32 
33     public void run() {
34     }
35 
36     public static void receiveFile(Socket socket) {
37         byte[] inputByte = null;
38         int length = 0;
39         DataInputStream dis = null;
40         FileOutputStream fos = null;
41         try {
42             try {
43                 dis = new DataInputStream(socket.getInputStream());
44                 fos = new FileOutputStream(new File("C:\\任务\\2018年8月\\ceshi01.docx"));
45                 inputByte = new byte[1024];
46                 System.out.println("开始接收数据...");
47                 while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
48                     System.out.println(length);
49                     fos.write(inputByte, 0, length);
50                     fos.flush();
51                 }
52                 System.out.println("完成接收");
53             } finally {
54                 if (fos != null)
55                     fos.close();
56                 if (dis != null)
57                     dis.close();
58                 if (socket != null)
59                     socket.close();
60             }
61         } catch (Exception e) {
62         }
63     }
64 }

猜你喜欢

转载自www.cnblogs.com/ananaini/p/9455482.html