<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>显示进度</title>
<script type="text/javascript">
-
function a() {
var xhr = null;
if(window.XMLHttpRequest)
xhr = new XMLHttpRequest();
if(window.ActiveXObject)
xhr = new ActiveXObject("Microsoft.XMLHTTP");
// 打开与某个资源的连接
xhr.open("get","${pageContext.request.contextPath }/servlet/ProgressServlet?t="+new Date().getTime(), false);
// 调用send 方法发送请求,post方式需要发送消息体,get方式则不用直接传入null值
xhr.send(null);
// 访问 responseText 属性获得 Servlet 回送的数据
var data = xhr.responseText;
//可以向这个id的位置写入HTML代码
window.parent.document.getElementById("info").innerHTML = data;
}
window.onload = function() {
window.parent.document.getElementById("mysubmit").onclick = function() {
//即浏览器第隔1称秒都会执行一次a函数。
window.setInterval(a, 1000);
}
}
</script>
</head>
<body>
<div id="info">准备上传</div><br>
</body>
</html>
在另一个网页中添加这样的代码就可以实现文档中的文档,或者浮动的框架,来加载上面的网页,从而隐藏起来运行,
<iframe name="top" src="${pageContext.request.contextPath }/progress.jsp" style="display:none;"></iframe>
添加服务器端的进度监听
ServletFileUpload fileUpload = new ServletFileUpload(
new DiskFileItemFactory());
// 添加进度条监听
fileUpload.setProgressListener(new ProgressListener() {
private long temp = -1;
// 上传过程中 不断地触发该方法
public void update(long pBytesRead, long pContentLength, int pItems) {
// 获得上传的百分比 100 4
long percent = pBytesRead*100/pContentLength;
if(percent==temp)
return ;
//System.out.println("已上传:" + percent + "%");
request.getSession().setAttribute("percent", percent);
temp = percent;
}
});