简单的NIO使用实例
public class ThreadTest_2 {
public static void main(String[] args) {
Thread downloaderThread = null;
for (String url : args) {
downloaderThread = new Thread(new FileDownload(url));
downloaderThread.start();
}
} static class FileDownload implements Runnable { private final String fileURL; public FileDownload(String fileURL) {
this.fileURL = fileURL;
} @Override
public void run() {
System.out.println("Downloading from " + fileURL);
String fileBaseName = fileURL.substring(fileURL.lastIndexOf("/") + 1);
try {
URL url = new URL(fileURL);
String localFileNmae = "D:\\viscent-" + fileBaseName;
System.out.println("Saving to " + localFileNmae);
downloadFile(url, new FileOutputStream(localFileNmae), 1024);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done downloading from " + fileURL);
} private void downloadFile(URL url, FileOutputStream fileOutputStream, int bufSize) throws Exception {
final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
int resonseCode = httpURLConnection.getResponseCode();
//读通道
ReadableByteChannel inChannel = null;
//写通道
WritableByteChannel outChannel = null; try {
if (2 != resonseCode / 100) {
throw new IOException("Eroor: HTTP" + resonseCode);
}
if (0 == httpURLConnection.getContentLength()) {
System.out.println("没有内容可下载");
}
inChannel = Channels.newChannel(new BufferedInputStream(httpURLConnection.getInputStream()));
outChannel = Channels.newChannel(new BufferedOutputStream(fileOutputStream));
ByteBuffer byteBuffer = ByteBuffer.allocate(bufSize);
while (-1 != inChannel.read(byteBuffer)) {
//反转状态,由写变读
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
}finally {
inChannel.close();
outChannel.close();
httpURLConnection.disconnect();
}
}
}
}
简单的NIO使用实例的更多相关文章
- 一个简单的Android小实例
原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台 3.安装J ...
- mongodb 简单部署方案及实例
mongodb 简单部署方案及实例 转载:http://my.oschina.net/zhuzhu0129/blog/53290 第一节 准备工作 一 安装mongodb 我这里选用rehl 5.6 ...
- Linux下简单的socket通信实例
Linux下简单的socket通信实例 If you spend too much time thinking about a thing, you’ll never get it done. —Br ...
- 一个简单的jQuery插件开发实例
两年前写的一个简单的jQuery插件开发实例,还是可以看看的: <script type="text/javascript" src="jquery-1.7.2.m ...
- springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目
一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...
- [WCF REST] 一个简单的REST服务实例
Get:http://www.cnblogs.com/artech/archive/2012/02/04/wcf-rest-sample.html [01] 一个简单的REST服务实例 [02] We ...
- PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例
前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...
- Hibernate入门2.简单的项目开发实例
Hibernate入门2.简单的项目开发实例 这一节通过一个简单的项目学习Hibernate项目的配置 代码下载 : 链接: http://pan.baidu.com/s/1zlgjl 密码: p34 ...
- 简单的Slony-I设置实例 II
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页 接前面例子, 简单的Slony-I设置实例 这次我 ...
随机推荐
- IIS中报错弹出调试,系统日志-错误应用程序名称: w3wp.exe,版本: 8.5.9600.16384,时间戳: 0x5215df96(360主机卫士)
偶遇一次特殊情况,在使用Web系统导入数据模版(excel)时,服务端IIS会报错并弹出调试框,然后整个网站都处于卡死的debug状态,如果点否不进行调试,则IIS会中断调试,Web系统继续执行,运行 ...
- 迁移虚拟机打开快照报错:CPUID错误
场景:迁移虚拟机后,恢复快照报错 这个没办法解决,正常启动不会报错,恢复快照有可能报错,是因为你的cpu与快照那个cpu不匹配造成的
- Spring Cloud Commons模块
上一篇介绍了 Spring Cloud Context模块 ,本文介绍SpringCloud的另一个基础模块 SpringCloud Commons模块 .只要在项目的pom文件中引入了spring- ...
- 【medium】990. Satisfiability of Equality Equations 并查集
Given an array equations of strings that represent relationships between variables, each string equa ...
- 帆软报表(finereport)实现自动滚屏效果
例如Demo:IOS平台年度数据报表. 展示内容丰富,一个页面中存在多个图表.内容,超出了浏览器窗口的大小导致内容展示不全. 为了能够预览这个报表的全部内容,可以使用JS滚屏效果来实现. 操作步骤: ...
- Light OJ 1085 - All Possible Increasing Subsequences
题目 link 给定一个序列, 求出上升子序列的总数. 分析 Dp[i] 表示序列 以 i 结尾的数目. 可知 Dp[i]=∑Dp[x]+1 这是一个前缀和, 用树状数组维护. Code #inclu ...
- Android 插件化和热修复知识梳理
概述 在Android开发中,插件化和热修复的话题越来越多的被大家提及,同时随着技术的迭代,各种框架的发展更新,插件化和热修复的框架似乎已经日趋成熟,许多开发者也把这两项技术运用到实际开发协作和正式的 ...
- Python 猜数游戏
import random//引用随机数rand=random.randint(0,99)t=0while 1: //用t来记录使用次数 t+=1 //自行输入一个随机数 aNumber=int(in ...
- python 对Excel表格的读取
import xlrd flbrd = "D:\\考勤系统.xlsx" ws = xlrd.open_workbook(flbrd) # 获取所有sheet名字:ws.sheet_ ...
- 认识 Linux 文件权限
Linux 之所以是比较安全的操作系统,和它丰富的文件权限设定是有关系的,学习 Linux 操作系统一定要懂文件的权限识别和设置.通过这篇博文我们了解 Linux 操作系统的文件权限. 拥有者 / ...