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

2020-09-04 10:21:28 浏览数 (1)

服务端

代码语言:javascript复制
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();
                }

            }
        }


    }
}

客户端

代码语言:javascript复制
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�400000/3600000)>0))?((tempTime�400000/3600000) "h"):("")) 
                                ((((tempTime/3600000)>0)||((tempTime600000/60000)>0))?((tempTime600000/60000) "m"):("")) 
                                ((((tempTime/60000)>0)||((tempTime`000/1000)>0))?((tempTime`000/1000) "s"):("")) 
                                ((tempTime00) "ms"));
                        fileChannel.close();
                        file.close();
                        channel.close();

                    }else {
                        break;
                    }
                }

            }
        }


    }
}

0 人点赞