Java spring boot中的文件上载进度

Java spring boot中的文件上载进度,java,spring,file,spring-boot,file-upload,Java,Spring,File,Spring Boot,File Upload,要在spring boot中上载文件,可以使用以下方法: @PostMapping("/") public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { storageService.store(file); redirectAttributes.addFlashAttribute("message",

要在spring boot中上载文件,可以使用以下方法:

@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
    RedirectAttributes redirectAttributes) {

    storageService.store(file);
    redirectAttributes.addFlashAttribute("message",
        "You successfully uploaded " + file.getOriginalFilename() + "!");

    return "redirect:/";
}
而且效果很好

我的问题是如何扩展此代码以在上载过程中获得某种更新。例如:每完成上传的10%发送一封电子邮件。
是否有任何机制在上传期间创建此类事件?我可以覆盖一些内部spring方法以使其工作吗?

要获得进度更新回调,请将以下bean添加到应用程序中:

@Bean(name = "multipartResolver")
public CommonsMultipartResolver createMultipartResolver() {

  final CommonsMultipartResolver cmr = new CommonsMultipartResolver();

  cmr.setMaxUploadSize(10000000); // customize as appropriate
  cmr.setDefaultEncoding("UTF-8");  // important to match in your client
  cmr.getFileUpload().setProgressListener(
      (long pBytesRead, long pContentLength, int pItems) -> {
        // insert progress update logic here
      });

  return cmr;
}
还要将以下属性添加到应用程序属性中:

spring.http.multipart.enabled = false

注意:客户机应用程序必须与预期的内容编码相匹配(本例中为
UTF-8
)。如果没有,则将使用临时
FileUpload
对象,而不是我们在上述代码中自定义的对象。这可能是
CommonFileUploadSupport.java中的一个错误,因为它将原始
FileUpload
的所有其他成员复制到临时文件中,而忽略了侦听器。

我担心这是不可能的。例如,它类似于从一个位置复制并粘贴到另一个位置。粘贴可能需要更多的时间,因为这是一个写操作,在这段时间内,如果源更改意味着当前粘贴过程将不会意识到它。您必须重新上传该文件。当然,但在复制粘贴过程中,您可以看到复制的进度条。我在找类似的东西。只是从后端完成,而不是像我找到的所有教程那样从前端完成。我认为您必须在这里开发自己的逻辑