网络文件传输 - NIO 和传统 IO

1. 传统 IO 方式

1.1 传统 IO 服务器端

public class OidIOServer {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(7001);
        while (true) {
            Socket socket = serverSocket.accept();
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());

            try {
                byte[] byteArray = new byte[4096];
                while (true) {
                    int read = inputStream.read(byteArray, 0, byteArray.length);
                    if (read == -1) {
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

1.2 传统 IO 客户端

public class OidIOClient {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("127.0.0.1", 7001);
        String fileName = "book.zip"; // 放在项目的根目录
        InputStream inputStream = new FileInputStream(fileName);
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());

        byte[] buffer = new byte[4096];
        long readCount;
        long total = 0;
        long startTime = System.currentTimeMillis();

        while ((readCount = inputStream.read(buffer)) >= 0) {
            total += readCount;
            outputStream.write(buffer);
        }

        System.out.println("发送字节数: " + total + ", 耗时: " + (System.currentTimeMillis()-startTime));
        outputStream.close();
        socket.close();
        inputStream.close();
    }
}

在这里插入图片描述

2. NIO 方式

2.1 NIO 服务器端

public class NewIOServer {
    public static void main(String[] args) throws Exception {
        InetSocketAddress socketAddress = new InetSocketAddress(7001);
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = socketChannel.socket();
        serverSocket.bind(socketAddress);

        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        while (true) {
            SocketChannel channel = socketChannel.accept();
            int readCount = 0;
            while (-1 != readCount) {
                try {
                    readCount = channel.read(byteBuffer);
                } catch (Exception e) {
                    //e.printStackTrace();
                }

                byteBuffer.rewind(); // 倒带,将position=0,将mark作废
            }
        }
    }
}

2.2 NIO 客户端

public class NewIOClient {
    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 7001));

        String fileName = "book.zip";
        FileChannel channel = new FileInputStream(fileName).getChannel();

        long start = System.currentTimeMillis();

        /**
         * 在linux下,一个transferTo方法就可以完成传输
         * 在windows下,调用一次transferTo只能发送8M,则需要分段传输文件,而且要注意传输的位置
         */
        long transferCount = 0;
        long ls = 8 * 1024 * 1024;
        long size = channel.size();
        long count = size / ls;
        long flag = size % ls;
        if (flag != 0) {
            count++;
        }

        long startPosition = 0;
        for (int i = 0; i < count; i++) {
            long transfer = channel.transferTo(startPosition, ls, socketChannel);
            transferCount += transfer;
            startPosition += ls;
        }
        System.out.println("发送的总的字节数 = " + transferCount + ", 耗时 = " + (System.currentTimeMillis()-start));
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LF3_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值