NIO实现文件上传

NIO上传

NIO Client

package com.example.leetcode.file;

import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class NewIoClient {
    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost", 8899));
        socketChannel.configureBlocking(true);
        String fileName = "D:/Download/spark-2.4.0-bin-hadoop2.7.tgz";
        FileChannel channel = new FileInputStream(fileName).getChannel();
        long size = channel.size();
        long position = 0;
        long totalCount = 0;
        long startTime = System.currentTimeMillis();
        while (size > 0) {
            long count = channel.transferTo(position, channel.size(), socketChannel);
            position += count;
            size -= count;
            totalCount += count;
        }

        System.out.println("发送字节数:" + totalCount + ", 耗时:" + (System.currentTimeMillis() - startTime));
        channel.close();
        socketChannel.close();
    }
}

NIO Server

package com.example.leetcode.file;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NewIoServer {

    public static void main(String[] args) throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket socket = serverSocketChannel.socket();
        // 当Socket连接关闭时,并不会立刻释放端口,而是处于超时状态并继续持有端口一段时间
        // 如果端口号处于TIME_WAIT状态时,通过设置该参数为True,新的socket是可以绑定该端口号的
        socket.setReuseAddress(true);
        socket.bind(new InetSocketAddress(8899));
        ByteBuffer buffer = ByteBuffer.allocate(4096);
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            socketChannel.configureBlocking(true);
            int read = 0;
            long count = 0;
            while (read != -1) {
                read = socketChannel.read(buffer);
                buffer.rewind();
                count += read;
            }
            System.out.println("发送字节数:" + count);
        }
    }

}

上传文件(OLD)

Client

package com.example.leetcode.file;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.net.Socket;

public class OldIoClient {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 8899);
        String fileName = "D:/Download/spark-2.4.0-bin-hadoop2.7.tgz";
        FileInputStream fileInputStream = new FileInputStream(fileName);
        DataInputStream dataInputStream = new DataInputStream(fileInputStream);
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        byte[] buffer = new byte[4096];
        long startTime = System.currentTimeMillis();
        int read = 0;
        long count = 0;
        while (read >= 0) {
            read = dataInputStream.read(buffer);
            count += read;
            dataOutputStream.write(buffer);
        }
        System.out.println("发送字节数:" + count + ", 耗时:" + (System.currentTimeMillis() - startTime));
        dataOutputStream.close();
        dataInputStream.close();
        socket.close();
    }
}

Server

package com.example.leetcode.file;

import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class OldIoServer {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(8899);
        while (true) {
            Socket socket = serverSocket.accept();
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            byte[] buffer = new byte[4096];
            try {
                while (true) {
                    int read = dataInputStream.read(buffer);
                    if (read == -1) {
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值