SpringBoot上传文件如何返回前端进度条
更新时间:2024年05月30日 10:33:05 作者:是小故事呀
这篇文章主要介绍了SpringBoot上传文件如何返回前端进度条问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
SpringBoot上传文件返回前端进度条
话不多说,直接上干货
1、需要创建一个实现了processListener接口
的实现类用于监听文件上传进度
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 到目前为止读取文件的比特数 * 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 { // 读取的文件的比特数 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());// 文件上传进度监听器设置session用于存储上传进度 fileUpload.setProgressListener(uploadProgressListener);// 将文件上传进度监听器加入到 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 指向我们刚刚创建好的继承 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上传文件实现进度条代码编写完成,看效果
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
最新评论