添加文件上传进度条

This commit is contained in:
wuchunlei
2024-03-13 14:33:24 +08:00
parent fb1426398f
commit e6d1250cf6
3 changed files with 32 additions and 5 deletions

View File

@@ -7,10 +7,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Slf4j
@RestController
@@ -32,4 +29,13 @@ public class OssController {
String url = ossService.uploadFileAvatar(file);
return R.ok().put("url",url);
}
//获取进度条
@GetMapping("/getSchedule")
public R getSchedule(String fileName){
String percent = ossService.getSchedule(fileName);
return R.ok().put("percent",percent);
}
}

View File

@@ -7,4 +7,6 @@ public interface OssService {
String uploadFileAvatar(MultipartFile file);
String uploadHtml(String html,String name);
String getSchedule(String fileName);
}

View File

@@ -2,10 +2,14 @@ package com.peanut.modules.oss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.peanut.common.utils.ConstantPropertiesUtils;
import com.peanut.modules.oss.controller.PutObjectProgressListener;
import com.peanut.modules.oss.service.OssService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@@ -17,6 +21,9 @@ import java.util.UUID;
@Service
public class OssServiceImpl implements OssService {
@Autowired
private StringRedisTemplate redisTemplate;
//上传头像到oss
@Override
public String uploadFileAvatar(MultipartFile file) {
@@ -35,6 +42,7 @@ public class OssServiceImpl implements OssService {
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
String fileNameProgress = file.getOriginalFilename();
//1 在文件名称里面添加随机唯一的值
String uuid = UUID.randomUUID().toString().replaceAll("-","");
// yuy76t5rew01.jpg
@@ -52,7 +60,9 @@ public class OssServiceImpl implements OssService {
//第一个参数 Bucket名称
//第二个参数 上传到oss文件路径和文件名称 aa/bb/1.jpg
//第三个参数 上传文件输入流
ossClient.putObject(bucketName,fileName , inputStream);
long fileSize = file.getSize();
ossClient.putObject(new PutObjectRequest(bucketName,fileName, inputStream).
withProgressListener(new PutObjectProgressListener(fileSize,fileNameProgress)));
// 关闭OSSClient。
ossClient.shutdown();
@@ -120,4 +130,13 @@ public class OssServiceImpl implements OssService {
return null;
}
}
@Override
public String getSchedule(String fileName) {
String percent = redisTemplate.opsForValue().get(fileName);
if ("100".equals(percent)){
redisTemplate.delete(fileName);
}
return percent==null?"":percent;
}
}