Java 简单TCP文件传输

服务端

package TCP;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCP_File_Server {
    public static void main(String[] args) throws Exception {
        /**
         * 创建服务端套接字
         */
        ServerSocket ss = new ServerSocket();
        
        /**
         * 绑定指定端口
         */
        ss.bind(new InetSocketAddress(12345));
        System.out.println("《《《《网盘开始运行》》》》");
        /**
         * 监听并接受客户端socket连接,并返回一个socket
         */
        
        /**
         * 持续接收客户端发来的信息,并交给线程处理
         */
        while(true) {
            Socket socket = ss.accept();
            new Thread(new UpLoad(socket)).start();
        }
    }
}

class UpLoad implements Runnable{
    
    private Socket socket = null;
    
    public UpLoad(Socket socket) {
        this.socket = socket;
    }
    
    @Override
    public void run() {

        OutputStream out = null;
        try {
            // 创建文件输入流,接收客户端的socket中的文件流
            InputStream in = socket.getInputStream();
            /**
             * 获取文件名长度
             * 文件格式:文件名长度(数字)\r\文件名\r\n文件内容\r\n
             * 获取文件名 - 读到第一个回车换行之前 截取出文件名的长度 接着读取这个长度的字节 就是文件名
             * 读取数据 直到遇到第一个回车换行
             * 每次从流中读取一个字节 转成字符串 拼到line上 只要line还不是\r\n结尾 就重复这个过程
             */
            String line1 = "";
            byte[] by1 = new byte[1];
            while(!line1.endsWith("\r\n")) {
                in.read(by1);
                String str = new String(by1);
                line1 += str;
            }
            /**
             * 1.读到长度,去掉\r\n就是文件名字的长度
             * 2.parseInt():作用是将可分析的字符串转化为整数。
             * 3.substring():返回一个新字符串,它是此字符串的一个子字符串。
             */
            int len1 = Integer.parseInt(line1.substring(0, line1.length() - 2));
            /**
             * 1.读取文件名
             * 2.先创建一个长度和文件名长度相等的字节数组,用来存放文件名
             * 3.read(data):从输入流中读取一定数量的字节,并将其存储在缓冲区数组 data 中
             *      data数组有多大,就在in输入流里面读取多少内容,并将内容存放在data数组里面
             */
            byte[] data = new byte[len1];
            in.read(data);
            String fileName = new String(data);
            
            // 获取文件内容字节长度
            String line2 = "";
            byte[] by2 = new byte[1];
            while(!line2.endsWith("\r\n")) {
                in.read(by2);
                String str = new String(by2);
                line2 += str;
            }
            int len2 = Integer.parseInt(line2.substring(0, line2.length() - 2));
            
            // 创建输文件出流,指定文件输出地址
            String path = "E:/" + fileName;
            out = new FileOutputStream(path);
            // 获取文件内容字节
            // 流对接
            byte[] by3 = new byte[len2];
            in.read(by3);
            out.write(by3);
            
            System.out.println("接受到来自"+socket.getInetAddress().getHostAddress()+"上传的文件"+path);
            
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            // 关闭资源
            // 关闭输出流
            try {
                if(out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                out = null;
            }
            // 关闭socket
            try {
                if(socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                socket = null;
            }
        }
    }
}

客户端

package TCP;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Scanner;

public class TCP_File_Client {
    public static void main(String[] args) {
        Scanner scan = null;
        InputStream in = null;
        Socket socket = null;
        
        try {
            /**
             * 1.扫描控制台接收文件路径名
             *   创建一个file引用,指向一个新的File对象,并给文件赋予地址
             */
            System.out.println("请输入要传输文件的路径:");
            scan = new Scanner(System.in);
            String path = scan.nextLine();
            File file = new File(path);
            
            /**
             * 2.判断文件是文本文件而不是文件夹并且路径存在
             *  exists():判断文件是否存在
             *  isFile():判断是不是文件 
             */
            if(file.exists() && file.isFile()) {
                
                /**
                 * 3.创建文件输入流,发送文件 
                 *   将文件输入的内容都放在file里面
                 */
                in = new FileInputStream(file);
                
                /**
                 * Socket 这个类实现客户端套接字(也称为“套接字”)。套接字是两台机器间通信的端点。
                 *
                 * 4.创建客户端套接字
                 */
                socket = new Socket();
                //InetSocketAddress Inets = new InetSocketAddress("127.0.0.1", 12345);
                
                /**
                 * 5.连接TCP服务器
                 *       确定服务端的IP和端口号
                 */
                socket.connect(new InetSocketAddress("127.0.0.1", 12345));
                
                /**
                 * 6.获取到客户端的输出流
                 *   OutputStream     getOutputStream()
                 *                         返回此套接字的输出流。 
                 */
                OutputStream out = socket.getOutputStream();
                
                /**
                 * 7.向服务器发送文件
                 *   自己定义了一个协议来解决粘包现象,获取文件名
                 *   7.1.我们先将文件中的内容读取出来,放到file里面
                 *   7.2.先读文件名  file.getName()
                 *   7.3.将文件名转换成字节  file.getName().getBytes()
                 *   7.4.获取文件名的字节的长度  file.getName().getBytes().length
                 *   7.5.再在文件名长度的后面加上  \r\n 作为标识符
                 */
                // 向服务器发送[文件名字节长度 \r\n]
                out.write((file.getName().getBytes().length + "\r\n").getBytes());
                // 向服务器发送[文件名字节]
                out.write(file.getName().getBytes());
                // 向服务器发送[文件字节长度\r\n]
                out.write((file.length() + "\r\n").getBytes());
                // 向服务器发送[文件字节内容]
                byte[] data = new byte[1024];
                int i = 0;
                while((i = in.read(data)) != -1) {
                    out.write(data, 0, i);
                }
                
            }else {
                System.out.println("文件不存在或者一个文件~~");
            }
        } catch (Exception e) {
            
            e.printStackTrace();
        }finally {
            /**
             * 关闭Scanner,文件输入流,套接字
             * 套接字装饰了输出流,所以不用关闭输出流
             */
            if(scan != null) {
                scan.close();
            }
            try {
                if(in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                // 强制将输入流置为空
                in = null;
            }
            try {
                if(socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                // 强制释放socket
                socket = null;
            }
            
        }
        System.out.println("文件传输完毕");
    }
}

猜你喜欢

转载自www.cnblogs.com/chuijingjing/p/9550423.html