upload ratio: unknown(32768/ ...) 8192 bytes have been writ
标签:
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 + "http://www.mamicode.com/" + 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; } }
下载监听器
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32835.html