|
<script type=text/javascript></script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> </script> <iframe name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-1832179689702023&dt=1179588917140&lmt=1179546942&format=336x280_as&output=html&correlator=1179588917140&channel=7111701428&url=http%3A%2F%2Fwww.java3z.com%2Fcwbwebhome%2Farticle%2Farticle5%2F5779.html%3Fid%3D1379&color_bg=FFFFFF&color_text=000000&color_link=0000FF&color_url=3D81EE&color_border=FFFFFF&ad_type=text_image&ref=http%3A%2F%2Fwww.java3z.com%2Fcwbwebhome%2Ftype.jsp%3Ftype%3Dajax&cc=99&flash=9&u_h=768&u_w=1024&u_ah=738&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameBorder=0 width=336 scrolling=no height=280 allowTransparency></iframe> |
1.引言基于浏览器的文件上传,特别是对于通过<input type="file">标签来实现上传的情况, 存在着严重的性能问题,因为用户提交了文件之后,在浏览器把文件上传到服务器的过程中,界面看上去似乎是静止的,如果是小文件还好些,如果不幸需要上传的是几兆、几十兆甚至上百兆的文件,我相信那是一种非常痛苦的体验,我们中间的很多人应该都有过此种不堪的经历。(一笑) 现在我就针对这个问题给出一个解决方案,我们将实现一个具有监控能力的WEB上传的程序——它不仅把文件上传到服务器,而且"实时地"监视文件上传的实际过程。 解决方案的基本思路是这样的:
在介绍源代码之前,我们先来看看程序运行界面: 2.实现代码实现代码想当然的有服务器端代码和客户端代码(呵呵),我们先从服务器端开始。 2.1.服务器端代码2.1.1.文件上传状态类(FileUploadStatus)使用FileUploadStatus这个类记录文件上传状态,并将其作为服务器端与web客户端之间通信的媒介,通过对这个类对象提供上传状态作为服务器回应发送给web客户端, web客户端使用JavaScript获得文件上传状态。源代码如下: /** * 本例程演示了通过Web上传文件过程中的进度显示。您可以对本例程进行任何修改和使用。 * 如果需要转载本例程,请您注明作者。 * * 作者: 刘作晨 * EMail:liuzuochen@gmail.com */ package liuzuochen.sample.upload; import java.util.*; public class FileUploadStatus { //上传用户地址 private String uploadAddr; //上传总量 private long uploadTotalSize = 0; //读取上传总量 private long readTotalSize = 0; //当前上传文件号 private int currentUploadFileNum = 0; //成功读取上传文件数 private int successUploadFileCount = 0; //状态 private String status = ""; //处理起始时间 private long processStartTime = 0l; //处理终止时间 private long processEndTime = 0l; //处理执行时间 private long processRunningTime = 0l; //上传文件URL列表 private List uploadFileUrlList = new ArrayList(); //取消上传 private boolean cancel = false; //上传base目录 private String baseDir = ""; public FileUploadStatus() { } public String getBaseDir() { return baseDir; } public void setBaseDir(String baseDir) { this.baseDir = baseDir; } public boolean getCancel() { return cancel; } public void setCancel(boolean cancel) { this.cancel = cancel; } public List getUploadFileUrlList() { return uploadFileUrlList; } public void setUploadFileUrlList(List uploadFileUrlList) { this.uploadFileUrlList = uploadFileUrlList; } public long getProcessRunningTime() { return processRunningTime; } public void setProcessRunningTime(long processRunningTime) { this.processRunningTime = processRunningTime; } public long getProcessEndTime() { return processEndTime; } public void setProcessEndTime(long processEndTime) { this.processEndTime = processEndTime; } public long getProcessStartTime() { return processStartTime; } public void setProcessStartTime(long processStartTime) { this.processStartTime = processStartTime; } public long getReadTotalSize() { return readTotalSize; } public void setReadTotalSize(long readTotalSize) { this.readTotalSize = readTotalSize; } public int getSuccessUploadFileCount() { return successUploadFileCount; } public void setSuccessUploadFileCount(int successUploadFileCount) { this.successUploadFileCount = successUploadFileCount; } public int getCurrentUploadFileNum() { return currentUploadFileNum; } public void setCurrentUploadFileNum(int currentUploadFileNum) { this.currentUploadFileNum = currentUploadFileNum; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public long getUploadTotalSize() { return uploadTotalSize; } public String getUploadAddr() { return uploadAddr; } public void setUploadTotalSize(long uploadTotalSize) { this.uploadTotalSize = uploadTotalSize; } public void setUploadAddr(String uploadAddr) { this.uploadAddr = uploadAddr; } public String toJSon() { StringBuffer strJSon = new StringBuffer(); strJSon.append("{UploadTotalSize:").append(getUploadTotalSize()).append( ",") .append("ReadTotalSize:").append(getReadTotalSize()).append(",") .append("CurrentUploadFileNum:").append(getCurrentUploadFileNum()). append(",") .append("SuccessUploadFileCount:").append( getSuccessUploadFileCount()).append(",") .append("Status:'").append(getStatus()).append("',") .append("ProcessStartTime:").append(getProcessStartTime()). append(",") .append("ProcessEndTime:").append(getProcessEndTime()).append( ",") .append("ProcessRunningTime:").append(getProcessRunningTime()). append(",") .append("Cancel:").append(getCancel()).append("}"); return strJSon.toString(); } } 2.1.2.文件上传状态侦听类(FileUploadListener)使用Common-FileUpload 1.2版本(20070103)。此版本提供了能够监视文件上传情况的ProcessListener接口,使开发者通过FileUploadBase类对象的setProcessListener方法植入自己的Listener。 FileUploadListener类实现了ProcessListener,在整个文件上传过程中,它对上传进度进行监控,并且根据上传 情况实时的更新上传状态Bean。源代码如下: /** * 本例程演示了通过Web上传文件过程中的进度显示。您可以对本例程进行任何修改和使用。 * 如果需要转载本例程,请您注明作者。 * * 作者: 刘作晨 * EMail:liuzuochen@gmail.com */ package liuzuochen.sample.upload; import org.apache.commons.fileupload.ProgressListener; import javax.servlet.http.HttpServletRequest; public class FileUploadListener implements ProgressListener{ private HttpServletRequest request=null; public FileUploadListener(HttpServletRequest request){ this.request=request; } /** * 更新状态 */ public void update(long pBytesRead, long pContentLength, int pItems){ FileUploadStatus statusBean= BackGroundService.getStatusBean(request); statusBean.setUploadTotalSize(pContentLength); //读取完成 if (pContentLength == -1) { statusBean.setStatus("完成对" + pItems +"个文件的读取:读取了 " + pBytesRead + " bytes."); statusBean.setReadTotalSize(pBytesRead); statusBean.setSuccessUploadFileCount(pItems); statusBean.setProcessEndTime(System.currentTimeMillis()); statusBean.setProcessRunningTime(statusBean.getProcessEndTime()); //读取中 } else { statusBean.setStatus("当前正在处理第" + pItems +"个文件:已经读取了 " + pBytesRead + |
AJAX和JSP实现的基于WEB的文件上传的进度控制代码第1/2页
AJAX