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使用实例的更多相关文章

  1. 一个简单的Android小实例

    原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台   3.安装J ...

  2. mongodb 简单部署方案及实例

    mongodb 简单部署方案及实例 转载:http://my.oschina.net/zhuzhu0129/blog/53290 第一节 准备工作 一 安装mongodb  我这里选用rehl 5.6 ...

  3. Linux下简单的socket通信实例

    Linux下简单的socket通信实例 If you spend too much time thinking about a thing, you’ll never get it done. —Br ...

  4. 一个简单的jQuery插件开发实例

    两年前写的一个简单的jQuery插件开发实例,还是可以看看的: <script type="text/javascript" src="jquery-1.7.2.m ...

  5. springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目

    一个简单的用户登录系统 用户有账号密码,登录ip,登录时间 打开登录页面,输入用户名密码 登录日志,可以记录登陆的时间,登陆的ip 成功登陆了的话,就更新用户的最后登入时间和ip,同时记录一条登录记录 ...

  6. [WCF REST] 一个简单的REST服务实例

    Get:http://www.cnblogs.com/artech/archive/2012/02/04/wcf-rest-sample.html [01] 一个简单的REST服务实例 [02] We ...

  7. PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例

    前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...

  8. Hibernate入门2.简单的项目开发实例

    Hibernate入门2.简单的项目开发实例 这一节通过一个简单的项目学习Hibernate项目的配置 代码下载 : 链接: http://pan.baidu.com/s/1zlgjl 密码: p34 ...

  9. 简单的Slony-I设置实例 II

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页 接前面例子, 简单的Slony-I设置实例 这次我 ...

随机推荐

  1. centos7.4下的python3.6的安装

    1.系统环境 :centos 7.4 最小化安装 2.安装过程 yum install wget      安装下载工具 wget https://www.python.org/ftp/python/ ...

  2. mysql—增删改查语句总结

    关于MySQL数据库——增删改查语句集锦 一.基本的sql语句 CRUD操作: create 创建(添加) read 读取 update 修改 delete 删除 .添加数据 ,'n001','201 ...

  3. 高阶函数(Higher-order function)

    变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下代码: >>> abs(-15) 15 但是,如果只写abs呢? >>> abs ...

  4. Flask 之东方不败一

    1,flask的初始 flask是Python的一个轻量级的web框架,相当于django而言. 知识点Python 三大主流web框架的对比 1.Django 主要特点是大而全,集成了很多组件,例如 ...

  5. sqlmap注入入门

    sqlmap注入入门 sqlmap的用法: ​ linux中: sqlmap [选项] ​ Windows中: python sqlmap [选项] 常用的参数及含义: 目标 ​ -d DIRECT ...

  6. MongoDB代码——Python篇

    需要安装的库:pymongo 一.添加文档 from pymongo import MongoClient # 连接服务器 conn = MongoClient("localhost&quo ...

  7. xls 打乱序列 -和给拼接字符串加上双引号

    打乱  给空列 添加函数 =RAND() ,下拉,排序,即可打乱 添加双引号: =""""&C1&"""" ...

  8. Mathematica/偏导数/最小二乘法(线性回归)

    a = / a //输出的还是2/123 N[a] //输出的就是小数点 N[a,] //保留三位小数点 Clear[a] Solve[== x^- , x] //结果-3 和 3 Plot[Sin[ ...

  9. 阻塞IO,非阻塞IO,IO多路复用模型

    #服务端 import socket sk = socket.socket() sk.bind(('127.0.0.1',8080)) sk.listen() while True: conn, ad ...

  10. 【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树

    按层输出二叉树,广度优先. 3 / \ 9 20 / \ 15 7 [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * ...