FileCleanerCleanup and ProgressListener in Commons fileupload

  Commons fileupload组件提供了两个辅助类和接口。

 

   第一个是FileCleanerCleanup,用于控制文件上传过程时产生的临时文件因未能及时删除而且做的文件清除工作。使用它只需要在Web项目的部署描述文件web.xml中添加一个监听器即可。

 

<listener>
        <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class>
</listener>

 

  该监听器会启动一个FileCleaningTracker,即文件清除跟踪器用于清除临时文件,节省磁盘空间。在FileCleaningTracker中,有一个reaper属性,类型为Thread,该收割机线程会开展临时文件清除的工作。

 

  第二个是ProgressListener接口,用于监听文件上传的进度,需要实现该功能,只需要自定义一个实现该接口的类,在接口方法中定义自定义实现即可。

 

 


package com.dream.file;

import org.apache.commons.fileupload.ProgressListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-9-4
 * Time: 上午10:18
 */
public class FileUploadProgressListener implements ProgressListener {
    private Logger logger = LoggerFactory.getLogger(FileUploadProgressListener.class);

    /**
     * Updates the listeners status information.
     *
     * @param pBytesRead     The total number of bytes, which have been read
     *                       so far.
     * @param pContentLength The total number of bytes, which are being
     *                       read. May be -1, if this number is unknown.
     * @param pItems         The number of the field, which is currently being
     *                       read. (0 = no item so far, 1 = first item is being read, ...)
     */
    @Override
    public void update(long pBytesRead, long pContentLength, int pItems) {
        if (pBytesRead % 1024 == 0) {
            long percent = (pBytesRead / pContentLength) * 100;
            logger.info("Has uploaded the file of " + percent + "%.");
        }
    }
}

 

  自定义文件上传进度监听器后,还需要注入到FileUpload类中去。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值