简体   繁体   中英

Vaadin - deleting temporary Files from Upload-Component

hope you can help me. I have a Spring-Boot vaadin-Project with a few Upload-Fields. Everythings fine. if you click on the send button in the end everything is processed and tempfiles are deleted. Though when you upload a file and leave the site then the temp-directory stays untouched. Is there any way to programatically delete all temporary files when a new instance is called?

When I upload a file on a built-with-vaadin-website and leave the site then, my temp directory gets fuller and fuller. i just want to delete all files which were created in the actual Vaadin Session when starting a new one. Or is there a way to find all files created in a spring session periodically?

I would create a custom VaadinServiceInitListener class (annotated with @Component), I would make a deleting method and in the serviceInit method I would call the deleting method with the uploading path. Something like this:

@Component
public class ApplicationServiceInitListener
    implements VaadinServiceInitListener {

  @Override
  public void serviceInit(ServiceInitEvent event) {
      // Delete the upload directory's content
      try {
          deleteDirectory(new File("[your_upload_directory_path]"));
      } catch (IOException e) {
          throw new RuntimeException(e);
      }
      // ...
  }

  private boolean deleteDirectory(File directoryToBeDeleted) {
      File[] allContents = directoryToBeDeleted.listFiles();
      if (allContents != null) {
          for (File file : allContents) {
              deleteDirectory(file);
          }
      }
      return directoryToBeDeleted.delete();
  }

}

Service Init Listener Vaadin doc: https://vaadin.com/docs/latest/advanced/service-init-listener

Ps: Of course, you can also use a File Util class, eg from common-io.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM