首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

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

2013-11-21 
求教,文件上传进度条问题我使用的apache commons,但是上传后,前面一直阻塞,进度条不动,一直到最后上传完,

求教,文件上传进度条问题
我使用的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;
}

}
[解决办法]
每次alert//alert(msg);值会变吗?

热点排行