博客
关于我
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中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
MySql中的concat()相关函数
查看>>
mysql中的concat函数,concat_ws函数,concat_group函数之间的区别
查看>>
MySQL中的count函数
查看>>
MySQL中的DB、DBMS、SQL
查看>>
MySQL中的DECIMAL类型:MYSQL_TYPE_DECIMAL与MYSQL_TYPE_NEWDECIMAL详解
查看>>
MySQL中的GROUP_CONCAT()函数详解与实战应用
查看>>
MySQL中的IO问题分析与优化
查看>>
MySQL中的ON DUPLICATE KEY UPDATE详解与应用
查看>>
mysql中的rbs,SharePoint RBS:即使启用了RBS,内容数据库也在不断增长
查看>>
mysql中的undo log、redo log 、binlog大致概要
查看>>
Mysql中的using
查看>>
MySQL中的关键字深入比较:UNION vs UNION ALL
查看>>
MYSQL中频繁的乱码问题终极解决
查看>>
Mysql主从不同步
查看>>