Java TCP服务端向客户端发送图片

 1 /**
 2  * 1、创建TCP服务端,TCP客户端
 3  * 2、服务端等待客户端连接,客户端连接后,服务端向客户端写入图片
 4  * 3、客户端收到后进行文件保存
 5  * @author Administrator
 6  *
 7  */
 8 public class ServerTcpListener implements Runnable{
 9 
10     public static void main(String[] args) {
11 
12         try {
13             final ServerSocket server = new ServerSocket(33456);
14             
15             
16             Thread th = new Thread(new Runnable() {
17                 public void run() {
18                     while (true) {
19                         try {
20                             System.out.println("开始监听...");
21                             Socket socket = server.accept();
22                             System.out.println("有链接");
23                             send(socket);
24                         } catch (Exception e) {
25                         }
26                     }
27                 }
28             });
29 
30             th.run(); //启动线程运行
31         } catch (Exception e) {
32             e.printStackTrace();
33         }
34     }
35     
36     @Override
37     public void run() {
38         
39     }
40     
41     public static void send(Socket socket) {
42         byte[] inputByte = null;
43         int length = 0;
44         DataOutputStream dos = null;
45         FileInputStream fis = null;
46         try {
47             try {
48                 File file = new File("D:/1.png");
49                 fis = new FileInputStream(file);
50                 dos = new DataOutputStream(socket.getOutputStream());
51                 
52                 inputByte = new byte[1024];
53                 while ((length = fis.read(inputByte, 0, inputByte.length)) > 0) {
54                     dos.write(inputByte, 0, length);
55                     dos.flush();
56                 }
57                
58             } finally {
59                 if (fis != null)
60                     fis.close();
61                 if (dos != null)
62                     dos.close();
63                 if (socket != null)
64                     socket.close();
65             }
66         } catch (Exception e) {
67         }
68     }
69 }
ServerTcpListener.java
 1 public class ClientTcpReceive {
 2 
 3     public static void main(String[] args) {
 4 
 5             int length = 0;
 6             byte[] receiveBytes = null;
 7             Socket socket = null;
 8             DataInputStream dis = null;
 9             FileOutputStream fos = null;
10 
11             try {
12                 try {
13                     socket = new Socket("127.0.0.1", 33456);
14                     
15                     dis = new DataInputStream(socket.getInputStream());
16                     fos = new FileOutputStream(new File("1.png"));
17                    
18                     receiveBytes = new byte[1024];
19                     System.out.println("开始接收数据...");
20                     while ((length = dis.read(receiveBytes, 0, receiveBytes.length)) > 0) {
21                         System.out.println(length);
22                         fos.write(receiveBytes, 0, length);
23                         fos.flush();
24                     }
25                     System.out.println("完成接收");
26                 } finally {
27                     if (dis != null)
28                         dis.close();
29                     if (fos != null)
30                         fos.close();
31                     if (socket != null)
32                         socket.close();
33                 }
34             } catch (Exception e) {
35                 e.printStackTrace();
36             }
37     }
38 
39 }
ClientTcpReceive.java

 

posted on 2016-10-18 09:48  语风6649  阅读(410)  评论(0编辑  收藏  举报

导航