如何处理和删除“忘记”上传的文件? [英] How to handle and delete "forgotten" uploaded files?

查看:292
本文介绍了如何处理和删除“忘记”上传的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单来上传不同类型的文件。我需要根据上传的文件类型提问。例如,如果该文件是pdf,我需要问作者。如果文件是mp3,我需要问这首歌的标题。



所以:


  1. 用户上传保存在服务器某处的文件;

  2. 用户回答与文件类型相关的问题;

  3. 用户点击保存按钮(答案被确认)来确认一切。

目前一切都很好。现在,如果用户从不回答问题或从不点击保存按钮?显然我需要删除这个忘记的文件。

我的想法是将未确认的文件存储在一个定期清理的远程缓冲区中。问题是适当地管理这个缓冲区,并决定何时清理它。



有没有最佳实践/现有解决方案?什么是最好的方法?

解决方案

跟踪 @SessionScoped中未经确认的上传文件

code> bean并使用 @



开球示例:

执行清理。 p> @SessionScoped
public class UserFileManager {

private List< File> unconfirmedUploadedFiles;

@PostConstruct
public void init(){
unconfirmedUploadedFiles = new ArrayList<>();
}

public void addUnconfirmedUploadedFile(File unconfirmedUploadedFile){
unconfirmedUploadedFiles.add(unconfirmedUploadedFile);
}

public void confirmUploadedFile(File confirmedUploadedFile){
unconfirmedUploadedFiles.remove(confirmedUploadedFile);


$ PreDestroy
public void destroy(){
for(File unconfirmedUploadedFile:unconfirmedUploadedFiles){
unconfirmedUploadedFile.delete();



$ b code


您不应该将文件内容存储在服务器的内存中。它迟早会炸毁服务器。而是将它们存储在磁盘上并传递文件引用。


I have a form to upload different kind of files. I need to ask questions according to the uploaded file type. For instance, if the file is a pdf, I need to ask the author. If the file is an mp3, I need to ask the title of the song.

So :

  1. the user uploads the file which is saved somewhere on the server;
  2. the user answers the questions associated to the file type;
  3. the user clicks the Save button (the answers are validated) to confirm everything.

Everything is fine so far. Now what if the user never answer the questions or never click the Save Button? Obviously I need to delete this "forgotten" file.

An idea I had was to store "unconfirmed" files in a kind of remote buffer cleaned on a regular basis. Problem is managing appropriately this buffer and deciding when to clean it.

Is there any best practice / existing solution for this? What is the best approach?

解决方案

Keep track of those unconfirmed uploaded files in a @SessionScoped bean and use @PreDestroy to perform cleanup.

Kickoff example:

@SessionScoped
public class UserFileManager {

    private List<File> unconfirmedUploadedFiles;

    @PostConstruct
    public void init() {
        unconfirmedUploadedFiles = new ArrayList<>();
    }

    public void addUnconfirmedUploadedFile(File unconfirmedUploadedFile) {
        unconfirmedUploadedFiles.add(unconfirmedUploadedFile);
    }

    public void confirmUploadedFile(File confirmedUploadedFile) {
        unconfirmedUploadedFiles.remove(confirmedUploadedFile);
    }

    @PreDestroy
    public void destroy() {
        for (File unconfirmedUploadedFile : unconfirmedUploadedFiles) {
            unconfirmedUploadedFile.delete();
        }
    }

}

Do note that you shouldn't be storing the file content in server's memory. It will blow up the server sooner or later. Rather store them on disk and pass around File references.

这篇关于如何处理和删除“忘记”上传的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆