一、首先需要自定义一个监听进度的listener
import javax.servlet.http.HttpSession; import org.apache.commons.fileupload.ProgressListener; import org.springframework.stereotype.Component; @Component public class FileUploadProgressListener implements ProgressListener { private HttpSession session; public void setSession(HttpSession session){ this.session=session; Progress status = new Progress(); session.setAttribute("status", status); }
/*
* pBytesRead 到目前为止读取文件的比特数 pContentLength 文件总大小 pItems 目前正在读取第几个文件
*/
public void update(long pBytesRead, long pContentLength, int pItems) {
//System.out.println(“正在更新:pBytesRead=”+pBytesRead+",pContentLength="+pContentLength+",pItems="+pItems);
Progress status = (Progress) session.getAttribute(“status”);
status.setpBytesRead(pBytesRead);
status.setpContentLength(pContentLength);
status.setpItems(pItems);
Progress status1 = (Progress) session.getAttribute(“status”);
//System.out.println(“session更新了:pBytesRead=”+status1.getpBytesRead()+",pContentLength="+status1.getpContentLength()+",pItems="+status1.getpItems());
}
}
二、将自定义的listener注入到解析multimultipartResolver里面,实现上传文件的数据不断更新
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
public class CustomMultipartResolver extends CommonsMultipartResolver {
@Autowired
private FileUploadProgressListener progressListener;
public void setFileUploadProgressListener(FileUploadProgressListener progressListener){
this.progressListener=progressListener;
}
@Override
@SuppressWarnings(“unchecked”)
public MultipartParsingResult parseRequest(HttpServletRequest request)
throws MultipartException {
String encoding = determineEncoding(request);
FileUpload fileUpload = prepareFileUpload(encoding);
progressListener.setSession(request.getSession());
fileUpload.setProgressListener(progressListener);
try {
List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
return parseFileItems(fileItems, encoding);
}
catch (FileUploadBase.SizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
}
catch (FileUploadException ex) {
throw new MultipartException(“Could not parse multipart servlet request”, ex);
}
}
}
三、自定义Progress类来封装listener更新的数据
/**
* Created by liudaxia on 2018/7/10.
*/
public class Progress {<span style="color:#659b58;">/**
* long pBytesRead, long pContentLength, int pItems
*/
private long pBytesRead;
private long pContentLength;
private int pItems;
public long getpBytesRead() {
return pBytesRead;
}
<span style="color:#e66600;">public void </span><span style="color:#d8a92f;">setpBytesRead</span>(<span style="color:#e66600;">long </span><span style="color:#fffab1;">pBytesRead</span>) {
<span style="color:#e66600;">this</span>.<span style="color:#fffab1;">pBytesRead </span>= <span style="color:#fffab1;">pBytesRead</span><span style="color:#e66600;">;
}
<span style="color:#e66600;">public long </span><span style="color:#d8a92f;">getpContentLength</span>() {
<span style="color:#e66600;">return </span><span style="color:#fffab1;">pContentLength</span><span style="color:#e66600;">;
}
<span style="color:#e66600;">public void </span><span style="color:#d8a92f;">setpContentLength</span>(<span style="color:#e66600;">long </span><span style="color:#fffab1;">pContentLength</span>) {
<span style="color:#e66600;">this</span>.<span style="color:#fffab1;">pContentLength </span>= <span style="color:#fffab1;">pContentLength</span><span style="color:#e66600;">;
}
<span style="color:#e66600;">public int </span><span style="color:#d8a92f;">getpItems</span>() {
<span style="color:#e66600;">return </span><span style="color:#fffab1;">pItems</span><span style="color:#e66600;">;
}
<span style="color:#e66600;">public void </span><span style="color:#d8a92f;">setpItems</span>(<span style="color:#e66600;">int </span><span style="color:#fffab1;">pItems</span>) {
<span style="color:#e66600;">this</span>.<span style="color:#fffab1;">pItems </span>= <span style="color:#fffab1;">pItems</span><span style="color:#e66600;">;
}
<span style="color:#b3e633;">@Override
public String toString() {
return “Progress{” +
“pBytesRead=” + pBytesRead +
“, pContentLength=” + pContentLength +
“, pItems=” + pItems +
‘}’;
}
}
四、写个controller不断供前台进行调用
@Controller
@SessionAttributes(“status”)
public class ProgressController {
@RequestMapping(value = “/upfile/progress”)
@ResponseBody
public String initCreateInfo(Map<String, Object> model) {
Progress status = (Progress) model.get(“status”);
if(status==null){
return “{}”;
}
System.out.println(status.toString());
return status.toString();
}
}
五、修改xml文件
<bean id=“multipartResolver”
class=“此处改为自己定义的CustomMultipartResoler”>
<property name=“maxUploadSize” value=“4194304000” />
<property name=“maxInMemorySize” value=“1048576” />
</bean>
原文链接: https://blog.csdn.net/qq923014086/article/details/80997154