ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

OSS 上传下载的进度条

2019-12-28 14:55:43  阅读:397  来源: 互联网

标签:8192 进度条 OSS bytes upload been written time 上传下载


ossClient.uploadFile和ossClient.downloadFile方法不支持进度条功能.

 

 根据官方文档 https://help.aliyun.com/document_detail/84796.html?spm=a2c4g.11186623.6.791.40554d83cDiWSN

及代码示例制作加入进度条功能 https://github.com/aliyun/aliyun-oss-java-sdk/blob/master/src/samples/GetProgressSample.java?spm=a2c4g.11186623.2.7.39811bd4Zk16e3&file=GetProgressSample.java

 

 流式上传不支持百分比 而new File是支持的

流式上传:

Start to upload......
8192 bytes have been written at this time, upload ratio: unknown(8192/...)
8192 bytes have been written at this time, upload ratio: unknown(16384/...)
8192 bytes have been written at this time, upload ratio: unknown(24576/...)
8192 bytes have been written at this time, upload ratio: unknown(32768/...)
8192 bytes have been written at this time, upload ratio: unknown(40960/...)
8192 bytes have been written at this time, upload ratio: unknown(49152/...)
8192 bytes have been written at this time, upload ratio: unknown(57344/...)
1967 bytes have been written at this time, upload ratio: unknown(59311/...)
Succeed to upload, 59311 bytes have been transferred in total

而new File(localFilePath);是可以看到百分比的,从开始也获取了文件的整个大小.

Start to upload......
3546429 bytes in total will be uploaded to OSS
8192 bytes have been written at this time, upload progress: 0%(8192/3546429)
8192 bytes have been written at this time, upload progress: 0%(16384/3546429)
8192 bytes have been written at this time, upload progress: 0%(24576/3546429)
8192 bytes have been written at this time, upload progress: 0%(32768/3546429)
8192 bytes have been written at this time, upload progress: 1%(40960/3546429)
8192 bytes have been written at this time, upload progress: 1%(49152/3546429)
8192 bytes have been written at this time, upload progress: 1%(57344/3546429)
....
....
7485 bytes have been written at this time, upload progress: 100%(3546429/3546429)
Succeed to upload, 3546429 bytes have been transferred in total

整个上传下载速度都还挺快的

试了下300MB文件每次2M/s 

上传监听器

package com.springboot.oss.listener;

import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;

/**
 * ossClient.putObject, ossClient.getObject, ossClient.uploadPart方法支持进度条功能.
 * ossClient.uploadFile和ossClient.downloadFile方法不支持进度条功能.
 * 简单上传进度条监听器
 * https://help.aliyun.com/document_detail/84796.html?spm=a2c4g.11186623.6.791.40554d83cDiWSN
 */
public class PutObjectProgressListener implements ProgressListener {

    private long bytesWritten = 0;
    private long totalBytes = -1;
    private boolean succeed = false;

    @Override
    public void progressChanged(ProgressEvent progressEvent) {
        long bytes = progressEvent.getBytes();
        ProgressEventType eventType = progressEvent.getEventType();
        switch (eventType) {
            case TRANSFER_STARTED_EVENT:
                System.out.println("Start to upload......");
                break;

            case REQUEST_CONTENT_LENGTH_EVENT:
                this.totalBytes = bytes;
                System.out.println(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);
                    System.out.println(bytes + " bytes have been written at this time, upload progress: " +
                            percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")");
                } else {
                    System.out.println(bytes + " bytes have been written at this time, upload ratio: unknown" +
                            "(" + this.bytesWritten + "/...)");
                }
                break;

            case TRANSFER_COMPLETED_EVENT:
                this.succeed = true;
                System.out.println("Succeed to upload, " + this.bytesWritten + " bytes have been transferred in total");
                break;

            case TRANSFER_FAILED_EVENT:
                System.out.println("Failed to upload, " + this.bytesWritten + " bytes have been transferred");
                break;

            default:
                break;
        }
    }

    public boolean isSucceed(){
        return succeed;
    }
}

下载监听器

package com.springboot.oss.listener;

import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;

/**
 * 文件下载监听器
 * ossClient.uploadFile和ossClient.downloadFile方法不支持进度条功能.
 */
public class GetObjectProgressListener implements ProgressListener {

    private long bytesRead = 0;
    private long totalBytes = -1;
    private boolean succeed = false;

    @Override
    public void progressChanged(ProgressEvent progressEvent) {
        long bytes = progressEvent.getBytes();
        ProgressEventType eventType = progressEvent.getEventType();
        switch (eventType) {
            case TRANSFER_STARTED_EVENT:
                System.out.println("Start to download......");
                break;

            case RESPONSE_CONTENT_LENGTH_EVENT:
                this.totalBytes = bytes;
                System.out.println(this.totalBytes + " bytes in total will be downloaded to a local file");
                break;

            case RESPONSE_BYTE_TRANSFER_EVENT:
                this.bytesRead += bytes;
                if (this.totalBytes != -1) {
                    int percent = (int)(this.bytesRead * 100.0 / this.totalBytes);
                    System.out.println(bytes + " bytes have been read at this time, download progress: " +
                            percent + "%(" + this.bytesRead + "/" + this.totalBytes + ")");
                } else {
                    System.out.println(bytes + " bytes have been read at this time, download ratio: unknown" +
                            "(" + this.bytesRead + "/...)");
                }
                break;

            case TRANSFER_COMPLETED_EVENT:
                this.succeed = true;
                System.out.println("Succeed to download, " + this.bytesRead + " bytes have been transferred in total");
                break;

            case TRANSFER_FAILED_EVENT:
                System.out.println("Failed to download, " + this.bytesRead + " bytes have been transferred");
                break;

            default:
                break;
        }
    }

    public boolean isSucceed() {
        return succeed;
    }
}

关于怎样把进度展示到前端去,可能需要在方法调用上再调整一番.

标签:8192,进度条,OSS,bytes,upload,been,written,time,上传下载
来源: https://www.cnblogs.com/ukzq/p/12111638.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有