Atitit 上传进度的实现与原理
目录
1.1. 前端 1
1.2. 读取进度 1
1.3. 后端 定时注入进度 1
window.setInterval(function(){
var url="../commServletV3?class=%20com.attilax.web.UploadServlet1%20&method=upProcessInfo%20&argstypes=&args=";
$.get(url, function (result, status, xhr) {
$("#uppct").text(result.rate+"%");
}, "json");
},3000);
public static Object upProcessInfo() {
return Global.getVarVal("vals626_upProcessInfo");
}
//{"itemNum":3,"path":"","rate":100,"readSize":57815489,"show":"57815489/57815489 byte","totalSize":57815489}
upload.setProgressListener(new ProgressListener() {
public void update(long pBytesRead, long pContentLength, int pItems) {
ProcessInfo pri = new ProcessInfo();
pri.itemNum = pItems;
pri.readSize = pBytesRead;
pri.totalSize = pContentLength;
pri.show = pBytesRead + "/" + pContentLength + " byte";
pri.rate = Math.round(new Float(pBytesRead) / new Float(pContentLength) * 100);
System.out.println( JSON.toJSONString(pri) );
Global.setVarVal("vals626_upProcessInfo", pri);
// hs.setAttribute("proInfo", pri);
}
});
package com.attilax.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import com.alibaba.fastjson.JSON;
import com.attilax.io.pathx;
import com.attilax.util.Global;
import com.attilax.util.cli.SSHHelper;
import com.google.common.collect.Maps;
//
@WebServlet(name = "UploadServlet1", urlPatterns = { "/UploadServlet1", "/dafdafsss22" }, loadOnStartup = 1)
public class UploadServlet1 extends HttpServlet {
public static Object upProcessInfo() {
return Global.getVarVal("vals626_upProcessInfo");
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
// 创建工厂 common fileup 1.3 jar
DiskFileItemFactory factory = new DiskFileItemFactory();
// 解决上传文件名的中文乱码
// upload.setHeaderEncoding("UTF-8");
// 通过工厂创建解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//{"itemNum":3,"path":"","rate":100,"readSize":57815489,"show":"57815489/57815489 byte","totalSize":57815489}
upload.setProgressListener(new ProgressListener() {
public void update(long pBytesRead, long pContentLength, int pItems) {
ProcessInfo pri = new ProcessInfo();
pri.itemNum = pItems;
pri.readSize = pBytesRead;
pri.totalSize = pContentLength;
pri.show = pBytesRead + "/" + pContentLength + " byte";
pri.rate = Math.round(new Float(pBytesRead) / new Float(pContentLength) * 100);
System.out.println( JSON.toJSONString(pri) );
Global.setVarVal("vals626_upProcessInfo", pri);
// hs.setAttribute("proInfo", pri);
}
});