「高并發(fā)通信框架Netty4 源碼解讀(番外篇)」NIO實(shí)現(xiàn)大文件傳輸

服務(wù)端

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("花費(fèi)時(shí)間:"+
                                (((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;
                    }
                }

            }
        }


    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容