我正在尝试使用java获取S3的上传百分比进度。我试着这样做:
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);我得到的输出如下:
...
Percentage completed: 2.0119817541351908
Completed
Percentage completed: 1.4128940473756373
Completed
Percentage completed: 0.0
Completed
Percentage completed: 0.0
Completed如何获得合适的百分比值?
发布于 2018-04-06 21:28:33
如果getBytesTransferred()返回非累加值-您需要将总字节数存储在侦听器方法之外(现在您在侦听器方法内部声明了变量totalByteRead,因此每次触发progressChanged事件时都会将其重新声明为0)
发布于 2018-04-06 21:48:52
// 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 whilehttps://stackoverflow.com/questions/49692075
复制相似问题