`

java实时进度条

 
阅读更多

Java实时进度条的操纵类

package com.dotmin.web.java.progress;

/**
 * 实时进度条操纵类
 * @author Administrator
 */
public class ProgressBarCommon {
	/**
	 * 换行符
	 */
	private final String NEW_LINE = "\r\n";
	
	/**
	 * 获取进度条页面的基础HTML
	 * @return
	 */
	public String getBaseHtml(String user, String now) {
		StringBuilder builder = new StringBuilder();
		builder.append("<%@ page pageEncoding=\"UTF-8\"%><head>" + NEW_LINE);
		builder.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" + NEW_LINE);
		builder.append("<title>短信发送进度</title>" + NEW_LINE);
		builder.append("<link href=\"Sms.css\" rel=\"stylesheet\" type=\"text/css\">" + NEW_LINE);
		builder.append("<script language=\"javascript\" type=\"text/javascript\">" + NEW_LINE);
		builder.append("var g_inits = 3;" + NEW_LINE);
		builder.append("var g_tels = 500;" + NEW_LINE);
		builder.append("var g_lates = 2;" + NEW_LINE);
		builder.append("var g_barmax = 500;" + NEW_LINE);
		builder.append("//设置指定名称元素的内容" + NEW_LINE);
		builder.append("function setTxt(elemName, elemText){" + NEW_LINE);
		builder.append("	document.getElementById(elemName).innerHTML = elemText;" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//设置指定名称元素的宽度" + NEW_LINE);
		builder.append("function setPos(elemName, elemWidth){" + NEW_LINE);
		builder.append("	document.getElementById(elemName).style.width = elemWidth + \"px\";" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//根据当前位置和最大位置,获取子进度条长度" + NEW_LINE);
		builder.append("function getBarPos(pos, maxnum){" + NEW_LINE);
		builder.append("	var barpos = Math.ceil(pos * g_barmax / maxnum);" + NEW_LINE);
		builder.append("	if(barpos<1){" + NEW_LINE);
		builder.append("		return 1;" + NEW_LINE);
		builder.append("	}else if(barpos>g_barmax){" + NEW_LINE);
		builder.append("		return g_barmax;" + NEW_LINE);
		builder.append("	}else{" + NEW_LINE);
		builder.append("		return barpos;" + NEW_LINE);
		builder.append("	}" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//初始化个数" + NEW_LINE);
		builder.append("function setInits(inits){" + NEW_LINE);
		builder.append("	g_inits = inits;" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//总号码数" + NEW_LINE);
		builder.append("function setTels(tels){" + NEW_LINE);
		builder.append("	g_tels = tels;" + NEW_LINE);
		builder.append("	setTxt(\"tels\", tels);" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//清理数" + NEW_LINE);
		builder.append("function setLates(lates){" + NEW_LINE);
		builder.append("	g_lates = lates;" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//进度条1的文本, 进度" + NEW_LINE);
		builder.append("function b1(txt, pos){" + NEW_LINE);
		builder.append("	setTxt(\"txt1\", txt);" + NEW_LINE);
		builder.append("	setPos(\"bar1\", getBarPos(pos, g_inits));" + NEW_LINE);
		builder.append("	setTxt(\"tip1\", pos + \"/\" + g_inits);" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//进度条2的文本, 进度" + NEW_LINE);
		builder.append("function b2(txt, pos){" + NEW_LINE);
		builder.append("	setTxt(\"txt2\", txt);" + NEW_LINE);
		builder.append("	setPos(\"bar2\", getBarPos(pos, g_tels));" + NEW_LINE);
		builder.append("	setTxt(\"tip2\", pos);" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//进度条3的成功进度" + NEW_LINE);
		builder.append("function b31(pos){" + NEW_LINE);
		builder.append("	setPos(\"bar3\", getBarPos(pos, g_tels));" + NEW_LINE);
		builder.append("	setTxt(\"tip3\", pos);" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//进度条3的失败文本" + NEW_LINE);
		builder.append("function b32(txt){" + NEW_LINE);
		builder.append("	setTxt(\"txt3\", txt);" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//进度条4的文本, 进度" + NEW_LINE);
		builder.append("function b4(txt, pos){" + NEW_LINE);
		builder.append("	setTxt(\"txt4\", txt);" + NEW_LINE);
		builder.append("	setPos(\"bar4\", getBarPos(pos, g_lates));" + NEW_LINE);
		builder.append("	setTxt(\"tip4\", pos + \"/\" + g_lates);" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//发送成功" + NEW_LINE);
		builder.append("function succ(millisec){" + NEW_LINE);
		builder.append("	setTxt(\"ret_result\", \"发送成功!\");" + NEW_LINE);
		builder.append("	setTxt(\"ret_text\", \"群发耗时:\" + (millisec / 1000) + \"秒\");" + NEW_LINE);
		builder.append("	alert(\"发送成功!\");" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("//发送失败的原因" + NEW_LINE);
		builder.append("function error(txt){" + NEW_LINE);
		builder.append("	setTxt(\"ret_result\", \"群发失败!\");" + NEW_LINE);
		builder.append("	setTxt(\"ret_text\", txt);" + NEW_LINE);
		builder.append("	alert(\"群发失败!\");" + NEW_LINE);
		builder.append("}" + NEW_LINE);
	    builder.append("//停止发送" + NEW_LINE);
		builder.append("function smsStop(){" + NEW_LINE);
		builder.append("	try{" + NEW_LINE);
		builder.append("		var req;" + NEW_LINE);
		builder.append("		if (window.ActiveXObject) {" + NEW_LINE);
		builder.append("			req = new ActiveXObject(\"Microsoft.XMLHTTP\");" + NEW_LINE);
		builder.append("		} else if (window.XMLHttpRequest) {" + NEW_LINE);
		builder.append("			req = new XMLHttpRequest();" + NEW_LINE);
		builder.append("		} else {" + NEW_LINE);
		builder.append("			alert(\"当前系统不支持此操作!\");" + NEW_LINE);
		builder.append("			return;" + NEW_LINE);
		builder.append("		}" + NEW_LINE);
		builder.append("		req.open(\"GET\", \"SmsStopServlet\", true);" + NEW_LINE);
		builder.append("		req.onreadystatechange = function(){" + NEW_LINE);
		builder.append("			if (req.readyState == 4) {" + NEW_LINE);
		builder.append("				if (req.status != 200) {" + NEW_LINE);
		builder.append("					alert(\"中止失败!\" + req.statusText);" + NEW_LINE);
		builder.append("				}" + NEW_LINE);
		builder.append("			}" + NEW_LINE);
		builder.append("		};" + NEW_LINE);
		builder.append("		req.send();" + NEW_LINE);
		builder.append("	}catch(e){" + NEW_LINE);
		builder.append("		alert(\"出错了!\" + e.message);" + NEW_LINE);
		builder.append("	}" + NEW_LINE);
		builder.append("}" + NEW_LINE);
		builder.append("</script>" + NEW_LINE);
		builder.append("</head>" + NEW_LINE);
		builder.append("<table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\" class=\"jason_sms_word\">" + NEW_LINE);
		builder.append("      <input type=\"button\" onclick=\"location.href='Sendindex.jsp'\" value=\"返回首页\" />" + NEW_LINE);
		builder.append("      <input type=\"button\" onclick=\"location.href='TelList.jsp'\" value=\"返回群发页面\" />" + NEW_LINE);
	    builder.append("      &nbsp;&nbsp;<input type=\"button\" onclick=\"smsStop()\" value=\"停止发送\" />" + NEW_LINE);
		builder.append("    </td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\"><label id=\"txt1\"></label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td width=\"500\" height=\"30\">" + NEW_LINE);
		builder.append("      <div style=\"width:508px; height:20px; background-color:#CCC; border:double\">" + NEW_LINE);
		builder.append("        <div id=\"bar1\" style=\"width:1px; height:100%; background-color:#66F\"></div>" + NEW_LINE);
		builder.append("      </div>" + NEW_LINE);
		builder.append("    </td>" + NEW_LINE);
		builder.append("    <td>&nbsp;<label id=\"tip1\">0/0</label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\"><label id=\"txt2\"></label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\">" + NEW_LINE);
		builder.append("      <div style=\"width:508px; height:20px; background-color:#CCC; border:double\">" + NEW_LINE);
		builder.append("        <div id=\"bar2\" style=\"width:1px; height:100%; background-color:#66F\"></div>" + NEW_LINE);
		builder.append("      </div>" + NEW_LINE);
		builder.append("    </td>" + NEW_LINE);
		builder.append("    <td>&nbsp;已发送<label id=\"tip2\">0</label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\"><label id=\"txt3\"></label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\">" + NEW_LINE);
		builder.append("      <div style=\"width:508px; height:20px; background-color:#CCC; border:double\">" + NEW_LINE);
		builder.append("        <div id=\"bar3\" style=\"width:1px; height:100%; background-color:#66F\"></div>" + NEW_LINE);
		builder.append("      </div>" + NEW_LINE);
		builder.append("    </td>" + NEW_LINE);
		builder.append("    <td>&nbsp;成功数<label id=\"tip3\">0</label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\"><label id=\"txt4\"></label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\">" + NEW_LINE);
		builder.append("      <div style=\"width:508px; height:20px; background-color:#CCC; border:double\">" + NEW_LINE);
		builder.append("        <div id=\"bar4\" style=\"width:1px; height:100%; background-color:#66F\"></div>" + NEW_LINE);
		builder.append("      </div>" + NEW_LINE);
		builder.append("    </td>" + NEW_LINE);
		builder.append("    <td>&nbsp;<label id=\"tip4\">0/0</label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("</table>" + NEW_LINE);
		builder.append("<table width=\"600\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\">" + NEW_LINE);
		builder.append("      号码数量:<label id=\"tels\">0</label> 个" + NEW_LINE);
		builder.append("    </td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\">" + NEW_LINE);
		builder.append("      短信内容:<label id=\"content\"></label>" + NEW_LINE);
		builder.append("    </td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\">群发结果:<label id=\"ret_result\"></label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("  <tr>" + NEW_LINE);
		builder.append("    <td height=\"30\" colspan=\"2\" class=\"jason_sms_word\"><label id=\"ret_text\"></label></td>" + NEW_LINE);
		builder.append("  </tr>" + NEW_LINE);
		builder.append("</table>" + NEW_LINE);
		return builder.toString();
	}

	/**
	 * 设置指定名称元素的内容
	 * @param inits
	 * @return
	 */
	public String getTxt(String elemName, String elemText) {
		return "<script>setTxt(\"" + clearLine(elemName) + "\", \"" + elemText + "\")</script>";
	}

	/**
	 * 设置初始化总数
	 * @param inits
	 * @return
	 */
	public String getInits(int inits) {
		return "<script>setInits(" + inits + ")</script>";
	}
	
	/**
	 * 设置号码总数
	 * @param mails
	 * @return
	 */
	public String getTels(int tels) {
		return "<script>setTels(" + tels + ")</script>";
	}
	
	/**
	 * 设置清理总数
	 * @param lates
	 * @return
	 */
	public String getLates(int lates) {
		return "<script>setLates(" + lates + ")</script>";
	}
	
	/**
	 * 进度条1
	 * @param txt 显示文本
	 * @param pos 进度
	 * @return
	 */
	public String getBar1(String txt, int pos) {
		return "<script>b1(\"" + clearLine(txt) + "\", " + pos + ")</script>";
	}
	
	/**
	 * 进度条2
	 * @param txt 显示文本
	 * @param pos 进度
	 * @return
	 */
	public String getBar2(String txt, int pos) {
		return "<script>b2(\"" + clearLine(txt) + "\", " + pos + ")</script>";
	}
	
	/**
	 * 进度条3,成功
	 * @param pos 进度
	 * @return
	 */
	public String getBar3(int pos) {
		return "<script>b31(" + pos + ")</script>";
	}
	
	/**
	 * 进度条3,失败
	 * @param txt 显示文本
	 * @return
	 */
	public String getBar3(String txt) {
		return "<script>b32(\"" + clearLine(txt) + "\")</script>";
	}
	
	/**
	 * 进度条4
	 * @param txt 显示文本
	 * @param pos 进度
	 * @return
	 */
	public String getBar4(String txt, int pos) {
		return "<script>b4(\"" + clearLine(txt) + "\", " + pos + ")</script>";
	}
	
	/**
	 * 群发成功
	 * @return
	 */
	public String getSucc(long times) {
		return "<script>succ(" + times + ")</script>";
	}
	
	/**
	 * 群发失败
	 * @param txt 失败原因
	 * @return
	 */
	public String getErr(String txt) {
		return "<script>error(\"" + clearLine(txt) + "\")</script>";
	}
	
	/**
	 * 第一次Flush时的内容至少要有256字节
	 * @return 返回至少256个字符
	 */
	public String check256byte(String src) {
		while(src.length() < 256){
			src += "&nbsp;";
		}
		return src;
	}
	
	/**
	 * 去掉换行符
	 * @param srcStr 原内容
	 * @return 去掉换行符后的内容
	 */
	private String clearLine(String srcStr){
		if(srcStr.contains("\n")){
			srcStr = srcStr.replaceAll("\n", " ");
		}
		if(srcStr.contains("\"")){
			return srcStr.replaceAll("\"", "'");
		}
		return srcStr;
	}
}

 

 

调用示例:

package progress;

public class MailSendProgress {
	private PrintWriter write;
	private ProgressBarCommon proBar;
	
	public MailSendProgress(PrintWriter write) {
		this.write = write;
		proBar = new ProgressBarCommon();
		writeScript(proBar.getBaseHtml());
		writeScript(proBar.getBar1("正在保存发送日志", 1));

		for(int i=0; i<ListTel.size(); i++){
			if(stoped()){
				printScript(proBar.getErr("用户请求中止发送!"));
				return;
			}
			sendSMS(……);
		}
	}

	/**
	 * 即时输出内容
	 * @param script 要输出的脚本内容
	 */
	private void writeScript(String script){
		write.println(script);
		write.flush();
	}
	
	/**
	 * 检查运行状态
	 * @return true申请中止 false可继续运行
	 */
	private boolean stoped(){
		return null == session.getAttribute("SMS_RUNNING");
	}
}

 

Servlet页面:

package progress;

public class MailSendServlet extends HttpServlet implements Servlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setHeader("content-type","text/html;charset=UTF-8");
		
		//输出基础内容
		MailSendProgress mailSendProgress = new MailSendProgress(resp.getWriter());
		resp.flushBuffer();

		//获取参数
		String txt_sender = req.getParameter("txt_sender");
		String txt_content = req.getParameter("txt_content");
		
		//转码
		txt_sender = (null==txt_sender) ? "" : new String(txt_sender.getBytes("ISO-8859-1"), "UTF-8");
		txt_content = (null==txt_content) ? "" : new String(txt_content.getBytes("ISO-8859-1"), "UTF-8");
	}
}

 发送中止Servlet:

package com.dotmin.web.java.progress;

public class SmsStopServlet extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setHeader("cache-control", "no-cache");
		resp.setHeader("pragma", "no-cache");
		resp.setHeader("expires", "0");
		req.getSession().setAttribute("SMS_RUNNING", null);
	}
}

 

  • 大小: 5.7 KB
分享到:
评论

相关推荐

    Java实现显示进度条

    ### Java 实现显示进度条 #### 背景与需求 在软件开发过程中,特别是涉及到长时间运行的任务时,向用户展示任务完成进度是一项非常实用的功能。进度条是实现这一功能的一种常见方式。本文将详细介绍如何使用Java...

    java实时读取日志+ java 实现进度条实现demo

    在Java编程中,实时读取日志和实现进度条是两个重要的功能,广泛应用于系统监控、调试和用户界面的交互性提升。以下是对这两个知识点的详细讲解。 首先,让我们来看看如何在Java中实时读取日志。日志是程序运行过程...

    前后台交互的实时进度条 支持多浏览器

    在现代Web应用中,用户界面的交互体验是至关重要的,其中实时进度条是一个常见的功能,特别是在数据上传、下载或者后台处理任务时。本资源提供的"前后台交互的实时进度条"是一个实现这一功能的解决方案,它能兼容...

    java实现进度条.pdf

    Java进度条是指在Java GUI开发中,使用JProgressBar组件来显示进度的控件。JProgressBar可以显示进度的百分比,例如下载文件、安装软件等过程中,JProgressBar可以实时显示当前进度。 在本例中,我们使用了...

    基于Comet属性同步的Java Web实时进度条研究.pdf

    "基于Comet属性同步的Java Web实时进度条研究" 本文研究了基于Comet属性同步的Java Web实时进度条的实现方案。该方案通过结合Comet技术和Java的反射技术,实现了Java Web实时进度条的处理方式。该方案的优势在于纯...

    java多线程进度条

    本主题将深入探讨如何在Java多线程环境下实现进度条功能。 首先,理解Java多线程的基本概念至关重要。Java通过Thread类和Runnable接口来支持多线程。创建一个新线程通常有两种方式:继承Thread类并重写run()方法,...

    java 进度条的实现

    实现Java进度条主要涉及GUI(图形用户界面)和多线程技术。以下是对Java进度条实现的详细解释: 1. **Swing和AWT库**:在Java中,我们通常使用Swing或AWT库来创建图形用户界面,这两个库提供了丰富的组件,包括...

    Java的命令行进度条 JCmdLineProgess

    Java的命令行进度条JCmdLineProgress是一种在控制台或者终端显示进度的工具,它能够为执行时间较长的任务提供可视化反馈,使用户了解程序运行的状态。在开发命令行应用程序时,这种工具尤其有用,因为它可以帮助用户...

    java开发页面进度条

    ### Java开发页面进度条知识点详解 #### 一、概述 在Web应用开发中,为了提升用户体验,页面加载进度条已经成为了一种常见的UI组件。通过Java技术实现页面进度条不仅可以直观地展示页面或数据加载的过程,还能增强...

    java带进度条的异步上传

    在Java Web开发中,实现一个带进度条的异步上传功能是常见的需求,尤其是在处理大文件上传时,用户界面的反馈至关重要。这个“java带进度条的异步上传”示例利用了Servlet、Ajax以及可能的JavaScript库,如jQuery,...

    java带进度条大文件上传

    "java带进度条大文件上传"的实现主要涉及到以下几个关键知识点: 1. **虚拟内存技术**:传统的文件上传方式可能会遇到内存限制,特别是对于大文件,一次性读入整个文件到内存可能导致服务器崩溃。为了解决这个问题...

    JAVA 带进度条的文件拷贝程序

    在Java编程中,创建一个带有进度条的文件拷贝程序是一项实用技能,尤其对于那些希望增强用户界面体验的开发者来说。下面将详细讲解这个“JAVA 带进度条的文件拷贝程序”的核心知识点。 首先,我们需要理解的是,...

    java实现进度条

    #### 二、Java进度条的实现 ##### 1. 导入必要的包 首先,我们需要导入Java Swing中的相关包,包括`javax.swing.JFrame`, `javax.swing.JPanel`, `javax.swing.JButton`, `javax.swing.JProgressBar`, `java.awt....

    数据导入与实时进度条实现

    如果使用Java,我们可以利用Swing或JavaFX的GUI组件创建进度条,结合线程池或Future接口来监控任务进度。在微服务或分布式系统中,可以利用消息队列(如RabbitMQ或Kafka)配合事件驱动设计,将进度信息发送到前端...

    Java的命令行进度条 JCmdLineProgess.7z

    Java的命令行进度条是开发基于控制台的Java应用程序时常用的一种增强用户体验的工具。它在执行长时间运行的任务时,如文件传输、大型数据处理或系统扫描时,为用户提供可视化的进度反馈。JCmdLineProgress库就是为了...

    java下载进度条模块(NetBeans工程)

    Java下载进度条模块是用于在Java应用程序中展示文件下载进度的一种可视化组件,它通常与网络I/O操作结合,为用户提供实时的下载状态反馈。在这个NetBeans工程中,开发者可以找到如何构建这样一个功能的实例。 首先...

    JAVA进度条源代码

    Java进度条是用户界面(UI)设计中常见的一种元素,用于提供给用户关于长时间运行任务的进度反馈。在Java编程中,我们可以使用Java Swing或JavaFX库来创建进度条。以下将详细介绍如何在Java中实现进度条功能,以及...

    Netty+H5实现实时进度条文件上传,支持断点续传

    Netty+H5实现实时进度条文件上传,支持断点续传。 1、WebSocketServer:服务启动类 2、服务启动后,浏览器访问http://localhost:9999 3、FileWebSocketFrameHandler类SERVER_SAVE_PATH常量为文件上传保存路经

    java gui进度条

    本篇文章将深入探讨如何使用Java中的`JProgressBar`类来创建和管理进度条。 `JProgressBar`是Java Swing库中的一个组件,属于javax.swing包。它提供了一种可视化的方式,用以展示任务完成的百分比或者进度。`...

Global site tag (gtag.js) - Google Analytics