主要是通过UploadListener 监听服务端接收了多少字节,返回给客户端,由javascript,div,css来操作进度条的显示
public class UploadListener implements OutputStreamListener
{
??? private HttpServletRequest request;
??? private long delay = 0;
??? private long startTime = 0;
??? private int totalToRead = 0;
??? private int totalBytesRead = 0;
??? private int totalFiles = -1;
??? public UploadListener(HttpServletRequest request, long debugDelay)
??? {
??????? this.request = request;
??????? this.delay = debugDelay;
??????? totalToRead = request.getContentLength();
??????? this.startTime = System.currentTimeMillis();
??? }
??? public void start()
??? {
??????? totalFiles ++;
??????? updateUploadInfo("start");
??? }
??? public void bytesRead(int bytesRead)
??? {
??????? totalBytesRead = totalBytesRead + bytesRead;
??????? updateUploadInfo("progress");
??????? try
??????? {
??????????? Thread.sleep(delay);
??????? }
??????? catch (InterruptedException e)
??????? {
??????????? e.printStackTrace();
??????? }
??? }
??? public void error(String message)
??? {
??????? updateUploadInfo("error");
??? }
??? public void done()
??? {
??????? updateUploadInfo("done");
??? }
??? private long getDelta()
??? {
??????? return (System.currentTimeMillis() - startTime) / 1000;
??? }
??? private void updateUploadInfo(String status)
??? {
??????? long delta = (System.currentTimeMillis() - startTime) / 1000;
??????? request.getSession().setAttribute("uploadInfo", new UploadInfo(totalFiles, totalToRead, totalBytesRead,delta,status));
??? }
}
上面的代码中,原作者为了演示本机操作上的显示效果,特别加入了debugDelay做延时处理,在实际应用中可以删除。
upload.jsp:
<%@ page import="be.telio.mediastore.ui.upload.MonitoredDiskFileItemFactory" %>
<%@ page import="be.telio.mediastore.ui.upload.UploadListener" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.FileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.FileUploadException" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--
/* Licence:
*?? Use this however/wherever you like, just don't blame me if it breaks anything.
*
* Credit:
*?? If you're nice, you'll leave this bit:
*
*?? Class by Pierre-Alexandre Losson -- http://www.telio.be/blog
*?? email : plosson@users.sourceforge.net
*/
--%>
<%
??? UploadListener listener = new UploadListener(request, 30);
??? // Create a factory for disk-based file items
??? FileItemFactory factory = new MonitoredDiskFileItemFactory(listener);
??? // Create a new file upload handler
??? ServletFileUpload upload = new ServletFileUpload(factory);
??? try
??? {
??????? // process uploads ..
??????? upload.parseRequest(request);
//....处理上传文件
??? }
??? catch (FileUploadException e)
??? {
??????? e.printStackTrace();? //To change body of catch statement use File | Settings | File Templates.
??? }
%>
详细解决方案
ajax下传文件进度条实现
热度:208 发布时间:2012-10-30 16:13:36.0
相关解决方案
- (Struts2+JSON+Ajax) XMLHttpRequest ==500如何解决
- 。各位大神,小弟有个简单的有关问题,就是小弟我用struts+ajax+juery+json时为何总是不进入action啊 配置文件如上
- ajax 多值 jsp 急
- ajax responsetext 替空值,想不通。
- springmvc jquery ajax 提交复杂对象,415异常解决办法
- jquery ajax select解决方法
- Ajax 更新商品列表下一页,页面不在脑袋开始
- CAPTCHA 模块中 ajax form 导致CAPTCHA session reuse attack detected异常,解决方法
- ajax Post 一直返回的是error ,为啥
- struts2 ajax 上拉框联动有关问题
- AJAX 学习过程中的有关问题
- servlet+jquery1.4.2(ajax error?)解决方案
- jQuery.ajax()请求struts2数据 回到data值了undefined
- 扭结,AJAX WEB UI库选型
- 求大神解决偏题 - jquery ajax
- AJAX 传递数据如何样才可以是 json
- ajax 调用很慢解决办法
- ajax 拓扑图 实现,该如何解决
- struts2+jquery+ajax 回调函数没反应啊请们帮忙看看啊 跪谢
- ajax 怎么实现文件下载
- 使用$.ajax()异步无刷新请求servlet无反应解决方法
- struts2+ajax+oracle实现目录树,该怎么解决
- jquery- ui ajax 下拉列表框解决思路
- JSON,AJAX
- 求个php+ajax+easyUI datagrid的简略完整的demo
- 验证用户名是否存在。Ajax+Action如何写了
- Struts Ajax 有关问题,求教
- $.ajax({})如何返回一个boolean,答案,多谢大神
- :前台和后台开发有啥区别?还有css、html、ajax、js、jquery都有什么区别
- jquery ajax+json(插件) 提交数据,让人捉摸不透的是:【部分中文显示问号】,该怎么解决