如果上传大文件,那么上传进度的显示就比较重要,那么如何实现进度条的显示呢?
http://commons.apache.org/fileupload/
它的UserGuide里http://commons.apache.org/fileupload/using.html有详细的使用的例子和说明。
我的实现思路如下:
//
DiskFileItemFactoryfactory = new DiskFileItemFactory();
factory.setSizeThreshold(2048);
StringtempPath = AppConstants.ROOTPATH + "upload/tmp/";
File fp1 =new File(tempPath);
if(!fp1.exists())
fp1.mkdirs();
File tempFile = newFile(tempPath);
factory.setRepository(newFile(tempPath));
ServletFileUploadupload = new ServletFileUpload(factory);
upload.setSizeMax(1024000000);
ProgressListenerprogressListener = newUploadProgress();
request.getSession().setAttribute("progress",progressListener);
upload.setProgressListener(progressListener);
List items = upload.parseRequest(request);
Stringcomment = ""; //存储评论
Iterator iter= items.iterator();
while(iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if(name.equals("pid"))
{
pid = value;
}
else
comment = value;
} else {
String fileName = item.getName();
if(fileName == null || fileName.equals(""))
continue;
fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
String ext = fileName.substring(fileName.lastIndexOf("."));
ext=ext.toLowerCase();
long curTime = System.currentTimeMillis();
Randomrandom = new Random(100000000);
Stringfilename = String.valueOf(curTime) + "_"
+random.nextInt(100000000) + ext;
StringfilePath = AppConstants.ROOTPATH +"/uploadfile/"+loguser.getId()+"/files/"+pid+"/";
Stringpath = "/uploadfile/"+loguser.getId()+"/files/"+pid+"/";
Filefp = new File(filePath);
if(!fp.exists())
fp.mkdirs();
File uploadedFile = new File(fp,filename);
item.write(uploadedFile);
如上源代码,upload.setSizeMax(1024000000);
upload.setProgressListener(progressListener);
我的progressListner定义如下:
public class UploadProgress implements ProgressListener {
private double upRate = 0.0;
private double percent = 0.0;
private long useTime = 0;
private long upSize = 0;
private long allSize = 0;
private int item;
private long beginT =System.currentTimeMillis();
private long curT =System.currentTimeMillis();
public void update(long pBytesRead, longpContentLength, int pItems) {
curT =System.currentTimeMillis();
item = pItems;
allSize = pContentLength; //byte
upSize = pBytesRead; //byte
useTime = curT-beginT; //ms
if(useTime != 0)
upRate =pBytesRead/useTime; //byte/ms
else
upRate =0.0;
if(pContentLength == 0)
return;
percent =(double)pBytesRead/(double)pContentLength; //百分比
}
public long getAllSize() {
return allSize;
}
public void setAllSize(long allSize) {
this.allSize = allSize;
}
public long getBeginT() {
return beginT;
}
public void setBeginT(long beginT) {
this.beginT = beginT;
}
public long getCurT() {
return curT;
}
public void setCurT(long curT) {
this.curT = curT;
}
public int getItem() {
return item;
}
public void setItem(int item) {
this.item = item;
}
public double getPercent() {
return percent;
}
public void setPercent(double percent) {
this.percent = percent;
}
public double getUpRate() {
return upRate;
}
public void setUpRate(double upRate) {
this.upRate = upRate;
}
public long getUpSize() {
return upSize;
}
public void setUpSize(long upSize) {
this.upSize = upSize;
}
public long getUseTime() {
return useTime;
}
public void setUseTime(long useTime) {
this.useTime = useTime;
}
}
这个监听类要继承ProgressListener 并实现update函数。
而如何实现进度的显示呢,可以通过在页面上用javascript写一个定时器,setInterval("showRequest();",1000);通过这个定时器,不断去请求后台,获取进度,进行显示,而获取进度处理函数如何获取文件上传的进度呢,可以通过request.getSession().setAttribute("progress",progressListener);