import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List; public class Test { public static void main(String[] args)
{
Test.FileDownloader fd = new FileDownloader("http://download.microsoft.com/download/e/c/9/ec94a5d4-d0cf-4484-8b7a-21802f497309/Vs6sp6.exe"
, "f:\\Vs6sp6.exe"); fd.addDownloadListener(new DownloadListener() {
@Override
public void DownloadProgress(int totalLength ,int bytesCompleted) {
System.out.println("订阅者1进度,已下载:" + bytesCompleted + "字节");
}
}); fd.addDownloadListener(new DownloadListener() {
@Override
public void DownloadProgress(int totalLength ,int bytesCompleted) {
double percent = (double)bytesCompleted*100/(double)totalLength;
DecimalFormat df = new java.text.DecimalFormat("#.00");
System.out.println("订阅者2进度,已下载:" + df.format(percent) + "%");
}
}); fd.download();
System.out.println("任务下载完成!");
} public static class FileDownloader implements DownloadListener
{
private List<DownloadListener> downloadListeners = new ArrayList<DownloadListener>(); private String URL = null;
private String savePath = null; public FileDownloader(String url, String savePath)
{
this.URL = url;
this.savePath = savePath;
} @SuppressWarnings("resource")
public boolean download()
{
int bytesum = 0;
int byteread = 0; URL url;
try {
url = new URL(this.URL);
} catch (MalformedURLException e1) {
e1.printStackTrace();
return false;
} try {
URLConnection conn = url.openConnection();
int totalLength = conn.getContentLength();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream(this.savePath); byte[] buffer = new byte[1204];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
DownloadProgress(totalLength ,bytesum);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (Exception e){
e.printStackTrace();
return false;
} return true;
} @Override
public void DownloadProgress(int totalLength ,int bytesCompleted) {
for(DownloadListener listener : downloadListeners){
listener.DownloadProgress(totalLength ,bytesCompleted);
}
} public void addDownloadListener(DownloadListener listener)
{
downloadListeners.add(listener);
}
} public interface DownloadListener extends EventListener
{
public void DownloadProgress(int totalLength ,int bytesCompleted);
} }

java 实现自定义事件的更多相关文章

  1. Java Spring 自定义事件监听

    ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...

  2. java事件监听机制(自定义事件)

    java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中作为参数,一般存在与listerner的方法之中 2.event source ...

  3. java自定义事件机制分析

    import java.util.ArrayList; import java.util.EventListener; import java.util.EventObject; import jav ...

  4. java事件处理机制(自定义事件)

    java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...

  5. C# 自定义事件

    C#自定义事件和java有所不同,涉及到委托.下面代码包括自定义事件从事件定义到事件触发和执行的全过程. using System; using System.Collections.Generic; ...

  6. springboot-admin自定义事件通知

    springboot-admin组建已经提供了很多开箱即用的通知器(例如邮件),但在有些业务场景下我们需要做一些企业内部的通知渠道,这就需要我们来自定义通知器. 实现其实很简单,只需要往spring注 ...

  7. C#的委托与Java的自定义接口的异曲同工的同步操作

    C#的委托(以WinForm为例) 在子窗体(ChildFrm)中定义一个委托 this.CaptureListener(callback);//子窗体触发委托事件,以告诉调用的窗体 /// < ...

  8. Spring(十)之自定义事件

    编写自定义事件的简单流程如下: (1)编写CustomEvent.java package com.tutorialspoint; import org.springframework.context ...

  9. Spring Boot(六)自定义事件及监听

    事件及监听并不是SpringBoot的新功能,Spring框架早已提供了完善的事件监听机制,在Spring框架中实现事件监听的流程如下: 自定义事件,继承org.springframework.con ...

随机推荐

  1. java8中的时间处理

    java8中关于时间的处理整理 package com.xb.utils; import java.time.*; import java.time.format.DateTimeFormatter; ...

  2. Mac+Apache+PHP 安装 Xdebug 方法

    MAC homebrew自2018/3/31之后弃用homebrew/php By 31st March we will deprecate and archive the Homebrew/php ...

  3. 『Python CoolBook:heapq』数据结构和算法_heapq堆队列算法&容器排序

    一.heapq堆队列算法模块 本模块实现了堆队列算法,也叫作优先级队列算法.堆队列是一棵二叉树,并且拥有这样特点,它的父节点的值小于等于任何它的子节点的值. 本模块实际上实现了一系列操作容器的方法,使 ...

  4. React文档(十四)深入JSX

    根本上,JSX只是为React.createElement(component, props, ...children)函数提供语法糖.JSX代码是这样的: <MyButton color=&q ...

  5. java 常见语法,但是发现switch等基础,常见面试套路不会了,待补充

    1,面向对象,万物皆对象,面向对象的三个基本特性:封 装,继承,多态. 面向对象的程序都是由类组成,类封装了对象的属性和行为. 封装:隐藏对象的属性和实现细节,仅对外提供接口. 继承:实现代码复用,将 ...

  6. DBMS_NETWORK_ACL_ADMIN (OCP 053 第七题)

    You create an access control list(ACL)using the DBMS_NETWORK_ACL_ADMIN package It is a list of users ...

  7. Python- - -函数目录

    一.函数的定义.调用.返回值.参数. 二.名称空间.作用域.加载顺序.取值顺序. 三.函数的嵌套.作用域链.函数名的应用.闭包. 四.装饰器

  8. 安装tifffile

    python的opencv和PIL不能很好地处理tiff图像,所以我就转用tifffile 但是tifffile也装了很近,因为我是远控别人的电脑,所以看不太清报错,就自己瞎猜,不过还是装好了 首先去 ...

  9. 颜色(color)透明

    颜色写法: 1.英文单词 backgroud-color:red: 2. #fff background-color:#fff; 3.#fefefe background-color:fefefe; ...

  10. 2015-10-12 jQuery4

    十. 直接获取.编辑内容 1.获取内容 alert($("#d1").text());    //获取文本内容 alert($("#d1").html()); ...