OSS 上传下载的进度条

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;
    }
}

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

猜你喜欢

转载自www.cnblogs.com/ukzq/p/12111638.html