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 ...
随机推荐
- POJ1141 Brackets Sequence---区间DP+输出路径
题目意思就是输入一串括号,让你找到最小的补偿数目使括号串合法,并且输出补全后的串. 基本是区间DP的模板题,该题特别让你输出补全后的答案.这和区间dp的反向思路很像,就是把一个大的区间划分为多个互不干 ...
- Mac OS X 显示和隐藏文件
参考: mac系统如何显示和隐藏文件 Mac OSX系统 显示和隐藏文件 1.显示隐藏文件: 打开Terminal,并输入以下命令: defaults write com.apple.finder A ...
- Sql语句中IN和exists的区别及应用
表展示 首先,查询中涉及到的两个表,一个user和一个order表,具体表的内容如下: user表: order表: in 确定给定的值是否与子查询或列表中的值相匹配.in在查询的时候,首先查询子查询 ...
- Promise 异步函数的加上外壳终止Promise
//promise异步函数的请求终止 export default function markCancleble(promise){ let hasCanceled_ = false ; const ...
- leecode第一百四十八题(排序链表)
class Solution { public: void sort_list(ListNode* head1, ListNode* head2,int len)//在原链表上进行排序 { ListN ...
- webpack点滴
一个比较完整的webpack的配置,自己配置不断更新. const path = require('path') const configs = require('./configs/') const ...
- ionic3 双向数据绑定失效 脏值检测失效
最近在使用ionic3过程中,使用了eval()方法进行字符串拼接成一个function使用 在eval()方法中,只能使用局部变量,全局变量无法使用,ionic3的this在eval中失效(unde ...
- 『TensorFlow』slim模块常用API
辅助函数 slim.arg_scope() slim.arg_scope可以定义一些函数的默认参数值,在scope内,我们重复用到这些函数时可以不用把所有参数都写一遍,注意它没有tf.variable ...
- HTTP 错误 500.19 - Internal Server Error v4.0.30319
1 打开运行,输入cmd进入到命令提示符窗口.2 进入到C:\Windows\Microsoft.NET\Framework\v4.0.30319 目录.3 输入aspnet_regiis.exe - ...
- 2010-10-08在浏览器中兼容+jQuery3
一.实现背景图片铺满(兼容各种浏览器) <script type="text/javascript"> $(document).ready(function() { $ ...