日期:2014-05-18 浏览次数:20718 次
private List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
DiskFileItemFactory fac = createDiskFileItemFactory(saveDir);
ServletFileUpload upload = new ServletFileUpload(fac);
//添加监听器
upload.setProgressListener(new ProgressListener() {
@Override
public void update(long pBytesRead, long pContentLength, int pItems) {
int percent = (int) (((float)pBytesRead / (float)pContentLength) * 100);
ActionContext.getContext().getSession().put("process", percent + "%");//将进度百分比放到session中。
System.out.println("--------"+percent);//输出一下上传的进度
}
});
upload.setSizeMax(maxSize);
return upload.parseRequest(createRequestContext(servletRequest));
}
<!--ProgressListener -->
<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="requestParser"
class="com.gycenter.listener.MyMultiPartRequest" scope="default"/>
<constant name="struts.multipart.handler" value="requestParser" />
<!-- ProgressListener end -->
public void uploadProgress(){
try {
String process = (String) ActionContext.getContext().getSession().get("process");
System.out.println("文件已经上传"+process);