添加文件上传进度条
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.peanut.modules.oss.service;
|
||||
|
||||
import com.peanut.common.utils.R;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
public interface OssService {
|
||||
@@ -10,5 +11,5 @@ public interface OssService {
|
||||
|
||||
String uploadHtml(String html,String name);
|
||||
|
||||
String getSchedule(String uid);
|
||||
R getSchedule(String uid);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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.common.utils.R;
|
||||
import com.peanut.modules.oss.controller.PutObjectProgressListener;
|
||||
import com.peanut.modules.oss.service.OssService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -26,63 +27,20 @@ public class OssServiceImpl implements OssService {
|
||||
|
||||
@Override
|
||||
public String uploadFileAvatar(MultipartFile file) {
|
||||
// 工具类获取值
|
||||
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
|
||||
//第三个参数 上传文件输入流
|
||||
ossClient.putObject(bucketName,fileName, inputStream);
|
||||
|
||||
// 关闭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;
|
||||
}
|
||||
return uploadFile(file,"");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadFileSchedule(MultipartFile file,String uid) {
|
||||
return uploadFile(file,uid);
|
||||
}
|
||||
|
||||
public String uploadFile(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);
|
||||
@@ -92,27 +50,25 @@ public class OssServiceImpl implements OssService {
|
||||
String fileName = file.getOriginalFilename();
|
||||
//1 在文件名称里面添加随机唯一的值
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-","");
|
||||
// yuy76t5rew01.jpg
|
||||
fileName = uuid+fileName;
|
||||
|
||||
//2 把文件按照日期进行分类
|
||||
// 2019/11/12
|
||||
//2 把文件按照日期进行分类 拼接 2019/11/12/ewtqr313401.jpg
|
||||
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路径手动拼接出来
|
||||
//把上传之后文件路径返回 需要把上传到阿里云oss路径手动拼接出来
|
||||
// https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpg
|
||||
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
|
||||
//调用oss方法实现上传
|
||||
if ("".equals(uid)){
|
||||
ossClient.putObject(bucketName,fileName, inputStream);
|
||||
}else {
|
||||
//带进度条
|
||||
long fileSize = file.getSize();
|
||||
System.out.println(uid+ "" +url);
|
||||
ossClient.putObject(new PutObjectRequest(bucketName,fileName, inputStream).
|
||||
withProgressListener(new PutObjectProgressListener(fileSize,uid,url)));
|
||||
}
|
||||
// 关闭OSSClient。
|
||||
ossClient.shutdown();
|
||||
return url;
|
||||
}catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -127,44 +83,21 @@ public class OssServiceImpl implements OssService {
|
||||
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;
|
||||
InputStream inputStream = new ByteArrayInputStream(html.getBytes());
|
||||
String fileName = name;
|
||||
//2 把文件按照日期进行分类
|
||||
//获取当前日期
|
||||
// 2019/11/12
|
||||
//2 把文件按照日期进行分类 获取当前日期 2019/11/12/ewtqr313401.jpg
|
||||
String datePath = new DateTime().toString("yyyy/MM/dd");
|
||||
//拼接
|
||||
// 2019/11/12/ewtqr313401.jpg
|
||||
fileName = datePath+"/"+fileName;
|
||||
|
||||
//调用oss方法实现上传
|
||||
//第一个参数 Bucket名称
|
||||
//第二个参数 上传到oss文件路径和文件名称 aa/bb/1.jpg
|
||||
//第三个参数 上传文件输入流
|
||||
ossClient.putObject(bucketName,fileName , inputStream);
|
||||
|
||||
// 关闭OSSClient。
|
||||
ossClient.shutdown();
|
||||
|
||||
//把上传之后文件路径返回
|
||||
//需要把上传到阿里云oss路径手动拼接出来
|
||||
// https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpg
|
||||
//把上传之后文件路径返回 需要把上传到阿里云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();
|
||||
@@ -173,8 +106,16 @@ public class OssServiceImpl implements OssService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSchedule(String uid) {
|
||||
String percent = redisTemplate.opsForValue().get(uid);
|
||||
return percent==null?"":percent;
|
||||
public R getSchedule(String uid) {
|
||||
R result = new R();
|
||||
result.put("percent","");
|
||||
result.put("url","");
|
||||
String str = redisTemplate.opsForValue().get(uid);
|
||||
if (str!=null&&str.contains(";")){
|
||||
String[] strs = str.split(";");
|
||||
result.put("percent",strs[0]);
|
||||
result.put("url",strs[1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user