|
@@ -1,5 +1,6 @@
|
|
|
package com.slibra.framework.web.service;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
import com.qiniu.common.QiniuException;
|
|
|
import com.qiniu.common.Zone;
|
|
|
import com.qiniu.http.Response;
|
|
@@ -9,24 +10,34 @@ import com.qiniu.storage.UploadManager;
|
|
|
import com.qiniu.util.Auth;
|
|
|
import com.slibra.common.config.QiNiuYunProperties;
|
|
|
import com.slibra.common.constant.MyConstants;
|
|
|
+import com.slibra.common.core.redis.RedisCache;
|
|
|
+import com.slibra.common.utils.StringUtils;
|
|
|
import com.slibra.common.utils.third.StringUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+import static com.slibra.common.constant.CacheConstants.QINIUYUN_UPLOAD_KEY;
|
|
|
+import static com.slibra.common.constant.CacheConstants.QINIUYUN_UPLOAD_KEY_TIME;
|
|
|
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
+//@DependsOn("redisCache")
|
|
|
public class QiNiuYunServiceImpl {
|
|
|
|
|
|
@Autowired
|
|
|
private QiNiuYunProperties qiNiuYunProperties;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
// 七牛文件上传管理器
|
|
|
private UploadManager uploadManager;
|
|
|
//上传的token
|
|
@@ -48,7 +59,8 @@ public class QiNiuYunServiceImpl {
|
|
|
auth = Auth.create(qiNiuYunProperties.getAccessKey(), qiNiuYunProperties.getSecretKey());
|
|
|
// 根据命名空间生成的上传token
|
|
|
bucketManager = new BucketManager(auth, new Configuration(Zone.zone1()));
|
|
|
- token = auth.uploadToken(qiNiuYunProperties.getBucketName());
|
|
|
+// token = auth.uploadToken(qiNiuYunProperties.getBucketName());
|
|
|
+// redisCache.setCacheObject(QINIUYUN_UPLOAD_KEY, token, QINIUYUN_UPLOAD_KEY_TIME, TimeUnit.SECONDS);
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -57,30 +69,51 @@ public class QiNiuYunServiceImpl {
|
|
|
* @return java.lang.String
|
|
|
**/
|
|
|
public String uploadQNImg(MultipartFile file) {
|
|
|
+ FileInputStream inputStream = null;
|
|
|
try {
|
|
|
// 获取文件的名称
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
-
|
|
|
+// log.info("获取到的文件的名字是{}", fileName);
|
|
|
// 使用工具类根据上传文件生成唯一图片名称
|
|
|
String imgName = MyConstants.QI_NIU_YUN_UPLOAD_URL + StringUtil.getRandomImgName(fileName);
|
|
|
-
|
|
|
- FileInputStream inputStream = (FileInputStream) file.getInputStream();
|
|
|
+// log.info("处理以后到的文件的名字是{}", imgName);
|
|
|
+ inputStream = (FileInputStream) file.getInputStream();
|
|
|
// 上传图片文件
|
|
|
+ //token默认是3600s Redis缓存处理
|
|
|
+ token = redisCache.getCacheObject(QINIUYUN_UPLOAD_KEY);
|
|
|
+ if(StringUtils.isBlank(token)){
|
|
|
+ log.info("七牛云缓存的token过期了或者是第一次初始化");
|
|
|
+ token = auth.uploadToken(qiNiuYunProperties.getBucketName());
|
|
|
+ redisCache.setCacheObject(QINIUYUN_UPLOAD_KEY, token, QINIUYUN_UPLOAD_KEY_TIME, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
Response res = uploadManager.put(inputStream, imgName, token, null, null);
|
|
|
+// log.info("上传接口返回的结果是{}", JSON.toJSONString(res));
|
|
|
if (!res.isOK()) {
|
|
|
- throw new RuntimeException("上传七牛出错:" + res.toString());
|
|
|
+ throw new RuntimeException("上传七牛出错,返回结果是:" + JSON.toJSONString(res));
|
|
|
}
|
|
|
// 解析上传成功的结果
|
|
|
// DefaultPutRet putRet = new Gson().fromJson(res.bodyString(), DefaultPutRet.class);
|
|
|
|
|
|
// 直接返回外链地址
|
|
|
- return getPrivateFile(imgName);
|
|
|
+ return getPublicName(imgName);
|
|
|
} catch (QiniuException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ //这里有个bug e.printStackTrace() 不打印异常信息,假如遇到token过期了,但是不会抛出异常
|
|
|
+ log.error("上次异常,异常信息为{}", e.getMessage());
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("上次异常,异常信息为{}", e.getMessage());
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
- return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getPublicName(String imgName) {
|
|
|
+ return String.format("%s/%s", this.qiNiuYunProperties.getUrl(), imgName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -88,19 +121,22 @@ public class QiNiuYunServiceImpl {
|
|
|
*
|
|
|
* @param fileKey
|
|
|
* @return
|
|
|
+ * 2023年12月8日13:50:33 做为公共链接返回
|
|
|
*/
|
|
|
public String getPrivateFile(String fileKey) {
|
|
|
String encodedFileName = null;
|
|
|
- String finalUrl = null;
|
|
|
+// String finalUrl = null;
|
|
|
try {
|
|
|
encodedFileName = URLEncoder.encode(fileKey, "utf-8").replace("+", "%20");
|
|
|
- String publicUrl = String.format("%s/%s", this.qiNiuYunProperties.getUrl(), encodedFileName);
|
|
|
- long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
|
|
|
- finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
|
|
|
+ return String.format("%s/%s", this.qiNiuYunProperties.getUrl(), encodedFileName);
|
|
|
+// String publicUrl = String.format("%s/%s", this.qiNiuYunProperties.getUrl(), encodedFileName);
|
|
|
+// long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
|
|
|
+// finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- return finalUrl;
|
|
|
+// return finalUrl;
|
|
|
+ return "";
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -117,4 +153,18 @@ public class QiNiuYunServiceImpl {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
+ String str = URLEncoder.encode("abc/ddd", "utf-8").replace("+", "%20");
|
|
|
+ System.out.println(String.format("%s/%s", "https://static.fuxicarbon.com", str));
|
|
|
+ System.out.println(String.format("%s/%s", "https://static.fuxicarbon.com", "abc/ddd"));
|
|
|
+
|
|
|
+ try {
|
|
|
+ int i = 5/0;
|
|
|
+ } catch (Exception e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|