修复图片上传OSS路径缺失问题
This commit is contained in:
parent
e4e70cc65c
commit
2c2b77fcc3
@ -183,4 +183,17 @@ public class FileUtils extends org.apache.commons.io.FileUtils
|
|||||||
}
|
}
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件类型
|
||||||
|
* @param fileUrl 文件名称
|
||||||
|
* @param prefixFlag 是否带连接符 .
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getFileType(String fileUrl, boolean prefixFlag){
|
||||||
|
if(fileUrl == null || fileUrl.lastIndexOf(".") == -1){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return fileUrl.substring(fileUrl.lastIndexOf(".")+(prefixFlag ? 0 : 1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,9 @@ public class AliyunOSSUtils {
|
|||||||
//下载链接默认有效期
|
//下载链接默认有效期
|
||||||
private static final Long expire = 3600L * 1000 * 24;
|
private static final Long expire = 3600L * 1000 * 24;
|
||||||
|
|
||||||
|
//默认文件路径前缀
|
||||||
|
private static final String default_prefix = "https://stdiet.oss-cn-shenzhen.aliyuncs.com";
|
||||||
|
|
||||||
public static OSS getOssClient() {
|
public static OSS getOssClient() {
|
||||||
return new OSSClientBuilder().build(AliyunOSSConfig.EndPoint, AliyunOSSConfig.AccessKeyID, AliyunOSSConfig.AccessKeySecret);
|
return new OSSClientBuilder().build(AliyunOSSConfig.EndPoint, AliyunOSSConfig.AccessKeyID, AliyunOSSConfig.AccessKeySecret);
|
||||||
}
|
}
|
||||||
@ -50,7 +53,8 @@ public class AliyunOSSUtils {
|
|||||||
* @return 文件URL
|
* @return 文件URL
|
||||||
*/
|
*/
|
||||||
private static String getRealName(String oranName) {
|
private static String getRealName(String oranName) {
|
||||||
return getURLHead() + oranName;
|
String fileUrl = getURLHead() + oranName;
|
||||||
|
return isAliyunUrl(fileUrl) ? fileUrl : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -71,10 +75,10 @@ public class AliyunOSSUtils {
|
|||||||
//去尾
|
//去尾
|
||||||
String tail = oranName.substring(cutPoint);
|
String tail = oranName.substring(cutPoint);
|
||||||
//返回正确的带路径的图片名称
|
//返回正确的带路径的图片名称
|
||||||
return prefix + head + uuid + "_" + tail;
|
return prefix + head + uuid + FileUtils.getFileType(tail, true);
|
||||||
}
|
}
|
||||||
//不存在 直接返回
|
//不存在 直接返回
|
||||||
return prefix + uuid + "_" + oranName;
|
return prefix + uuid + FileUtils.getFileType(oranName, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -231,7 +235,7 @@ public class AliyunOSSUtils {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String generatePresignedUrl(String fileUrl){
|
public static String generatePresignedUrl(String fileUrl){
|
||||||
if(StringUtils.isEmpty(fileUrl)) {
|
if(!isAliyunUrl(fileUrl)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// 创建OSSClient实例。
|
// 创建OSSClient实例。
|
||||||
@ -261,8 +265,10 @@ public class AliyunOSSUtils {
|
|||||||
Date expiration = new Date(System.currentTimeMillis() + expire);
|
Date expiration = new Date(System.currentTimeMillis() + expire);
|
||||||
|
|
||||||
for (String fileUrl : fileUrlList) {
|
for (String fileUrl : fileUrlList) {
|
||||||
String url = ossClient.generatePresignedUrl(AliyunOSSConfig.Buckets, getObjectName(fileUrl), expiration).toString();
|
if(isAliyunUrl(fileUrl)){
|
||||||
downUrlList.add(url);
|
String url = ossClient.generatePresignedUrl(AliyunOSSConfig.Buckets, getObjectName(fileUrl), expiration).toString();
|
||||||
|
downUrlList.add(url);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关闭OSSClient。
|
// 关闭OSSClient。
|
||||||
@ -271,6 +277,15 @@ public class AliyunOSSUtils {
|
|||||||
return downUrlList;
|
return downUrlList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断是否为阿里云OSS路径格式
|
||||||
|
* @param fileUrl
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isAliyunUrl(String fileUrl){
|
||||||
|
return StringUtils.isNotEmpty(fileUrl) && fileUrl.startsWith(default_prefix);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param fileUrlList
|
* @param fileUrlList
|
||||||
@ -288,7 +303,9 @@ public class AliyunOSSUtils {
|
|||||||
List<String> urlList = fileUrlList.get(key);
|
List<String> urlList = fileUrlList.get(key);
|
||||||
List<String> downList = new ArrayList<>();
|
List<String> downList = new ArrayList<>();
|
||||||
for (String fileUrl : urlList) {
|
for (String fileUrl : urlList) {
|
||||||
downList.add(ossClient.generatePresignedUrl(AliyunOSSConfig.Buckets, getObjectName(fileUrl), expiration).toString());
|
if(isAliyunUrl(fileUrl)){
|
||||||
|
downList.add(ossClient.generatePresignedUrl(AliyunOSSConfig.Buckets, getObjectName(fileUrl), expiration).toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
downUrlMap.put(key, downList);
|
downUrlMap.put(key, downList);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package com.stdiet.custom.utils;
|
package com.stdiet.custom.utils;
|
||||||
|
|
||||||
import com.stdiet.common.utils.StringUtils;
|
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.index.*;
|
import org.apache.lucene.index.*;
|
||||||
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
|
|
||||||
import org.apache.lucene.search.*;
|
import org.apache.lucene.search.*;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.store.FSDirectory;
|
import org.apache.lucene.store.FSDirectory;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user