添加文件上传进度
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -6,7 +6,9 @@ public interface OssService {
|
||||
//上传头像到oss
|
||||
String uploadFileAvatar(MultipartFile file);
|
||||
|
||||
String uploadFileSchedule(MultipartFile file,String uid);
|
||||
|
||||
String uploadHtml(String html,String name);
|
||||
|
||||
String getSchedule(String fileName);
|
||||
String getSchedule(String uid);
|
||||
}
|
||||
|
||||
@@ -24,7 +24,6 @@ public class OssServiceImpl implements OssService {
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
//上传头像到oss
|
||||
@Override
|
||||
public String uploadFileAvatar(MultipartFile file) {
|
||||
// 工具类获取值
|
||||
@@ -35,14 +34,13 @@ public class OssServiceImpl implements OssService {
|
||||
|
||||
try {
|
||||
// 创建OSS实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
|
||||
|
||||
//获取上传文件输入流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
//获取文件名称
|
||||
String fileName = file.getOriginalFilename();
|
||||
String fileNameProgress = file.getOriginalFilename();
|
||||
//1 在文件名称里面添加随机唯一的值
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
||||
// yuy76t5rew01.jpg
|
||||
@@ -60,9 +58,7 @@ public class OssServiceImpl implements OssService {
|
||||
//第一个参数 Bucket名称
|
||||
//第二个参数 上传到oss文件路径和文件名称 aa/bb/1.jpg
|
||||
//第三个参数 上传文件输入流
|
||||
long fileSize = file.getSize();
|
||||
ossClient.putObject(new PutObjectRequest(bucketName,fileName, inputStream).
|
||||
withProgressListener(new PutObjectProgressListener(fileSize,fileNameProgress)));
|
||||
ossClient.putObject(bucketName,fileName, inputStream);
|
||||
|
||||
// 关闭OSSClient。
|
||||
ossClient.shutdown();
|
||||
@@ -79,6 +75,51 @@ public class OssServiceImpl implements OssService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFileSchedule(MultipartFile file,String uid) {
|
||||
// 工具类获取值
|
||||
String endpoint = ConstantPropertiesUtils.END_POIND;
|
||||
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
|
||||
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
|
||||
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
|
||||
|
||||
try {
|
||||
// 创建OSS实例。
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
||||
//获取上传文件输入流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
//获取文件名称
|
||||
String fileName = file.getOriginalFilename();
|
||||
//1 在文件名称里面添加随机唯一的值
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
||||
// yuy76t5rew01.jpg
|
||||
fileName = uuid+fileName;
|
||||
|
||||
//2 把文件按照日期进行分类
|
||||
// 2019/11/12
|
||||
String datePath = new DateTime().toString("yyyy/MM/dd");
|
||||
//拼接 2019/11/12/ewtqr313401.jpg
|
||||
fileName = datePath+"/"+fileName;
|
||||
//调用oss方法实现上传
|
||||
//第一个参数 Bucket名称
|
||||
//第二个参数 上传到oss文件路径和文件名称 aa/bb/1.jpg
|
||||
//第三个参数 上传文件输入流
|
||||
long fileSize = file.getSize();
|
||||
ossClient.putObject(new PutObjectRequest(bucketName,fileName, inputStream).
|
||||
withProgressListener(new PutObjectProgressListener(fileSize,uid)));
|
||||
// 关闭OSSClient。
|
||||
ossClient.shutdown();
|
||||
//把上传之后文件路径返回
|
||||
//需要把上传到阿里云oss路径手动拼接出来
|
||||
// https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpg
|
||||
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
|
||||
return url;
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadHtml(String html, String name) {
|
||||
// 工具类获取值
|
||||
@@ -132,10 +173,10 @@ public class OssServiceImpl implements OssService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSchedule(String fileName) {
|
||||
String percent = redisTemplate.opsForValue().get(fileName);
|
||||
public String getSchedule(String uid) {
|
||||
String percent = redisTemplate.opsForValue().get(uid);
|
||||
if ("100".equals(percent)){
|
||||
redisTemplate.delete(fileName);
|
||||
redisTemplate.delete(uid);
|
||||
}
|
||||
return percent==null?"":percent;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user