Java Socket图片文件传输

来源:互联网 发布:中国人口数据 编辑:程序博客网 时间:2024/05/16 15:07
java socket通信-传输文件图片

ClientTcpSend.java 客户端发送类

Java Socket图片文件传输
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.yjf.test;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class ClientTcpSend {
   
    publicstatic String clientip ="127.0.0.1";
    publicstatic int port = 33456;
    publicstatic void main(String[] args) {
        int length = 0;
        byte[] sendBytes = null;
        Socket socket = null;
        DataOutputStream dos = null;
        FileInputStream fis = null;
        try {
            try {
               socket = newSocket();
               socket.connect(newInetSocketAddress(clientip, port),30* 1000);
               dos = newDataOutputStream(socket.getOutputStream());
               File file = newFile("F:\\aa.xml");
               fis = newFileInputStream(file);
               sendBytes = newbyte[1024*4];
               while ((length = fis.read(sendBytes,0, sendBytes.length)) >0) {
                   dos.write(sendBytes, 0, length);
                   dos.flush();
               }
            } finally {
               if (dos != null)
                   dos.close();
               if (fis != null)
                   fis.close();
               if (socket != null)
                   socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ServerTcpListener.java 服务器监听类

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