「高并发通信框架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;
}
}
}
}
}
}
- 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 数组属性和方法
- Real-time Xenomai 3 example 1
- Electron 常见问题收录II
- SQL性能调优技巧
- Linux查看CUDA版本以及cudnn版本号
- 数据结构算法操作试题(C++/Python)——最长有效括号
- 数据结构算法操作试题(C++/Python)——两两交换链表中的节点
- 数据结构算法操作试题(C++/Python)——最大子序和
- 数据结构算法操作试题(C++/Python)——四数之和
- 数据结构算法操作试题(C++/Python)——在排序数组中查找元素的第一个和最后一个位置
- 数据结构算法操作试题(C++/Python)——搜索旋转排序数组
- 数据结构算法操作试题(C++/Python)——最后一个单词的长度
- IDEA 自动生成类注释和方法注释
- 包管理工具yarn的安装和使用详细介绍
- redis学习(九)
- 模型性能提升操作