栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

NIO实现文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685184.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号