<%@ page language="java" contentType="text/html; charset=GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'upload.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <mce:script type="text/javascript"><!-- var xmlHttp ; var percent ; function createXMLHttp(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else{ xmlHttp = new XMLHttpRequest() ; } } function ajaxSend(){ createXMLHttp() ; var content = "status=status" ; var url ="upload"; xmlHttp.onreadystatechange = handler ; xmlHttp.open("POST",url,true) ; xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(content) ; } function handler(){ //如果readyState处理"Ready"状态 if(xmlHttp.readyState == 4) { //返回的状态代码200意味着一切顺利 if(xmlHttp.status == 200){ //如果responseText不空 percent = xmlHttp.responseText ; var complete = document.getElementById("com") ; var last = document.getElementById("last") ; var p = document.getElementById("percent") ; var flag = document.getElementById("shangchuan"); if(percent <99 ){ p.innerHTML="已经上传了:"+percent+"%" ; setTimeout("ajaxSend()",10); } else{ percent = 100 ; p.innerHTML="已经全部上传" ; //document.getElemmentById("shangchuan").setAttribute("abled","abled"); } complete.setAttribute("width",percent+"%") ; last.setAttribute("width",(100-percent)+"%") ; } } return true; } function mySubmit(form){ form.action="upload" ; form.submit() ; ajaxSend() ; document.getElementById("status").style.visibility="visible" ; event.srcElement.setAttribute("disabled","disabled") ; } // --></mce:script> </head> <body> <form enctype="multipart/form-data" name="fileForm" method="post" target="target_upload"> <input type="file" value="浏览" name="fileUpload" /> <input type="button" id="shangchuan" value="上传" οnclick="mySubmit(fileForm)" /> </form> <div id="status" style="visibility:hidden" mce_style="visibility:hidden"> <table width="25%" align="left"> <tbody> <tr> <td width="100%"> <table align="center" width="100%"> <tbody> <tr height="20px"> <td width="0%" bgcolor="green" id="com"> </td> <td width="100%" bgcolor="#CCCCCC" id="last"> </td> </tr> </tbody> </table> </td> </tr> <tr> <td id="percent" align="center"> <td id="test" align="center"> </td> </tr> </tbody> </table> </div> <div id='div'> </div> </body> </html> demo.jsp:: upload.java package myupload.progressbar; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class Upload extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("GBK"); response.setContentType("text;charset=ISO-8859-1"); if ("status".equalsIgnoreCase(request.getParameter("status"))) { status(response); } else { totalSize = request.getContentLength(); try { upload(request); } catch (FileUploadException e) { e.printStackTrace(); } } } private void upload(HttpServletRequest request) throws FileUploadException, IOException { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(request); for (Iterator it = items.iterator(); it.hasNext();) { FileItem fileItem = (FileItem) it.next(); if (!fileItem.isFormField()) { String fileName = fileItem.getName().substring( fileItem.getName().lastIndexOf("//") + 1); System.out.println(fileName); File file = new File("C:/", fileName); InputStream in = fileItem.getInputStream(); FileOutputStream out = new FileOutputStream(file); byte[] buffer = new byte[2048]; int n; while ((n = in.read(buffer)) != -1) { out.write(buffer, 0, n); completedSize += (long) n; } fileItem.delete(); } } totalSize = completedSize = 0L; } private void status(HttpServletResponse response) throws IOException { long lastSeconds = (System.currentTimeMillis() - startTime) / 1000; double percentd = (double) (completedSize * 100) / (double) (totalSize + 0.0001); int percent = (int) (percentd); int lastpercent = 0; int uploadSpeed = (int) (completedSize / (lastSeconds + 0.0001)); System.out.print("lastSeconds:" + lastSeconds + "/tuploadSpeed:" + uploadSpeed + "/tpercent:" + percent); System.out.println("/tcompletedSize:" + completedSize + "/ttotalSize:" + totalSize); if(lastpercent==99&&percent==0) percent=100; //System.out.println("persent is " + percent); //System.out.println("lastpersent is " + lastpercent ); lastpercent = percent; response.getWriter().print(percent); } public void init() throws ServletException { totalSize = 0L; completedSize = 0L; startTime = System.currentTimeMillis(); } private long totalSize = 0L; private long completedSize = 0L; private long startTime = 0L; }