「高并发通信框架Netty4 源码解读(番外篇)」NIO实现大文件传输
时间:2022-07-24
本文章向大家介绍「高并发通信框架Netty4 源码解读(番外篇)」NIO实现大文件传输,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
服务端
package NIO.VIPIO;
import java.io.FileInputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
public class BigFileTrans {
public static void main(String[] args) throws Exception {
ServerSocketChannel sever = ServerSocketChannel.open();
sever.configureBlocking(false);
sever.bind(new InetSocketAddress("localhost",8999));
Selector selector = Selector.open();
sever.register(selector, SelectionKey.OP_ACCEPT);
while (true){
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey next = iterator.next();
iterator.remove();
if(next.isAcceptable()){
final SocketChannel socketChannel = sever.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector,SelectionKey.OP_WRITE);
}
if (next.isWritable()){
SocketChannel socketChannel = (SocketChannel) next.channel();
FileInputStream file = new FileInputStream("e:\Kylin-3.3-x86_64.rar");
FileChannel channel = file.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(400000000);
while(channel.position()<channel.size()){
channel.read(byteBuffer);
byteBuffer.flip();
while (byteBuffer.hasRemaining()){
socketChannel.write(byteBuffer);
}
byteBuffer.clear();
}
socketChannel.close();
}
}
}
}
}
客户端
package NIO.VIPIO;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
public class BigFileTransClient {
public static void main(String[] args) throws IOException {
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(new InetSocketAddress("localhost",8999));
Selector selector = Selector.open();
channel.register(selector, SelectionKey.OP_CONNECT);
while(true){
if (channel.isOpen()){
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
final Iterator<SelectionKey> iterator = selectionKeys.iterator();
while (iterator.hasNext()){
final SelectionKey next = iterator.next();
iterator.remove();
if (next.isConnectable()){
while (!channel.finishConnect()){}
channel.register(selector,SelectionKey.OP_READ);
}
if (next.isReadable()){
Long startTime = System.currentTimeMillis();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(400000000);
RandomAccessFile file = new RandomAccessFile("e:\cong.rar", "rw");
FileChannel fileChannel = file.getChannel();
while (channel.read(byteBuffer)!=-1){
byteBuffer.flip();
fileChannel.write(byteBuffer);
byteBuffer.clear();
}
Long endTime = System.currentTimeMillis();
Long tempTime = (endTime - startTime);
System.out.println("花费时间:"+
(((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
((tempTime%1000)+"ms"));
fileChannel.close();
file.close();
channel.close();
}else {
break;
}
}
}
}
}
}
- mvn编译的时候一个破错误,google只有3个结果maven : Failed to install metadata project Could not parse metadata maven-
- 读书|《Mastering Machine Learning with Python in Six Steps》
- 几种简单的文本数据预处理方法
- Fiddler中显示IP方法
- readlink: command not found 解决方案
- Java 并发编程系列: CountDownLatch (上厕所的案例)
- 详解ANGULAR2组件中的变化检测机制(对比ANGULAR1的脏检测)
- 如何快速查看github代码库中第一次commit的记录
- 更换包管理工具npm为yarn
- 【精选】使用Cryptory分析影响加密货币价格的因素(区块链系列3)
- React -- 组件间通信
- 图片和视频防盗链简单介绍
- 对比cp和scp命令 将数据从一台linux服务器复制到另一台linux服务器
- laravel—用Migration的操作数据库
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- dotnet OpenXML 从文档生成创建文档的代码的库
- 【LeetCode每日一题】24. Swap Nodes in Pairs
- 【LeetCode每日一题】26. Remove Duplicates from Sorted Array
- K8S 生态周报| NGINX Ingress Controller又添新特性
- 网络安全实验室平台(脚本关)
- shell 多线程及线程数控制实现
- CTF考点总结-sql注入篇
- dotnet OpenXML 为什么资源使用 Relationship 引用
- k8s部署Kafka集群
- 彻底搞懂 etcd 系列文章(六):etcd 核心 API v3
- Open3d学习计划—7(RGBD测程法)
- 一次信息泄露引发的越权
- MySQL 调优 | OPTIMIZER_TRACE 详解
- 这次被坑惨了,MySQL的隐式转换导致了一个线上BUG
- iOS开发之UIMenu