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. docker学习------centos7.5下的swarm集群可视化构建

    1.swarm集群 manager : 192.168.211.175 agent1    : 192.168.211.176    agent2    :  192.168.211.177 2.环境 ...

  2. ASP.NET之页面传值

    一.目前在ASP.NET中页面传值共有这么几种方式: 1.使用QueryString变量QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全 ...

  3. git出现refusing to merge unrelated histories

    问题描述当本地分支与远程分支没有共同祖先时,会出现 fatal: refusing to merge unrelated histories 的问题. 解决方案可以使用 rebase 的方式来进行合并 ...

  4. ueditor接入秀米编辑器

    秀米编辑器用来编辑微信页面很方便,功能也比较强大.秀米提供了第三方编辑器接入的功能,接入方法可以参照官网示例:http://hgs.xiumi.us/uedit/ 但是这里有几点要注意: 1. 示例中 ...

  5. 软件测试自动化…python学习到什么程度?代码好不好学!

    软件测试自动化…python学习到什么程度?代码好不好学! 如下:

  6. LeetCode第二十四题-交换链表中节点值

    Swap Nodes in Pairs 问题简介:给定链表,交换每两个相邻节点并返回链表. 举例: 输入:1->2->3->4 输出:2->1->4->3 链表结构 ...

  7. 二、vue之 使用vscode配置

    vue之 使用vscode配置 visual  studio code 简称vscode,下图为图标 ... 开启vscode ... 1.导入文件夹,选择菜单栏 文件-打开文件夹->选择相应已 ...

  8. jsp参数乱码解决

    iframe src引入jsp,?跟着的中文参数会乱码 解决方法: var CJJG=encodeURI(encodeURI(value.data.CJJG));//js里面转码一次 //jsp页面里 ...

  9. Ajax 统一处理token失效

    complete:function ajax 请求完成激活本事件.这个方法很方便完成ajax 异常的处理. $.ajaxSetup({ contentType:"application/x- ...

  10. centos7系统部署cobbler批量安装系统

    系统环境: 一.开启两个网卡.一个仅主机模式,一个桥接模式,主机模式对内提供cobbler服务 [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWE ...