上传进度 - ProgressListener
来源:互联网 发布:mac office 彻底关闭 编辑:程序博客网 时间:2024/04/30 04:33
如果上传大文件,那么上传进度的显示就比较重要,那么如何实现进度条的显示呢?
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);
- 上传进度 - ProgressListener
- springMVC + ProgressListener + CommonsMultipartResolver 上传进度查询
- PHP 显示上传进度
- fileupload上传显示进度
- servlet上传文件进度
- php上传带进度
- AFNetworking 上传进度注意事项
- AFNetwortking 上传进度
- 获取文件上传进度
- angularjs 上传进度获取
- 文件上传进度
- 用flash实现上传进度
- PHP APC监测上传进度
- android 文件上传 进度框
- PHP 获取文件上传进度
- HTML5上传文件显示进度
- OkHttp实现文件上传进度
- 文件上传及进度显示
- 文件操作函数(1)
- 开发记录————spring mvc 获取页面数据
- 几种常见的DVD光盘类型
- linux系统编程之基础必备(二):C 标准IO 库函数与Unbuffered IO函数
- RecyclerView里notifyItemRemoved的坑
- 上传进度 - ProgressListener
- 计算机视觉的一些牛人博客
- JQuery EasyUI combobox动态添加option
- 磁盘
- openwrt和srun的两三事
- poj2718(子集生成)
- 【技巧】数组作为另一个数组的下标
- 实验记录:Oracle redo logfile的resize过程
- Ubuntu PPA用法简介