java实时进度条

4 篇文章 0 订阅
3 篇文章 0 订阅

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\" οnclick=\"location.href='Sendindex.jsp'\" value=\"返回首页\" />" + NEW_LINE);
		builder.append("      <input type=\"button\" οnclick=\"location.href='TelList.jsp'\" value=\"返回群发页面\" />" + NEW_LINE);
	    builder.append("      &nbsp;&nbsp;<input type=\"button\" οnclick=\"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);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值