博客
关于我
java 零拷贝 分块传输大文件(解除8M限制)
阅读量:264 次
发布时间:2019-03-01

本文共 2433 字,大约阅读时间需要 8 分钟。

Server.java

package com.atguigu.nio.zerocopy;import java.io.IOException;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 {        InetSocketAddress address = new InetSocketAddress(7002);        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();        ServerSocket serverSocket = serverSocketChannel.socket();        serverSocket.bind(address);        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);        while (true) {            SocketChannel socketChannel = serverSocketChannel.accept();            int readCount = 0;            while (-1 != readCount) {                try {                    readCount = socketChannel.read(byteBuffer);                } catch (Exception e) {                    break;                }                byteBuffer.rewind();            }        }    }}

Client.java

package com.atguigu.nio.zerocopy;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", 7002));        String filename = "./test.avi";        FileChannel fileChannel = new FileInputStream(filename).getChannel();        long startTime = System.currentTimeMillis();        // 总共字节数        long totalSize = fileChannel.size();        // windows平台最大传送数据包        long max_pkg = 8 * 1024 * 1024;        // 本次传输到的位置        long cur_pos = 0;        long transSize = 0;        // 如果剩余的字节数量 > 8M        while (totalSize > max_pkg) {            // 本次传输的字节数            long tmp = fileChannel.transferTo(cur_pos, max_pkg, socketChannel);            // 传输后剩余字节数            totalSize -= tmp;            // 传输到的位置            cur_pos += tmp;            // 已经传输的大小 用于校验            transSize += tmp;        }        // 如果还有剩余,则将剩余一次性传输        if (totalSize > 0) {            long tmp = fileChannel.transferTo(cur_pos, totalSize, socketChannel);            transSize += tmp;        }        System.out.println("发送的总的字节数=" + transSize + " 耗时=" + (System.currentTimeMillis() - startTime));        fileChannel.close();    }}

 

转载地址:http://cqaa.baihongyu.com/

你可能感兴趣的文章
Mysql报错Packet for query is too large问题解决
查看>>
mysql报错级别_更改MySQL日志错误级别记录非法登陆(Access denied)
查看>>
Mysql报错:too many connections
查看>>
MySQL报错:无法启动MySQL服务
查看>>
mysql授权用户,创建用户名密码,授权单个数据库,授权多个数据库
查看>>
mysql排序查询
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
MySQL排序的艺术:你真的懂 Order By吗?
查看>>
Mysql推荐书籍
查看>>
Mysql插入数据从指定选项中随机选择、插入时间从指定范围随机生成、Navicat使用存储过程模拟插入测试数据
查看>>
MYSQL搜索引擎
查看>>
mysql操作数据表的命令_MySQL数据表操作命令
查看>>
mysql操作日志记录查询_如何使用SpringBoot AOP 记录操作日志、异常日志?
查看>>
MySQL支持的事务隔离级别,以及悲观锁和乐观锁的原理和应用场景?
查看>>
mysql支持表情
查看>>
MySQL支撑百万级流量高并发的网站部署详解
查看>>
MySQL改动rootpassword的多种方法
查看>>
mysql数据分组索引_MYSQL之索引配置方法分类
查看>>
mysql数据取差,mysql屏蔽主外键关联关系
查看>>
MySQL数据和Redis缓存一致性方案详解
查看>>