添加文件上传进度

This commit is contained in:
wuchunlei
2024-03-13 15:46:21 +08:00
parent cf2fa5daf6
commit ce9fbaa319
4 changed files with 67 additions and 17 deletions

View File

@@ -30,10 +30,17 @@ public class OssController {
return R.ok().put("url",url);
}
@PostMapping("/uploadFileSchedule")
@ApiOperation("上传带进度条")
public R uploadFileSchedule(@RequestParam("file") MultipartFile file,@RequestParam("uid") String uid) {
String url = ossService.uploadFileSchedule(file,uid);
return R.ok().put("url",url);
}
//获取进度条
@GetMapping("/getSchedule")
public R getSchedule(String fileName){
String percent = ossService.getSchedule(fileName);
public R getSchedule(String uid){
String percent = ossService.getSchedule(uid);
return R.ok().put("percent",percent);
}

View File

@@ -10,13 +10,13 @@ public class PutObjectProgressListener implements ProgressListener {
private long bytesWritten = 0;
private long totalBytes = 0;
private String fileName = "";
private String uid = "";
public PutObjectProgressListener() {
}
public PutObjectProgressListener(long fileSize,String fileName) {
public PutObjectProgressListener(long fileSize,String uid) {
this.totalBytes = fileSize;
this.fileName = fileName;
this.uid = uid;
}
@Override
@@ -37,14 +37,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(fileName,percent+"");
redisTemplate.opsForValue().set(uid,percent+"");
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(fileName);
redisTemplate.delete(uid);
// System.out.println("Failed to upload, " + this.bytesWritten + " bytes have been transferred");
// break;
default: