关于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 startTime;
$(function(){
$('#subButton').click(function(){
var myDate = new Date();
startTime = myDate.getTime();
$(this).attr("disabled", true);
$("#uploadForm").submit();
$("#progress").show();
window.setTimeout("getProgressBar()", 1000);
});
$("#close").click(function(){$("#progress").hide();});
});
function getProgressBar(){
var timestamp = (new Date()).valueOf();
var bytesReadToShow = 0;
var contentLengthToShow = 0;
var bytesReadGtMB = 0;
var contentLengthGtMB = 0;
$.ajax({
'url':'media/progressSevletAction',
'async':false,
'cache':false,
'type':'post',
'dataType':'json',
'success':function(status){
var bytesRead=(status.pBytesRead/1024).toString();
alert(bytesRead)
if(bytesRead>1024){
bytesReadToShow=(bytesRead/1024).toString();
bytesReadGtMB=1;
}else{
bytesReadToShow=bytesRead.toString();
}
var contentLength=(status.pContentLength/1024);
if(contentLength>1024){
contentLengthToShow=(contentLength/1024).toString();
contentLengthGtMB=1;
}else{
contentLengthToShow=(contentLength/1024).toString();
}
$('#test').html(contentLengthToShow);
$('#test1').html(bytesReadToShow);
}
});
var interval = window.setTimeout("getProgressBar()", 500);
}
这个是HTML代码:
<body>
<iframe id='target_upload' name='target_upload' src=''
style='display: none'></iframe>
<form action="upload/uploadMedia" enctype="multipart/form-data" method="post" id="uploadForm" target="arget_upload">
标题:<input type="text" name="title"/><br/>
描述:<input type="text" name="descript"><br/>
<span id="files">
<input type="file" name="upload"><br/>
</span>
<input type="button" id="subButton" value="提交" /><br/>
<input type="button" value="增加上传文件" onclick="addCompent();" />
</form>
<div id="progress">
<div id="title">
<span id="text">上传进度</span>
<div id="close">X</div>
</div>
<div id="progressBar">
<div id="uploaded"></div>
</div>
<div id="info"></div>
</div>
<span id="test"></span>
<span id="test1"></span>
</body>
下面就是我定时发送到我需要返回数据时的ACTION关键就在这里,他返回的总是已经上传完了后的数据,而不是边上传边返回,很郁闷
public class ProgressSevletAction extends BasicAction {
private FileUploadStatus status;
private boolean ok;
public String execute(){
status=(FileUploadStatus) session.get("status");
if(status==null){
status=new FileUploadStatus();
System.out.println("in null");
status.setpItems(0);
return null;
}else{
System.out.println("ss"+status.getpBytesRead());
System.out.println("yy"+status.getpContentLength());
return "success";
}
}
public FileUploadStatus getStatus() {
return status;
}
public void setStatus(FileUploadStatus status) {
this.status = status;
}
public boolean isOk() {
return ok;
}
public void setOk(boolean ok) {
this.ok = ok;
}
}
这个是配置的XML的ACTION:
<struts>
<package name="qyty-mediabackstage" extends="json-default" namespace="/media">
<action name="mediaAction_*" class="mediaAction" method="{1}">
<result name="findOk">mediaList.jsp</result>
</action>
<action name="viewAction" class="viewAction">
<result name="success">view.jsp</result>
</action>
<action name="progressSevletAction" class="progressSevletAction" >
<result name="success" type="json">
<param name="root">
status
</param>
</result>
</action>
</package>
</struts>
下面这个就是正常上传ACTION:
public class UploadMedia extends BasicAction {
private String title;
private String descript;
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
public String execute(){
try{
System.out.println(title);
System.out.println(descript);
for(int i=0;i<upload.size();i++){
System.out.println(upload.get(i).getAbsolutePath());
System.out.println(upload.get(i).getName());
System.out.println(uploadContentType.get(i));
System.out.println(uploadFileName.get(i).substring(uploadFileName.get(i).lastIndexOf(".")+1));
}
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescript() {
return descript;
}
public void setDescript(String descript) {
this.descript = descript;
}
public List<File> getUpload() {
return upload;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public List<String> getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public List<String> getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
各位大神很急啊 帮小弟看看,为什么AJAX返回时的数据是已经文件上传完了后返回的最后数据,
------解决方案--------------------
function getProgressBar(){
var timestamp = (new Date()).valueOf();
var bytesReadToShow = 0;
var contentLengthToShow = 0;
var bytesReadGtMB = 0;
var contentLengthGtMB = 0;
$.ajax({
'url':'media/progressSevletAction',
'async':true,// 异步改成true
'cache':false,
'type':'post',
'dataType':'json',
'success':function(status){
var bytesRead=(status.pBytesRead/1024).toString();
alert(bytesRead)
if(bytesRead>1024){
bytesReadToShow=(bytesRead/1024).toString();
bytesReadGtMB=1;
}else{
bytesReadToShow=bytesRead.toString();
}
var contentLength=(status.pContentLength/1024);
if(contentLength>1024){
contentLengthToShow=(contentLength/1024).toString();
contentLengthGtMB=1;
}else{
contentLengthToShow=(contentLength/1024).toString();
}
$('#test').html(contentLengthToShow);
$('#test1').html(bytesReadToShow);
}
});