servlet+jquery实现文件上传进度条示例代码(2)

package com.fei.util; public class FileUploadInfo { private final int K = 1024; private final int M = K * 1024; /** * 总大小 */ private long totalSize; /** * 开始上传时间 */ private long startTime = System.currentTimeMillis(); /** * 已上传多少 */ private long uploadSize; /** * 上传速度(K/S) */ public double getUploadSpeed_K(){ long currentTime = System.currentTimeMillis(); long usedTime = currentTime - startTime; if(usedTime == 0.0){ return 0.0; } return getUploadSize_K()/usedTime*1000d; } /** * 获取已上传百分比 * @return */ public double getUploadPercent(){ return (getUploadSize()*1.00/getTotalSize())*100d; } /** * 剩余时间(s) * @return */ public double getRemainTime(){ double speedKB = getUploadSpeed_K(); if(speedKB<= 0.00){ return -1d; } return (getTotalSize_K() - getUploadSize_K())/speedKB; } /** * 已上传时间 * @return */ public double getUseTime(){ return (System.currentTimeMillis() - startTime)/1000d; } public long getTotalSize() { return totalSize; } public double getTotalSize_K(){ return getTotalSize()*1.0/K; } public double getTotalSize_M(){ return getTotalSize()*1.0/M; } public long getUploadSize() { return uploadSize; } public double getUploadSize_K(){ return getUploadSize()/K; } public double getUploadSize_M(){ return getUploadSize()/M; } public void setTotalSize(long totalSize) { this.totalSize = totalSize; } public void setUploadSize(long uploadSize) { this.uploadSize = uploadSize; } private String double2String(double d){ return String.format("%.2f", d); } public String toString(){ return "{"+ "'totalSize':'"+double2String(getTotalSize_M())+"M',"+ "'uploadSize':'"+double2String(getUploadSize_M())+"M',"+ "'uploadSpeed':'"+double2String(getUploadSpeed_K())+"KB/s',"+ "'uploadPrecent':'"+double2String(getUploadPercent())+"',"+ "'remainTime':'"+(getRemainTime()<0?"未知":double2String(getRemainTime()))+"s"+"',"+ "'useTime':'"+double2String(getUseTime())+"s' "+ "}"; } }

读取文件上传进度的servlet

UploadFileProgressServlet.java

package com.fei.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.fei.util.FileUploadInfo; public class UploadFileProgressServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String responseContent = ""; Object obj = request.getSession().getAttribute("uploadInfo"); if(obj == null){ responseContent = "{'data':'NoData'}"; }else{ FileUploadInfo uploadInfo = (FileUploadInfo)obj; responseContent = uploadInfo.toString(); if(uploadInfo.getUploadPercent()== 100.0){ request.getSession().setAttribute("uploadInfo", null); } } System.out.println("文件上次情况:"+responseContent); response.getWriter().print(responseContent); } }

前台页面upload2.html

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script src="https://www.jb51.net/js/jquery-2.1.1.js" type="text/javascript"></script> <script > var falseNum = 0; $(document).ready(function(){ $('#subbut').bind('click', function(){ $('#fForm').submit(); var eventFun = function(){ $.ajax({ type: 'GET', url: '/uploadtest/UploadFileProgressServlet.do', data: {}, dataType: 'text', success : function(resData){ //如果10次都失败,那就不要继续访问了 if(falseNum > 10){ window.clearInterval(intId); return; } var obj = eval("("+resData+")"); if(obj.data == 'NoData'){ falseNum ++ ; return; } $("#graphbox").css('display','block'); $(".progressbar").css('width',obj.uploadPrecent+'%'); $("#uploadPer").html(obj.uploadPrecent+'%'); $("#progressInfo").html('总大小:'+obj.totalSize +',速度:'+obj.uploadSpeed +'%,剩余时间:'+obj.remainTime+',已用时间:'+obj.useTime); if(obj.uploadPrecent == 100){ window.clearInterval(intId); $("#progressInfo").html("上传成功!"); } }});}; var intId = window.setInterval(eventFun,500); }); }); </script> <style> #graphbox{ border:1px solid #e7e7e7; padding:5px; width:350px; background-color:#f8f8f8; margin:5px 0; display:none; } .graph{ position:relative; background-color:#F0EFEF; border:1px solid #cccccc; padding:2px; font-size:10px; font-weight:500; } .graph .progressbar{ position:relative; text-align:right; color:#ffffff; height:10px; line-height:10px; font-family:Arial; display:block; } .graph .progressbar{background-color:#66CC33;} </style> </head> <body> <form action="/uploadtest/UploadFileServlet.do" encType="multipart/form-data" target="uploadf" method="post"> <div> <label>上传文件:</label> <div> <input type="file"></br> <input type="file"></br> <button type="button">上传</button> </div> <div> <div> <span><span></span></span> </div> </div> <div> </div> </div> </form> <iframe></iframe> </body> </html>

将项目部署到tomcat(或其他web容器),访问:8080/uploadtest/upload2.html

效果:

servlet+jquery实现文件上传进度条示例代码


servlet+jquery实现文件上传进度条示例代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwgpfy.html