关于struts2上传文件进度条问题
今天用strtuts2文件上传进度条的问题,基本按照网上别人说的做了一次,但是现在问题出在监听确实进去了,但是我要没隔一断时间通过ajax返回我想要的数据,可是总是等文件已经上传完了才返回,而不是我想要的那样,下面附上代码:
XML上传配置文件,我在struts-xml里引用它了
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 指定国际化资源文件的baseName为uploadFile-->
<constant name="struts.custom.i18n.resources" value="uploadFile"></constant>
<!-- 设置该应用使用的编码集为utf-8 -->
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<!-- 设置文件上传大小 -->
<constant name="struts.multipart.maxSize" value="1000000000" />
<package name="qyty-upload" extends="json-default" namespace="/upload">
<action name="uploadMedia" class="uploadMedia">
<result name="success">/index.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">80000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
<bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest" name="requestParser" class="com.qyty.util.MyMultiPartRequest" scope="default" optional="true"></bean>
<constant name="struts.multipart.handler" value="requestParser" />
</struts>
这个是自己写的监听器:
FileUploadStatus 这个类就是个实体类,需要返回的数据。
public class MyListener implements ProgressListener{
private HttpSession session;
public MyListener(HttpServletRequest request){
System.out.println("sdsad");
session=request.getSession();
FileUploadStatus status=new FileUploadStatus();
session.setAttribute("status", status);
}
public void update(long pBytesRead, long pContentLength, int pItems) {
// TODO Auto-generated method stub
FileUploadStatus status=(FileUploadStatus) session.getAttribute("status");
System.out.println("updat"+pBytesRead);
status.setpBytesRead(pBytesRead);
status.setpContentLength(pContentLength);
status.setpItems(pItems);
System.out.println(pBytesRead);
}
}
这个是继承MultiPartRequest添加自己的监听程序
private List<FileItem> parseRequest(HttpServletRequest servletRequest, String saveDir) throws FileUploadException {
DiskFileItemFactory fac = createDiskFileItemFactory(saveDir);
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setSizeMax(maxSize);
/*自己新建监听器*/
MyListener progressListener = new MyListener(servletRequest);
upload.setProgressListener(progressListener);//添加自己的监听器
return upload.parseRequest(createRequestContext(servletRequest));
}
这个是页面JS代码:
var star