1. 程式人生 > >springmvc 後臺實現監聽上傳檔案大小及進度

springmvc 後臺實現監聽上傳檔案大小及進度

一、首先需要自定義一個監聽進度的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

一、首先需要自定義一個監聽進度的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);
   }