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. 如何恢复IIS出厂默认设置

    How to restore IIS settings and Default Web Site? http://superuser.com/questions/704850/how-to-resto ...

  2. Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)

    Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...

  3. 【转载】URL编码与两次encodeURI

    当使用地址栏提交查询参数时,如果不编码,非英文字符会按照操作系统的字符集进行编码提交到服务器,服务器会按照配置的字符集进行解码,所以如果两者不一致就会导致乱码. encodeURI函数采用UTF-8对 ...

  4. centos install jdk

    =========== 查询jdk版本 ===========yum search jdk =========== 安装jdk 64位开发版 ===========yum -y install jav ...

  5. 问题1——之Linux虚拟机ip地址消失

    原文转自 https://blog.csdn.net/keep_walk/article/details/75115926 以前一直通过ifconfig命令查看ip地址,但是今天用XShell连接自己 ...

  6. idea提示不区分大小写,解决方法

  7. 开机自动启动WEB服务,共享目录。

    最近工作中,需要共享一个目录,每次重启总要手动执行一下  nohup python -m SimpleHTTPServer 8000这个命令,想着实现让它开机自动启动,就一劳永逸了. 手动步骤如下: ...

  8. mapping values are not allowed in this context at line 115 column 10

    /opt/vagrant/embedded/lib/ruby//psych.rb::in `parse': (<unknown>): mapping values are not allo ...

  9. git命令及远程仓库操作内容整理

    0.在git官网上下载git对应的适配系统版本 进入到需要管理的目录,打开git终端 1.git相关的基础命令: git init 在当前文件夹下创建一个.git的隐藏文件夹,初始化版本控制器 注:不 ...

  10. Cassandra.yaml 配置详解

    cluster_name 设置Cassandra集群的名称. 在Cassandra集群中,每一台服务器都必须具备相应的集群的名称.如果名称不一致,则当前Cassandra服务器无法加入集群. init ...