SpringBoot上傳文件如何返回前端進度條
更新時間:2024年05月30日 10:33:05 作者:是小故事呀
這篇文章主要介紹了SpringBoot上傳文件如何返回前端進度條問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
SpringBoot上傳文件返回前端進度條
話不多說,直接上干貨
1、需要創(chuàng)建一個實現(xiàn)了processListener接口
的實現(xiàn)類用于監(jiān)聽文件上傳進度
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;
@Component
public class UploadProgressListener implements ProgressListener {
private HttpSession session;
public void setSession(HttpSession session){
this.session=session;
ProgressEntity status = new ProgressEntity();
session.setAttribute("status", status);
}
/* pBytesRead 到目前為止讀取文件的比特數(shù)
* pContentLength 文件總大小
* pItems 目前正在讀取第幾個文件
*/
public void update(long pBytesRead, long pContentLength, int pItems) {
ProgressEntity status = (ProgressEntity) session.getAttribute("status");
status.setPBytesRead(pBytesRead);
status.setPContentLength(pContentLength);
status.setPItems(pItems);
System.out.println("UploadProgressListener update ProgressEntity: "+status.toString());
}
}
2、返回給前端的上傳進度的實體類
@Component
public class ProgressEntity {
// 讀取的文件的比特數(shù)
private long pBytesRead = 0L;
// 文件的總大小
private long pContentLength = 0L;
// 目前正在讀取第幾個文件
private int pItems;
private long startTime = System.currentTimeMillis();
public ProgressEntity() {
pBytesRead = 0L;
pContentLength = 0L;
}
public long getPBytesRead() {
return pBytesRead;
}
public void setPBytesRead(long bytesRead) {
pBytesRead = bytesRead;
}
public long getPContentLength() {
return pContentLength;
}
public void setPContentLength(long contentLength) {
pContentLength = contentLength;
}
public int getPItems() {
return pItems;
}
public void setPItems(int items) {
pItems = items;
}
@Override
public String toString() {
float tmp = (float) pBytesRead;
float result = tmp / pContentLength * 100;
return "ProgressEntity [pBytesRead=" + pBytesRead + ", pContentLength="
+ pContentLength + ", percentage=" + result + "% , pItems=" + pItems + "]";
}
}
3、繼承CommonsMultipartResolver類
自定義文件上傳處理類
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 UploadProgressListener uploadProgressListener;
@Override
protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {
String encoding = determineEncoding(request);
FileUpload fileUpload = prepareFileUpload(encoding);
uploadProgressListener.setSession(request.getSession());// 文件上傳進度監(jiān)聽器設置session用于存儲上傳進度
fileUpload.setProgressListener(uploadProgressListener);// 將文件上傳進度監(jiān)聽器加入到 fileUpload 中
try {
List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);
return parseFileItems(fileItems, encoding);
} catch (FileUploadBase.SizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);
} catch (FileUploadBase.FileSizeLimitExceededException ex) {
throw new MaxUploadSizeExceededException(fileUpload.getFileSizeMax(), ex);
} catch (FileUploadException ex) {
throw new MultipartException("Failed to parse multipart servlet request", ex);
}
}
}
4、將spring默認的文件上傳處理類取消自動配置
并將 multipartResolver 指向我們剛剛創(chuàng)建好的繼承 CommonsMultipartResolver 類的自定義文件上傳處理類
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import com.baidu.doclabel.biz.listener.CustomMultipartResolver;
@EnableAutoConfiguration
@Configuration
public class UploadProgressApplication {
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver() {
CustomMultipartResolver customMultipartResolver = new CustomMultipartResolver();
return customMultipartResolver;
}
}
至此,SpringBoot上傳文件實現(xiàn)進度條代碼編寫完成,看效果
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java異常處理 Throwable實現(xiàn)方法解析
這篇文章主要介紹了Java異常處理 Throwable實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10
SpringBoot Application的exclude不生效問題及排查
這篇文章主要介紹了SpringBoot Application的exclude不生效問題及排查,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
使用springboot結(jié)合vue實現(xiàn)sso單點登錄
這篇文章主要為大家詳細介紹了如何使用springboot+vue實現(xiàn)sso單點登錄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06

