首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以S3格式获取上传和下载进度百分比

问以S3格式获取上传和下载进度百分比
EN

Stack Overflow用户
提问于 2018-04-06 19:45:26
回答 2查看 497关注 0票数 1

我正在尝试使用java获取S3的上传百分比进度。我试着这样做:

代码语言:javascript
复制
putObjectRequest.setGeneralProgressListener(new ProgressListener() {
    @Override
    public void progressChanged(ProgressEvent progressEvent) {
        progressEvent.getBytesTransferred();
        double totalByteRead = 0;
        double percentage = 0;
        totalByteRead += progressEvent.getBytesTransferred();
        percentage += (totalByteRead / size) * 100;
        System.out.println("Percentage completed: " + percentage);
        if (progressEvent.getEventType() == progressEvent.getEventType()) {
            System.out.println("Completed");
        }
    }
});
s3.putObject(putObjectRequest);

我得到的输出如下:

代码语言:javascript
复制
...
Percentage completed: 2.0119817541351908
Completed
Percentage completed: 1.4128940473756373
Completed
Percentage completed: 0.0
Completed
Percentage completed: 0.0
Completed

如何获得合适的百分比值?

EN

回答 2

Stack Overflow用户

发布于 2018-04-06 21:28:33

如果getBytesTransferred()返回非累加值-您需要将总字节数存储在侦听器方法之外(现在您在侦听器方法内部声明了变量totalByteRead,因此每次触发progressChanged事件时都会将其重新声明为0)

票数 0
EN

Stack Overflow用户

发布于 2018-04-06 21:48:52

代码语言:javascript
复制
    // Something to this effect
    // percentage += (totalByteRead / size) * 100;

    int arraymakerCOUNT=0;
    long partial = new Float(size / 100).longValue(); // make 1 percent
    long partial2 = partial; // partial and partial2 are 100th of file bytes size
    //
    long[] percentProgress = new long[100];
    while(arraymakerCOUNT < 100){
    if(arraymakerCOUNT==0){
    percentProgress[arraymakerCOUNT] = partial2;
    }else{
    partial += partial2; // make partial bigger by 1 percent relating file size count
    percentProgress[arraymakerCOUNT] = partial;
    }
    arraymakerCOUNT++;
    } //end while

// Above should be found when the process starts BUT percentProgress[] array should be available in scope to below

    // BELOW is for the "change listener" triggered by it or triggers a method with the listener containing its call
    int percentCheckLoop = 0; // for the index of the 100 element array containing 1 percent STEP sizes in order
    while(percentCheckLoop < 100){ // loop from 1 toward 100 percent by 1 percent at a test
    if(percentProgress[percentCheckLoop] < (new Double(totalByteRead).longValue())){
     System.out.println("Percentage completed: " + (percentCheckLoop+1);
// switch loop off after printout
    percentCheckLoop = 101; // ready the STOP loop condition
    }
    percentCheckLoop++;
    } // end while
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49692075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档