阿里云 OSS监控上传进度

阿里云 OSS监控上传进度

阿里云上传进度SDK

使用阿里云带进度条的上传,然后将长传进度存入session

 // 带进度条的上传
            ossClient.putObject(new PutObjectRequest(OSSConfigure.bucketName, path + fileName, is, objectMetadata).
                    <PutObjectRequest>withProgressListener(new PutObjectProgressListener(session)));

阿里云监控上传进度

package com.zichan360.oss;
import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpSession;

/**
* @ClassName: PutObjectProgressListener
* @ProjectName Validator
* @Author zhaohp
* @Date 2018/8/27 11:03
* @Description: OSS文件上传进度
*/
@Slf4j
public class PutObjectProgressListener implements ProgressListener {
private long bytesWritten = 0;
private long totalBytes = -1;
private boolean succeed = false;
private HttpSession session;
private int percent = 0;

//构造方法中加入session
public PutObjectProgressListener() {
}

public PutObjectProgressListener(HttpSession mSession) {
    this.session = mSession;
    session.setAttribute("upload_percent", percent);
}

@Override
public void progressChanged(ProgressEvent progressEvent) {
    long bytes = progressEvent.getBytes();
    ProgressEventType eventType = progressEvent.getEventType();
    switch (eventType) {
        case TRANSFER_STARTED_EVENT:
            log.debug("Start to upload......");
            break;
        case REQUEST_CONTENT_LENGTH_EVENT:
            this.totalBytes = bytes;
            log.debug(this.totalBytes + " bytes in total will be uploaded to OSS");
            break;
        case REQUEST_BYTE_TRANSFER_EVENT:
            this.bytesWritten += bytes;
            if (this.totalBytes != -1) {
                int percent = (int) (this.bytesWritten * 100.0 / this.totalBytes);

                //将进度percent放入session中
                session.setAttribute("upload_percent", percent);

                log.debug(bytes + " bytes have been written at this time, upload progress: " + percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")");
            } else {
                log.debug(bytes + " bytes have been written at this time, upload ratio: unknown" + "(" + this.bytesWritten + "/...)");
            }
            break;
        case TRANSFER_COMPLETED_EVENT:
            this.succeed = true;
            log.debug("Succeed to upload, " + this.bytesWritten + " bytes have been transferred in total");
            break;
        case TRANSFER_FAILED_EVENT:
            log.debug("Failed to upload, " + this.bytesWritten + " bytes have been transferred");
            break;
        default:
            break;
    }
}

public boolean isSucceed() {
    return succeed;
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值