All Downloads are FREE. Search and download functionalities are using the official Maven repository.

matrix.boot.common.dto.UploadProgressDto Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package matrix.boot.common.dto;

import lombok.Data;
import lombok.experimental.Accessors;
import matrix.boot.common.utils.ThreadUtil;
import org.apache.commons.fileupload.ProgressListener;

/**
 * 上传进度
 * @author wangcheng
 */
@Data
@Accessors(chain = true)
public abstract class UploadProgressDto implements ProgressListener {

    /**
     * 容器
     */
    private T container;

    /**
     * 上传速率
     */
    private Integer uploadRate = 0;

    /**
     * 上传bytes
     */
    private long bytesRead = 0L;

    /**
     * 每秒上传多少KB
     */
    private long bytesReadKBSpeedWith1s = 0L;

    public UploadProgressDto(T container) {
        this.container = container;
        ThreadUtil.startThread(() -> {
            while (uploadRate < 100) {
                long beforeRead = bytesRead;
                ThreadUtil.sleep(1000L);
                bytesReadKBSpeedWith1s = (bytesRead - beforeRead) / 1024;
            }
        });
    }

    /**
     * 更新方法(上传时的更新方法)
     * @param bytesRead 已上传byte
     * @param contentLength 内容大小
     * @param items 数量
     */
    @Override
    public void update(long bytesRead, long contentLength, int items) {
        Integer uploadRate = Math.round(bytesRead * 100.0f / contentLength);
        this.bytesRead = bytesRead;
        this.uploadRate = uploadRate;
        this.execute(this.container, uploadRate, this.bytesReadKBSpeedWith1s);
    }

    /**
     * 回调执行方法
     * @param container 容器
     * @param uploadRate 传输速率
     * @param bytesReadKBSpeedWith1s 参数
     */
    public abstract void execute(T container, Integer uploadRate, long bytesReadKBSpeedWith1s);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy