如果上传大文件,那么上传进度的显示就比较重要,那么如何实现进度条的显示呢?
显示文件上传进度,可以使用apache下的FileUpload 组件,它的下载地址是:
http://commons.apache.org/fileupload/
它的User Guide里http://commons.apache.org/fileupload/using.html有详细的使用的例子和说明。
我的实现思路如下:
// 准备上传
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(2048);
String tempPath = AppConstants.ROOTPATH + "upload/tmp/";
File fp1 = new File(tempPath);
if(!fp1.exists())
fp1.mkdirs();
File tempFile = new File(tempPath);
factory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1024000000);
ProgressListener progressListener = new UploadProgress();
request.getSession().setAttribute("progress", progressListener);
upload.setProgressListener(progressListener);
List items = upload.parseRequest(request);
String comment = ""; //存储评论
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();
Random random = new Random(100000000);
String filename = String.valueOf(curTime) + "_"
+ random.nextInt(100000000) + ext;
String filePath = AppConstants.ROOTPATH + "/uploadfile/"+loguser.getId()+"/files/"+pid+"/";
String path = "/uploadfile/"+loguser.getId()+"/files/"+pid+"/";
File fp = new File(filePath);
if(!fp.exists())
fp.mkdirs();
File uploadedFile = new File(fp, filename);
item.write(uploadedFile);
如上源代码,upload.setSizeMax(1024000000); 可以设置文件上传最大支持的大小, factory.setRepository(new File(tempPath));设置这个可以是上传大文件的时候,不是把上传的所有数据一直存入内存,而是当达到factory.setSizeThreshold(2048);所设置的文件大小后,就存入临时文件,利用这个特性可以使上传大文件的时候不会占用大量内存,可以提供用户的并发使用量。
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, long pContentLength, 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); 把监听类存入session,然后在获取进度的action中获取上传的进度。