Java NIO 服务器与客户端实现文件下载

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhangfengzhe.blog.51cto.com/8855103/1726488



写在前面

对于Java NIO已经学习了一段时间了,周末实践了下,折腾了一天,总算对NIO的理论,有了一个感性的认识。下面的实践是:服务器与客户端都采用NIO的方式来实现文件下载。对于传统的SOCKET BIO方式,服务器端会为每个连接上的客户端分配一个Worker线程来进行doWork,而NIO SERVER却没有为每个Socket链接分配线程的必要了,避免了大量的线程所需的上下文切换,借助NIO提供的Selector机制,只需要一个或者几个线程来管理成百上千的SOCKET连接。那么下面我们就来看看吧!


文件下载辅助类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 这个类的基本思路是,读取本地文件到缓冲区
 * 因为通道只能操作缓冲区
 */
public class DownloadFileProcesser implements Closeable {
    private ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
    private FileChannel fileChannel ;
    public DownloadFileProcesser() {
        try{
            FileInputStream fis = new FileInputStream("/Users/cross/PractiseProject/weixinTouxiang.py");
            fileChannel = fis.getChannel();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public int readFile2Buffer() throws IOException{
        int count = 0;
        buffer.clear();
        count = fileChannel.read(buffer);
        buffer.flip();
        return count;
    }
    public ByteBuffer getByteBuffer(){
        return buffer;
    }
    @Override
    public void close() throws IOException {
        fileChannel.close();
    }

}


服务端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

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

public class ServerMain {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.socket().bind(new InetSocketAddress(8887));
        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {
            selector.select();
            Iterator iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey s = iterator.next();
                // 如果客户端有连接请求
                if (s.isAcceptable()) {
                    System.out.println("客户端连接请求..");
                    ServerSocketChannel ssc = (ServerSocketChannel) s.channel();
                    SocketChannel sc = ssc.accept();
                    sc.configureBlocking(false);
                    sc.register(selector, SelectionKey.OP_READ);
                }

                // 如果客户端有发送数据请求
                if (s.isReadable()) {
                    System.out.println("接受客户端发送过来的文本消息...");
                    //这里拿出的通道就是ACCEPT上注册的SocketChannel通道
                    SocketChannel sc = (SocketChannel) s.channel();
                    //要读取数据先要准备好BUFFER缓冲区
                    ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);
                    //准备BYTE数组,形成输出
                    sc.read(buffer);
                    byte[] clientByteInfo = new byte[buffer.position()];
                    buffer.flip();
                    buffer.get(clientByteInfo);
                    System.out.println("服务器端收到消息:" + new String(clientByteInfo,"utf-8"));
                    //CLIENT下一步的动作就是读取服务器端的文件,因此需要注册写事件
                    SelectionKey selectionKey = sc.register(selector, SelectionKey.OP_WRITE);
                    //在这个selectionKey上绑定一个对象,以供写操作时取出进行处理
                    DownloadFileProcesser downloadFileProcesser = new DownloadFileProcesser();
                    selectionKey.attach(downloadFileProcesser);
                }
                // 如果客户端有下载文件数据请求
                if (s.isWritable()) {
                    System.out.println("IM HERE!");
                    //这里把attachment取出进行写入操作
                    DownloadFileProcesser downloadFileProcesser = (DownloadFileProcesser)s.attachment();
                    int count = downloadFileProcesser.readFile2Buffer();

                    if(count <= 0){
                        System.out.println("客户端下载完毕...");
                        //关闭通道
                        s.channel().close();
                        downloadFileProcesser.close();
                    }else{
                        //需要注意的是我们这里并没有出现常见的while写的结构,这是为何?
                        //因为client其实不断的在read操作,从而触发了SELECTOR的不断写事件!
                        SocketChannel sc = (SocketChannel)s.channel();
                        sc.write(downloadFileProcesser.getByteBuffer());
                    }
                }
                iterator.remove();
            }
        }
    }
}


客户端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

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;

/**
 * Created by cross on 2017/7/13.
 */
public class Client4DownloadFile implements Runnable {

    //标示
    private String name;
    private FileChannel fileChannel;
    public Client4DownloadFile(String name , RandomAccessFile randomAccessFile){
        this.name = name;
        this.fileChannel = randomAccessFile.getChannel();
    }

    private ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);

    @Override
    public void run() {
        try {
            SocketChannel sc = SocketChannel.open();
            Selector selector = Selector.open();
            sc.configureBlocking(false);
            sc.register(selector, SelectionKey.OP_CONNECT);
            sc.connect(new InetSocketAddress("127.0.0.1",8887));

            while(true){
                selector.select();
                Iterator iterator = selector.selectedKeys().iterator();

                while(iterator.hasNext()){
                    SelectionKey s = iterator.next();
                    if(s.isConnectable()){
                        System.out.println("客户端[" + name + "]已经连接上了服务器...");
                        SocketChannel sc2 = (SocketChannel)s.channel();
                        if(sc2.isConnectionPending() && sc2.finishConnect()){
                            sc2.configureBlocking(false);
                            String msg = "Thread-" + name + " send message!";
                            byte[] b = msg.getBytes("utf-8");
                            sc2.write(ByteBuffer.wrap(b));
                            System.out.println("客户端[" + name + "]给服务器端发送文本消息完毕...");
                            sc2.register(selector, SelectionKey.OP_READ);
                        }
                    }

                    if(s.isReadable()){
                        SocketChannel sc3 = (SocketChannel)s.channel();
                        buffer.clear();
                        int count = sc3.read(buffer);
                        if(count <= 0){
                            s.cancel();
                            System.out.println("Thread " + name + " 下载完毕...");
                        }
                        while(count > 0){
                            buffer.flip();
                            fileChannel.write(buffer);
                            count = sc3.read(buffer);
                        }
                    }
                    iterator.remove();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



1
2
3
4
5
6
7
8
9
10

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;

/**
 * Created by cross on 2017/7/13.
 */
public class ClientMain {
    public static void main(String[] args) throws FileNotFoundException {
        File file = new File("/Users/cross/PractiseProject/2.py");
        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        Client4DownloadFile client4DownloadFile = new Client4DownloadFile("1" , raf);
        new Thread(client4DownloadFile).start();

    }
}







你可能感兴趣的:(Java NIO 服务器与客户端实现文件下载)