首页 技术 正文
技术 2022年11月14日
0 收藏 963 点赞 3,017 浏览 2787 个字
public class NIOClient {
static int SIZE = 2;
final static int bufferSize = 500 * 1024;
static InetSocketAddress ip = new InetSocketAddress("localhost", 12345);
static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder(); static class Download implements Runnable {
protected int index;
String outfile = null; public Download(int index) {
this.index = index;
this.outfile = "c:\\" + index + ".rmvb";
} public void run() {
FileOutputStream fout = null;
// FileChannel fcout = null;
try {
fout = new FileOutputStream(outfile);
// fcout = fout.getChannel();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} try {
long start = System.currentTimeMillis(); // 打开客户端socket管道
SocketChannel client = SocketChannel.open(); // 客户端的管道的通讯模式
client.configureBlocking(false); // 选择器
Selector selector = Selector.open(); // 往客户端管道上注册感兴趣的连接事件
client.register(selector, SelectionKey.OP_CONNECT); // 配置IP
client.connect(ip); // 配置缓存大小
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
int total = 0;
FOR: for (;;) { // 阻塞,返回发生感兴趣事件的数量
selector.select(); // 相当于获得感兴趣事件的集合迭代
Iterator<SelectionKey> iter = selector.selectedKeys()
.iterator(); while (iter.hasNext()) { SelectionKey key = iter.next(); System.out.println("-----Thread " + index
+ "------------------" + key.readyOps()); // 删除这个马上就要被处理的事件
iter.remove(); // 感兴趣的是可连接的事件
if (key.isConnectable()) { // 获得该事件中的管道对象
SocketChannel channel = (SocketChannel) key.channel(); // 如果该管道对象已经连接好了
if (channel.isConnectionPending())
channel.finishConnect();
// channel.write(encoder.encode(CharBuffer
// .wrap("d://film//" + index + ".mp3"))); // 往管道中写一些块信息
channel.write(encoder.encode(CharBuffer
.wrap("d://film//1-hadoop-1.1.2.tar.gz"))); // 之后为该客户端管道注册新的感兴趣的事件---读操作
channel.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) { // 由事件获得通讯管道
SocketChannel channel = (SocketChannel) key
.channel(); // 从管道中读取数据放到缓存中
int count = channel.read(buffer);
System.out.println("count:" + count);
if (count > 0) { // 统计读取的字节数目
total += count; // 这样一来从posistion~limit这段缓存数据是有效,可利用的
// buffer.flip(); buffer.clear(); // 往输出文件中去写了
if (count < bufferSize) { byte[] overByte = new byte[count]; for (int index = 0; index < count; index++) {
overByte[index] = buffer.get(index);
} fout.write(overByte);
// System.out.println(":::"
// + new String(buffer.array()));
} else {
fout.write(buffer.array());
// System.out.println(":::"
// + new String(buffer.array()));
}
} else {
// 关闭客户端通道
client.close();
// 退出大循环
break FOR;
}
// ?
// buffer.clear();
}
}
} // 计算时间
double last = (System.currentTimeMillis() - start) * 1.0 / 1000;
System.out.println("Thread " + index + " downloaded " + total
/ 1024 + "kbytes in " + last + "s.");
} catch (IOException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) throws IOException { long startTime = System.currentTimeMillis(); // 启用线程池
ExecutorService exec = Executors.newFixedThreadPool(SIZE);
for (int index = 1; index <= SIZE; index++) {
exec.execute(new Download(index));
}
exec.shutdown(); long endTime = System.currentTimeMillis(); long timeLong = endTime - startTime; System.out.println("下载时间:" + timeLong); }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298