简单的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设置实例 这次我 ...
随机推荐
- mac air中编译安装swoole
本机php版本, 我的7.3.0 1 下载swoole源码 https://github.com/swoole/swoole-src/releases 我下载的版本是swoole-src-4.3.3. ...
- gunicorn+anaconda+nginx部署django项目(ubuntu)
首先进入conda 虚拟环境: source activate test 安装gunicorn: pip install gunicorn 运行gunicorn gunicorn -w 2 -b 12 ...
- jquery的datatables第二次加载报错
"destroy":true, "scrollX": true, "ordering": false, "sScrollXInne ...
- python算法&二分查找法
import random def random_list(n): result = [] ids = list(range(1001,1001+n)) a1 = ["赵",&qu ...
- 为什么使用SLF4J?
每个Java开发人员都知道日志记录对Java应用的重要性,尤其是对服务端应用,而且其中许多人都已经熟悉了各种记录日志的库,比如java.util.logging,Apache的log4j,logb ...
- nginx允许IP访问不生效问题【原创】
使用nginx的nginx_upstream_check模块来检测后端服务器的转态时,设置只允许某段IP访问,发现不生效,不在此网段的IP也可以访问. 原因为在允许IP访问最后一定要加deny all ...
- story 泄露服务器libc版本
记录下学到的姿势,利用信息泄露得到服务器libc 至少两个函数偏移,利用libc-databse得到服务器libc版本 传送门 泄露脚本如下 from pwn import * context.log ...
- C++入门篇十一
单例对象:为了让类只有一个实例,实例不需要自己释放掉 不管创建多少个实例对象进行访问,访问的都是同一个值 #include "pch.h" #include <iostrea ...
- E - Coin Change UVA - 674 &&(一些记录路径的方法)
这一道题并不难,我们只需要将dp数组先清空,再给dp[0]=1,之后就按照完全背包的模板写 主要是我们要证明着一种方法不会出现把(1+3+4)(1+4+3)当作两种方法,这一点如果自己写过背包的那个表 ...
- MySQL-mysql 8.0.11安装教程
网上的教程有很多,基本上大同小异.但是安装软件有时就可能因为一个细节安装失败.我也是综合了很多个教程才安装好的,所以本教程可能也不是普遍适合的. 安装环境:win7 1.下载zip安装包: MySQL ...