添加文件上传进度条

This commit is contained in:
wuchunlei
2024-03-14 10:58:13 +08:00
parent c3ca9f32f9
commit 36bb3fc194
4 changed files with 47 additions and 99 deletions

View File

@@ -9,6 +9,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/oss/fileoss")
@@ -40,8 +43,7 @@ public class OssController {
//获取进度条
@GetMapping("/getSchedule")
public R getSchedule(String uid){
String percent = ossService.getSchedule(uid);
return R.ok().put("percent",percent);
return ossService.getSchedule(uid);
}

View File

@@ -6,17 +6,21 @@ import com.aliyun.oss.event.ProgressListener;
import com.peanut.common.utils.SpringContextUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
public class PutObjectProgressListener implements ProgressListener {
private long bytesWritten = 0;
private long totalBytes = 0;
private String uid = "";
private String url = "";
public PutObjectProgressListener() {
}
public PutObjectProgressListener(long fileSize,String uid) {
public PutObjectProgressListener(long fileSize,String uid,String url) {
this.totalBytes = fileSize;
this.uid = uid;
this.url = url;
}
@Override
@@ -37,14 +41,14 @@ public class PutObjectProgressListener implements ProgressListener {
this.bytesWritten += bytes;
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 + ")");
redisTemplate.opsForValue().set(uid,percent+"");
redisTemplate.opsForValue().set(uid,percent+";"+url);
redisTemplate.expire(uid, 600, TimeUnit.SECONDS);
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:
redisTemplate.delete(uid);
// System.out.println("Failed to upload, " + this.bytesWritten + " bytes have been transferred");
// break;
default: