
地 址:上海市松江66号
电 话:19957237440
网址:lcjs666.com
邮 箱:79217309@qq.com
本文实例实现文件上(shang)传的何实(shi)进度显示,我们先看看都有哪些问题我们要解决。现动
1 上传数据的态显处理进(jin)度跟踪

2 进度数据在(zai)用(yong)户页面的显示

就这么2个问(wen)题,

第一个问题,示文主要是传进组件的选择
必须支持数(shu)据处理侦听或通(tong)知的组件。当(dang)然,度条(tiao)我肯定只用我自己的何实组件(jian)啦。基本原(yuan)理是现动
1 使用request.getContentLength() 读取(qu)到处理数据的总(zong)长度,注意这个长度不等于文件的态显长度,因为(wei)Base64等编码会(hui)增加数据量,示文如果(guo)超过了允许的(de)长度,直接返回(hui)-1;
2 在每读取(qu)一部分数据(ju)时(比如一行,传进或(huo)者64K,度条(tiao)或者你自定义的何实字节数),将读取的现动字节数通知我们的进度跟踪程序。我取名为 UploadListener代(dai)码如下
/*
* 处理附件上传(chuan)的(de)态显通知(zhi)。
* 各位可以(yi)继承这个类,来实现自己的特殊处理。
*
* @author 赵学庆 www.java2000.net
*/
public class UploadListener ... {
// 调(diao)试模式将在控制台打印出一些数据
private boolean debug;
// 总数据(ju)字节数
private int total;
// 当前已经(jing)处(chu)理的数据字节数(shu)
private int totalCurrent = 0 ;
// 延迟,用来调试用,免得速度太快,根本卡看不到进度
private int delay = 0 ;
/** */ /**
* 处理数(shu)据通知的(de)方法。
* 保(bao)存已经处理(li)的数据(ju)。并且在一定的比例进行延迟。默认每1%
* 如果不需用延迟,可以删掉内部的代码,加快速度。
*
* @param size 增(zeng)加的字节数
*/
public void increaseTotalCurrent( long size) ... {
this .totalCurrent += size;
try ... {
currentRate = totalCurrent * 100 / total;
if (currentRate > lastRate) ... {
if (delay > 0 ) ... {
Thread.sleep(delay);
}
if (debug) ... {
System.out.println( " rate= " + totalCurrent + " / " + total + " / " + (totalCurrent * 100 / total));
}
lastRate = currentRate;
}
} catch (Exception e) ... {
e.printStackTrace();
}
}
/** */ /**
* 读取全部自己数(shu)
*
* @return
*/
public int getTotal() ... {
return total;
}
/** */ /**
* 读取(qu)已经处理的字节数
*
* @return
*/
public int getTotalCurrent() ... {
return totalCurrent;
}
private long lastRate = 0 ;
private long currentRate = 0 ;
public int getDelay() ... {
return delay;
}
public void setDelay( int delay) ... {
this .delay = delay;
}
public void setTotal( int total) ... {
this .total = total;
}
public boolean isDebug() ... {
return debug;
}
public void setDebug( boolean debug) ... {
this .debug = debug;
}
}
你学会了吗?