求教,文件上传进度条问题

zfapple 2013-11-05 10:16:50
我使用的apache commons,但是上传后,前面一直阻塞,进度条不动,一直到最后上传完,进度条跳到100%。请各位大侠帮忙看看,谢谢
jsp部分:
<script>
var timeid;
function callback(){
//alert(123);
$.ajax({
type:"post",
url:"progress",//响应文件上传进度的servlet
success:function(msg){
//alert(msg);
value = msg;
if(value<100){
document.getElementById("xxx").innerHTML="已上传:"+msg;//显示读取百分比
}
else if(value==100){
window.clearInterval(timeid);
document.getElementById("xxx").innerHTML="已上传:"+msg;//显示读取百分比
}
}
});
}
function submitForm(){
timeid = window.setInterval("callback()", 10);
$('#ff').form('submit');
}
</script>
<form id="ff" method="post" action="UpFile" enctype="multipart/form-data">
<table>
<tr>
<td>导入路径:</td>
<td>
<select class="easyui-combobox" name="language" >
<option value="1">123</option>
<option value="2">456</option>
<option value="3">789</option>
</select>
</td>
</tr>
<tr>
<td>文件</td>
<td>
<input type=file name="file1" size="30"/>
</td>
</tr>
</table>
</form>
servlet部分:
UpFile
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
if (!isMultipartContent) {
return;
}

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
UpFileProgressListener upFileProgressListener = new UpFileProgressListener(request);
upload.setProgressListener(upFileProgressListener);

// HttpSession session = request.getSession();
// session.setAttribute("UpFileProgressListener", upFileProgressListener);

try {
List<FileItem> fields = upload.parseRequest(request);
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext()) {
return;
}
while (it.hasNext()) {
FileItem fileItem = it.next();
File f = new File("e:/abc");
try {
fileItem.write(f);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileUploadException e) {
out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
progress:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

HttpSession session = request.getSession(true);
if (session == null) {
out.println("Sorry, session is null"); // just to be safe
return;
}

String testProgressListener = (String) session.getAttribute("percentDone").toString();
if (testProgressListener == null) {
out.println("Progress listener is null");
return;
}

out.println(testProgressListener);
System.out.println(testProgressListener+"====<====<");
}

progress接口:
public class UpFileProgressListener implements ProgressListener {

private long num100Ks = 0;

private long theBytesRead = 0;
private long theContentLength = -1;
private int whichItem = 0;
private int percentDone = 0;
private boolean contentLengthKnown = false;
private HttpSession session;
public UpFileProgressListener(HttpServletRequest request) {
session=request.getSession();
}

public void update(long bytesRead, long contentLength, int items) {

if (contentLength > -1) {
contentLengthKnown = true;
}
theBytesRead = bytesRead;
theContentLength = contentLength;
whichItem = items;

long nowNum100Ks = bytesRead / 100000;
// Only run this code once every 100K
if (nowNum100Ks > num100Ks) {
num100Ks = nowNum100Ks;
if (contentLengthKnown) {
percentDone = (int) Math.round(100.00 * bytesRead / contentLength);
}
System.out.println(percentDone);
session.setAttribute("percentDone", percentDone);

}else{
session.setAttribute("percentDone", 100);
}

}

public String getMessage() {
if (theContentLength == -1) {
return "" + theBytesRead + " of Unknown-Total bytes have been read.";
} else {
return "" + percentDone;
}
}

public long getNum100Ks() {
return num100Ks;
}

public void setNum100Ks(long num100Ks) {
this.num100Ks = num100Ks;
}

public long getTheBytesRead() {
return theBytesRead;
}

public void setTheBytesRead(long theBytesRead) {
this.theBytesRead = theBytesRead;
}

public long getTheContentLength() {
return theContentLength;
}

public void setTheContentLength(long theContentLength) {
this.theContentLength = theContentLength;
}

public int getWhichItem() {
return whichItem;
}

public void setWhichItem(int whichItem) {
this.whichItem = whichItem;
}

public int getPercentDone() {
return percentDone;
}

public void setPercentDone(int percentDone) {
this.percentDone = percentDone;
}

public boolean isContentLengthKnown() {
return contentLengthKnown;
}

public void setContentLengthKnown(boolean contentLengthKnown) {
this.contentLengthKnown = contentLengthKnown;
}

}
...全文
285 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zfapple 2013-11-05
  • 打赏
  • 举报
回复
一开始下载没有弹,下载完了才会弹个100出来,感觉callback就是下载完了调用一次,好像被阻塞了一样
zfapple 2013-11-05
  • 打赏
  • 举报
回复
不会变,就最后弹出一个100
teemai 2013-11-05
  • 打赏
  • 举报
回复
每次alert//alert(msg);值会变吗?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧