java 实现自定义事件
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 实现自定义事件的更多相关文章
- Java Spring 自定义事件监听
ApplicationContext 事件 定义一个context的起动监听事件 import org.springframework.context.ApplicationListener; imp ...
- java事件监听机制(自定义事件)
java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中作为参数,一般存在与listerner的方法之中 2.event source ...
- java自定义事件机制分析
import java.util.ArrayList; import java.util.EventListener; import java.util.EventObject; import jav ...
- java事件处理机制(自定义事件)
java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中,作为参数,一般存在与listerner的方法之中 2.event sourc ...
- C# 自定义事件
C#自定义事件和java有所不同,涉及到委托.下面代码包括自定义事件从事件定义到事件触发和执行的全过程. using System; using System.Collections.Generic; ...
- springboot-admin自定义事件通知
springboot-admin组建已经提供了很多开箱即用的通知器(例如邮件),但在有些业务场景下我们需要做一些企业内部的通知渠道,这就需要我们来自定义通知器. 实现其实很简单,只需要往spring注 ...
- C#的委托与Java的自定义接口的异曲同工的同步操作
C#的委托(以WinForm为例) 在子窗体(ChildFrm)中定义一个委托 this.CaptureListener(callback);//子窗体触发委托事件,以告诉调用的窗体 /// < ...
- Spring(十)之自定义事件
编写自定义事件的简单流程如下: (1)编写CustomEvent.java package com.tutorialspoint; import org.springframework.context ...
- Spring Boot(六)自定义事件及监听
事件及监听并不是SpringBoot的新功能,Spring框架早已提供了完善的事件监听机制,在Spring框架中实现事件监听的流程如下: 自定义事件,继承org.springframework.con ...
随机推荐
- [POI1999][LOJ10112]原始生物
典型的有向图K笔画的问题 最后答案就是n+1-1+k 1笔画有一点入度比出度少1 k笔画则统计入度比出度少的点中所有少的总和 #include<bits/stdc++.h> using n ...
- Codeforces 40 E. Number Table
题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...
- Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)
Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...
- CentOs系统设置python版本
一.针对当前终端生效 最近云服务器安装了centos7系统,python默认版本是2.7.5,但是习惯用anaconda3, 安装anaconda3之后将系统默认python版本更改为python3. ...
- 『计算机视觉』Mask-RCNN_训练网络其二:train网络结构&损失函数
Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...
- 6月19 使用tp框架生成验证码及文件上传
ThinkPHP中自带能生成验证码的类:ThinkPHP/Library/Think/Verify.class.php 默认情况下,验证码的字体是随机使用 ThinkPHP/Library/Think ...
- 通用Mapper环境下,mapper接口无法注入问题
写了一个mapper接口 package com.nyist.mapper; import com.nyist.entity.User; import tk.mybatis.mapper.common ...
- ID基本操作(复制页面)(移动页面)(调整跨页页数)(版面调整)5.16
1.在页面面板中选择要复制的页面.拖动到新建页面图标上就可以新建页面. 2.在页面面板中选择要复制的页面.点击右上角的下箭头选择直接复制跨页. 3.在页面面板中选择要移动的页面.用鼠标拖到要移动的地方 ...
- java九九乘法表
public class Nine { public static void main(String[] args) { //外层循环控制行数 for (int i = 9;i >=1;i--) ...
- Python之路-函数
一.函数是什么: python中函数定义:函数是逻辑结构化和过程化的一种编程方法.定义函数的方法为: def function(): ""The function definiti ...