博客
关于我
java 零拷贝 分块传输大文件(解除8M限制)
阅读量:256 次
发布时间: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/

你可能感兴趣的文章
面试时被问技术栈底层 , 机智小伙反秀面试官一脸
查看>>
学而时习之网络篇: 又是HTTP缓存的锅 !
查看>>
初学源码如何越学越香 ?
查看>>
XCTF web Web_php_include (php://过滤)
查看>>
记录一次需求变动导致的重构
查看>>
python-requests模块实现ip代理池
查看>>
使用async、await改善异步代码
查看>>
BugkuCTF web_1-10
查看>>
POJ 2387 Til the Cows Come Home(Dijkstra优先队列)
查看>>
零基础入门JavaScript 这一篇笔记就够了
查看>>
MySQL_属性、记录长度、设计范式、表关系
查看>>
MySQL_安全管理、表单传值、php操作
查看>>
POJ 3468 A Simple Problem with Integers(线段树+区间更新)
查看>>
BUUCTF web WarmUp
查看>>
hcnp笔记
查看>>
python数据分析
查看>>
MySQL(四)数据库结构设计
查看>>
mybatis 如何切割字符串 查询多个值
查看>>
9.5 正则表达式及字符串的替换与分解
查看>>
在所有情况下取到顶层对象
查看>>