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

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

原文地址:https://www.cnblogs.com/ukzq/p/12111638.html

时间: 2024-10-29 19:14:30

OSS 上传下载的进度条的相关文章

小小一方士 C# Async\Await 之 上传/下载文件进度条实现原理

关于上传下载文件(图片等),涉及到UI进度条的显示,c#中 System.IProgress提供了相应的api. namespace System { // // 摘要: // 定义进度更新的提供程序. // // 类型参数: // T: // 进度更新值的类型.此类型参数是逆变.即可以使用指定的类型或派生程度更低的类型.有关协变和逆变的详细信息,请参阅 泛型中的协变和逆变. public interface IProgress<in T> { // // 摘要: // 报告进度更新. //

字符串操作以及打印 —— 实现上传下载的进度条功能

import sys def processBar(num, total): rate = num / total rate_num = int(rate * 100) if rate_num == 100: r = '\r%s>%d%%\n' % ('=' * rate_num, rate_num,) else: r = '\r%s>%d%%' % ('=' * rate_num, rate_num,) sys.stdout.write(r) sys.stdout.flush process

Retrofit2文件上传下载及其进度显示

序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multipart @POST("fileService") Call<User> uploadFile(@Part MultipartBody.Part file); 构造请求体上传 1 2 3 4 5 File file = new File(filePath); RequestBod

asp.net 文件批量选取,批量上传,带进度条,uploadify3.2 TOP

http://www.16aspx.com/Article/3444 asp.net 文件批量选取,批量上传,带进度条,uploadify3.2 TOP,布布扣,bubuko.com

用Struts2实现文件上传时显示进度条功能

最近在做一个资源共享的项目中,采用了Struts2.1.8+Spring2.5.6+hibernate3.32的框架整合方式进行开发.在文件上传这块,因为需要实现文件上传时显示进度条的功能,所以尝试了一下.怕以后忘记,先贴出来分享下.   要在上传文件时能显示进度条,首先需要实时的获知web服务端接收了多少字节,以及文件总大小,这里我们在页面上使用AJAX技术每一秒向服务器发送一次请求来获得需要的实时上传信息.但是当我们使用struts2后怎么在服务端获得实时的上传大小呢?这里需要用到commo

Asp.Net 无刷新文件上传并显示进度条的实现方法及思路

相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦苦来 实现呢?我并不否认"拿来主义",只是我个人更喜欢凡是求个所以然.本篇将阐述通过Html,IHttpHandler和 IHttpAsyncHandler实现文件上传和上传进度的原理,希望对你有多帮助. 效果图: 本文涉及到的知识点:1.前台用到Html,Ajax,JQuery,JQuery UI 2.后台用到一般处理程序(IHttpHandler)和

ajax 异步上传视频带进度条并提取缩略图

最近在做一个集富媒体功能于一身的项目.需要上传视频.这里我希望做成异步上传,并且有进度条,响应有状态码,视频连接,缩略图. 1 { 2 "thumbnail": "/slsxpt//upload/thumbnail/6f05d4985598160c548e6e8f537247c8.jpg", 3 "success": true, 4 "link": "/slsxpt//upload/video/6f05d498559

asp.net mvc 实现上传文件带进度条

思路:ajax异步上传文件,且开始上传文件的时候启动轮询来实时获取文件上传进度.保存进度我采用的是memcached缓存,因为项目其他地方也用了的,所以就直接用这个啦.注意:不能使用session来保存进度,因为session是线程安全的不能实时获取进度,可是试试httpcache或者memorycache,这两个我没有试过,请自行尝试. ps:使用websocket来实现也是不错的,不过我没有试过,有心的大神可以去试试. 下面贴一张效果图: 前端ajax上传文件,我使用了两种jq插件.一种是a

servlet多文件上传(带进度条)

需要commons-fileupload-1.3.jar和commons-io-2.4.jar的支持 页面效果:(图片文件都可以) (1)进度标识类 public class UploadStatus { private long bytesRead; private long contentLength; private int items; private long startTime = System.currentTimeMillis(); //set get 方法 省略 } (2)监听