1 2 3 4 | DiskFileItemFactory factory = new DiskFileItemFactory(); PortletFileUpload pfu = new PortletFileUpload(factory); pfu.setSizeMax(uploadMaxSize); // Maximum upload size pfu.setProgressListener(new FileUploadProgressListener()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package uk.ac.dl.esc.gtg.myportlets.fileupload; import java.text.NumberFormat; import org.apache.commons.fileupload.ProgressListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class FileUploadProgressListener implements ProgressListener { private static Log log = LogFactory.getLog(FileUploadProgressListener.class); private static long bytesTransferred = 0; private static long fileSize = -100; private long tenKBRead = -1; public FileUploadProgressListener() { } public String getFileUploadStatus() { // per looks like 0% - 100%, remove % before submission String per = NumberFormat.getPercentInstance().format( (double) bytesTransferred / (double) fileSize); return per.substring(0, per.length() - 1); } public void update(long bytesRead, long contentLength, int items) { // update bytesTransferred and fileSize (if required) every 10 KB is // read long tenKB = bytesRead / 10240; if (tenKBRead == tenKB) return; tenKBRead = tenKB; bytesTransferred = bytesRead; if (fileSize != contentLength) fileSize = contentLength; } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |