「高并发通信框架Netty4 源码解读(番外篇)」NIO实现大文件传输

时间:2022-07-24
本文章向大家介绍「高并发通信框架Netty4 源码解读(番外篇)」NIO实现大文件传输,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

服务端

package NIO.VIPIO;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;

public class BigFileTrans {

    public static void main(String[] args) throws Exception {

        ServerSocketChannel sever = ServerSocketChannel.open();
        sever.configureBlocking(false);
        sever.bind(new InetSocketAddress("localhost",8999));
        Selector selector = Selector.open();
        sever.register(selector, SelectionKey.OP_ACCEPT);
        while (true){
            selector.select();
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while(iterator.hasNext()){
                SelectionKey next = iterator.next();
                iterator.remove();
                if(next.isAcceptable()){
                    final SocketChannel socketChannel = sever.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector,SelectionKey.OP_WRITE);

                }
                if (next.isWritable()){
                    SocketChannel socketChannel = (SocketChannel) next.channel();
                    FileInputStream file = new FileInputStream("e:\Kylin-3.3-x86_64.rar");
                    FileChannel channel = file.getChannel();
                    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(400000000);
                    while(channel.position()<channel.size()){
                        channel.read(byteBuffer);
                        byteBuffer.flip();
                        while (byteBuffer.hasRemaining()){
                            socketChannel.write(byteBuffer);
                        }
                        byteBuffer.clear();
                    }
                    socketChannel.close();
                }

            }
        }


    }
}

客户端

package NIO.VIPIO;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class BigFileTransClient {

    public static void main(String[] args) throws IOException {

        SocketChannel channel = SocketChannel.open();
        channel.configureBlocking(false);
        channel.connect(new InetSocketAddress("localhost",8999));
        Selector selector = Selector.open();
        channel.register(selector, SelectionKey.OP_CONNECT);
        while(true){
            if (channel.isOpen()){
                selector.select();
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                final Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()){
                    final SelectionKey next = iterator.next();
                    iterator.remove();
                    if (next.isConnectable()){
                        while (!channel.finishConnect()){}
                        channel.register(selector,SelectionKey.OP_READ);
                    }
                    if (next.isReadable()){
                        Long startTime = System.currentTimeMillis();
                        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(400000000);
                        RandomAccessFile file = new RandomAccessFile("e:\cong.rar", "rw");
                        FileChannel fileChannel = file.getChannel();
                        while (channel.read(byteBuffer)!=-1){
                            byteBuffer.flip();
                            fileChannel.write(byteBuffer);
                            byteBuffer.clear();
                        }
                        Long endTime = System.currentTimeMillis();
                        Long tempTime = (endTime - startTime);
                        System.out.println("花费时间:"+
                                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                                ((tempTime%1000)+"ms"));
                        fileChannel.close();
                        file.close();
                        channel.close();

                    }else {
                        break;
                    }
                }

            }
        }


    }
}