日期:2014-05-17 浏览次数:20731 次
//这里面就是解析上传文件的核心代码
private List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
DiskFileItemFactory fac = createDiskFileItemFactory(saveDir);
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setSizeMax(this.maxSize);
/*注册上传进度监听器这里面你就可以记录进度到session中,你前台再用setInterval到另一个action中取session的进度就可以了*/
upload.setProgressListener(new ProgressListener(){
/*第一个参数代表当前已读了多少字节,第二个参数代表整个表单的长度也可以当做文件的总大小,第三个参数是代表已解析到第几个(这里对你不重要)*/
public void update(long readBytes, long contentLength, int items){
HttpSession session=ServletActionContext.getRequest().getSession();
session.setAttribute("currentProgress",readBytes);
session.setAttribute("contentLength",contentLength);
}
});
return upload.parseRequest(createRequestContext(servletRequest));
}