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. SSH框架整合,css、js会被过滤器过滤掉

    如果是默认状态 <!--struts2过滤器--> <filter> <filter-name>struts2</filter-name> <fi ...

  2. MAC之间共享屏幕

    A 机设置-->共享-->屏幕共享 ☑️勾选上,打开屏幕共享.   B 机 safari 里输入 vnc://ip 回车就可以共享屏幕了   转载请注明出处:https://www.cnb ...

  3. ng工程升级cli版本

    全局更新ng 然后在工程里 ng update @angular/cli @angular/core

  4. Linux(CentOS 7)命令行模式安装VMware Tools 详解

    本篇文章主要介绍了如何在Linux(CentOS 7)命令行模式安装VMware Tools,具有一定的参考价值,感兴趣的小伙伴们可以参考一下. 本例中为在Linux(以CentOS 7为例)安装VM ...

  5. JavaScript基础四

    1.13 Js中的面向对象 1.13.1 创建对象的几种常用方式 1.使用Object或对象字面量创建对象 2.工厂模式创建对象 3.构造函数模式创建对象 4.原型模式创建对象 1.使用Object或 ...

  6. UPX脱壳全程分析(转)

    [文章标题]: UPX脱壳全程分析 [保护方式]: 本地验证 [使用工具]: OllyDBG [作者声明]: 只是感兴趣,没有其他目的.失误之处敬请诸位大侠赐教! ------------------ ...

  7. MySQL三层循环

    begindeclare i int;  #定义i变量declare j int;  #定义j变量declare k int;  #定义k变量set i=1;set j=1;set k=1;while ...

  8. vs2013+qt5.3.2+pcl1.8.0+osg2.3开发环境配置

    一.安装vs2013 注意:最好安装VS2013 Update 5,不然有些项目会报莫名其妙的异常. VS2013 Update 5下载:https://pan.baidu.com/s/1TAU5Qi ...

  9. vue教程自学笔记(二)

    三.模板语法 1.文本 数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值. 通过v-once指令,你也能执行一次性地插值,当数据改变时,插值处的内容不会更新. 2.原始H ...

  10. 关于c#连接数据库的代码

    using System;using System.Collections.Generic;using System.Data;using System.Data.SQLite;using Syste ...