Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐少华
midjourney-proxy
Commits
9c92af97
Commit
9c92af97
authored
May 10, 2019
by
songxiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
OSS增加图片裁剪方法同时持久化
parent
0a626b44
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
439 additions
and
324 deletions
+439
-324
FileUtils.java
...mmon/src/main/java/com/pcloud/common/utils/FileUtils.java
+23
-12
ImageUtils.java
...mon/src/main/java/com/pcloud/common/utils/ImageUtils.java
+125
-10
OssUtils.java
...rc/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
+291
-302
No files found.
pcloud-common/src/main/java/com/pcloud/common/utils/FileUtils.java
View file @
9c92af97
...
@@ -468,7 +468,8 @@ public class FileUtils {
...
@@ -468,7 +468,8 @@ public class FileUtils {
/**
/**
* 根据路径删除指定的目录或文件,无论存在与否
* 根据路径删除指定的目录或文件,无论存在与否
*
*
* @param sPath 要删除的目录或文件
* @param sPath
* 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
* @return 删除成功返回 true,否则返回 false。
*/
*/
public
static
boolean
deleteFolder
(
String
sPath
)
{
public
static
boolean
deleteFolder
(
String
sPath
)
{
...
@@ -493,7 +494,8 @@ public class FileUtils {
...
@@ -493,7 +494,8 @@ public class FileUtils {
/**
/**
* 删除单个文件
* 删除单个文件
*
*
* @param sPath 被删除文件的文件名
* @param sPath
* 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
* @return 单个文件删除成功返回true,否则返回false
*/
*/
public
static
boolean
deleteFile
(
String
sPath
)
{
public
static
boolean
deleteFile
(
String
sPath
)
{
...
@@ -513,7 +515,8 @@ public class FileUtils {
...
@@ -513,7 +515,8 @@ public class FileUtils {
/**
/**
* 删除目录(文件夹)以及目录下的文件
* 删除目录(文件夹)以及目录下的文件
*
*
* @param sPath 被删除目录的文件路径
* @param sPath
* 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
* @return 目录删除成功返回true,否则返回false
*/
*/
public
static
boolean
deleteDirectory
(
String
sPath
)
{
public
static
boolean
deleteDirectory
(
String
sPath
)
{
...
@@ -687,8 +690,10 @@ public class FileUtils {
...
@@ -687,8 +690,10 @@ public class FileUtils {
/**
/**
* 合并两个文件
* 合并两个文件
*
*
* @param outFile 目标文件
* @param outFile
* @param leafFile 源文件
* 目标文件
* @param leafFile
* 源文件
*/
*/
public
static
void
mergeFiles
(
File
outFile
,
File
leafFile
)
{
public
static
void
mergeFiles
(
File
outFile
,
File
leafFile
)
{
FileOutputStream
fos
=
null
;
FileOutputStream
fos
=
null
;
...
@@ -705,7 +710,7 @@ public class FileUtils {
...
@@ -705,7 +710,7 @@ public class FileUtils {
fos
=
new
FileOutputStream
(
outFile
,
true
);
fos
=
new
FileOutputStream
(
outFile
,
true
);
fis
=
new
FileInputStream
(
leafFile
);
fis
=
new
FileInputStream
(
leafFile
);
int
len
=
0
;
int
len
=
0
;
for
(
byte
[]
buf
=
new
byte
[
1024
*
1024
];
(
len
=
fis
.
read
(
buf
))
!=
-
1
;
)
{
for
(
byte
[]
buf
=
new
byte
[
1024
*
1024
];
(
len
=
fis
.
read
(
buf
))
!=
-
1
;
)
{
fos
.
write
(
buf
,
0
,
len
);
fos
.
write
(
buf
,
0
,
len
);
}
}
}
catch
(
IOException
ioe
)
{
}
catch
(
IOException
ioe
)
{
...
@@ -724,8 +729,10 @@ public class FileUtils {
...
@@ -724,8 +729,10 @@ public class FileUtils {
/**
/**
* 合并多个文件
* 合并多个文件
*
*
* @param outFile 输出文件,
* @param outFile
* @param leafFiles 文件碎片集
* 输出文件,
* @param leafFiles
* 文件碎片集
*/
*/
public
static
void
mergeFileList
(
File
outFile
,
List
<
File
>
leafFiles
)
{
public
static
void
mergeFileList
(
File
outFile
,
List
<
File
>
leafFiles
)
{
FileOutputStream
fos
=
null
;
FileOutputStream
fos
=
null
;
...
@@ -764,9 +771,12 @@ public class FileUtils {
...
@@ -764,9 +771,12 @@ public class FileUtils {
/**
/**
* 音频文件转换
* 音频文件转换
*
*
* @param data 音频byte数组
* @param data
* @param sourExt 原始后缀
* 音频byte数组
* @param ext 新后缀
* @param sourExt
* 原始后缀
* @param ext
* 新后缀
* @return
* @return
* @author PENG
* @author PENG
*/
*/
...
@@ -1093,7 +1103,8 @@ public class FileUtils {
...
@@ -1093,7 +1103,8 @@ public class FileUtils {
/**
/**
* 从输入流中获取数据
* 从输入流中获取数据
*
*
* @param inStream 输入流
* @param inStream
* 输入流
* @return
* @return
* @throws Exception
* @throws Exception
*/
*/
...
...
pcloud-common/src/main/java/com/pcloud/common/utils/ImageUtils.java
View file @
9c92af97
...
@@ -11,10 +11,11 @@ import java.math.BigDecimal;
...
@@ -11,10 +11,11 @@ import java.math.BigDecimal;
import
javax.imageio.ImageIO
;
import
javax.imageio.ImageIO
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
com.dcg.util.StringUtils
;
import
com.itextpdf.text.Image
;
import
com.itextpdf.text.Image
;
import
com.pcloud.common.constant.AliyunConstant
;
import
com.pcloud.common.constant.AliyunConstant
;
import
com.pcloud.common.constant.FilePathConst
;
import
com.pcloud.common.constant.FilePathConst
;
...
@@ -440,6 +441,43 @@ public class ImageUtils {
...
@@ -440,6 +441,43 @@ public class ImageUtils {
}
}
/**
/**
* 获取图片的宽度和高度(考虑了图片旋转的情况。也就是说,获得的宽就是宽,高就是高)
*
* @param img
* 图片文件
* @return
*/
public
static
float
[]
getWidthHeightSize
(
String
fileUrl
)
{
Image
image
=
null
;
String
localFile
=
null
;
float
[]
size
=
new
float
[
2
];
try
{
if
(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
OSS_CDN_URLS
))
{
localFile
=
OssUtils
.
imageAutoOrient
(
fileUrl
,
0
);
image
=
Image
.
getInstance
(
localFile
);
}
else
if
(
fileUrl
.
startsWith
(
"http"
))
{
image
=
Image
.
getInstance
(
FileUtils
.
downloadByteFromUrl
(
fileUrl
));
}
else
{
image
=
Image
.
getInstance
(
fileUrl
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【IMAGE API】获取图片的宽度和高度[getWidthHeightSize]:"
+
e
.
getMessage
(),
e
);
}
finally
{
if
(!
StringUtil
.
isEmpty
(
localFile
))
{
FileUtils
.
deleteFile
(
localFile
);
}
}
if
(
image
==
null
)
{
size
[
0
]
=
0
;
size
[
1
]
=
0
;
}
else
{
size
[
0
]
=
image
.
getWidth
();
size
[
1
]
=
image
.
getHeight
();
}
return
size
;
}
/**
* 获取对比比例
* 获取对比比例
*
*
* @param num1
* @param num1
...
@@ -655,27 +693,28 @@ public class ImageUtils {
...
@@ -655,27 +693,28 @@ public class ImageUtils {
}
}
/**
/**
* 获取图片的宽度和高度
* 获取图片的宽度和高度
(未考虑图片旋转的情况。也就是说,获得的宽可能是宽也可能是高,高可能是高也肯是宽)
*
*
* @param img
* @param fileUrl
* 图片文件
* @return
* @return
*/
*/
public
static
float
[]
getWidthHeight
Size
(
String
fileUrl
)
{
public
static
float
[]
getWidthHeight
NoOrient
(
String
fileUrl
)
{
Image
image
=
null
;
Image
image
=
null
;
String
localFile
=
null
;
String
localFile
=
null
;
float
[]
size
=
new
float
[
2
];
float
[]
size
=
new
float
[
2
];
try
{
try
{
if
(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
OSS_CDN_URLS
))
{
/*
localFile
=
OssUtils
.
imageAutoOrient
(
fileUrl
,
0
);
* if (StringTools.contains(fileUrl, AliyunConstant.OSS_CDN_URLS)) { localFile =
image
=
Image
.
getInstance
(
localFile
);
* OssUtils.imageAutoOrient(fileUrl); image = Image.getInstance(localFile); }
}
else
if
(
fileUrl
.
startsWith
(
"http"
))
{
* else
*/
if
(
fileUrl
.
startsWith
(
"http"
))
{
image
=
Image
.
getInstance
(
FileUtils
.
downloadByteFromUrl
(
fileUrl
));
image
=
Image
.
getInstance
(
FileUtils
.
downloadByteFromUrl
(
fileUrl
));
}
else
{
}
else
{
image
=
Image
.
getInstance
(
fileUrl
);
image
=
Image
.
getInstance
(
fileUrl
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【IMAGE API】获取图片的宽度和高度:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【IMAGE API】获取图片的宽度和高度
[getWidthHeightNoOrient]
:"
+
e
.
getMessage
(),
e
);
}
finally
{
}
finally
{
if
(!
StringUtil
.
isEmpty
(
localFile
))
{
if
(!
StringUtil
.
isEmpty
(
localFile
))
{
FileUtils
.
deleteFile
(
localFile
);
FileUtils
.
deleteFile
(
localFile
);
...
@@ -692,6 +731,82 @@ public class ImageUtils {
...
@@ -692,6 +731,82 @@ public class ImageUtils {
}
}
/**
/**
* image transcode to webp
*
* 注意:使用此方法转图片格式,机器上必须安装有谷歌cwebp工具
*
* @param fileUrl
* @return
*/
// public static UploadResultInfo transcodeToWebp(String fileUrl, int quality) {
// LOGGER.info("【IMAGE API】image transcode to webp.<START>.[fileUrl]=" + fileUrl
// + ",[quality]=" + quality);
// String fileNameAll = FileUtils.getFileNameAll(fileUrl);
// String localFilePath = FilePathConst.DOWNLOAD_PATH + fileNameAll;
// FileUtils.downloadFileFromUrl(fileUrl, localFilePath);
// String outputFilePath = FilePathConst.IMAGE_PATH + "webp/" + fileNameAll +
// ".webp";
// FileUtils.creatFiles(outputFilePath);
// UploadResultInfo uploadResultInfo = null;
// try {
// String os = System.getProperty("os.name");
// if (os.toLowerCase().startsWith("win")) {
// executeCwebp4Win(localFilePath, outputFilePath, quality);
// } else {
// executeCwebp4Linux(localFilePath, outputFilePath, quality);
// }
// uploadResultInfo = OssUtils.uploadLocalFile4Child(outputFilePath, fileUrl);
// } catch (Exception e) {
// LOGGER.error("An error happend when convert to webp. Img is: " +
// e.getMessage(), e);
// throw new FileException(FileException.FILE_CONVERT_FAIL, "transcode to webp
// is fail!");
// } finally {
// FileUtils.deleteFile(localFilePath);
// FileUtils.deleteFile(outputFilePath);
// }
// LOGGER.info("【IMAGE API】image transcode to webp.<START>.[uploadResultInfo]="
// + uploadResultInfo);
// return uploadResultInfo;
// }
/**
* execute cwebp command:cwebp [options] input_file -o output_file.webp
*
* @param inputFilePath
* @param outputFilePath
* @param quality
* @throws Exception
*/
// private static void executeCwebp4Win(String inputFilePath, String
// outputFilePath, int quality) throws Exception {
// Process process = new ProcessBuilder("cwebp", "-q", (quality == 0 ? 80 :
// quality) + "", inputFilePath, "-o",
// outputFilePath).redirectErrorStream(true).start();
// if (0 != process.waitFor()) {
// throw new Exception("process wait for fail!");
// }
// }
/**
* execute cwebp command:cwebp [options] input_file -o output_file.webp
*
* @param inputFilePath
* @param outputFilePath
* @param quality
* @throws Exception
*/
// private static void executeCwebp4Linux(String inputFilePath, String
// outputFilePath, int quality) throws Exception {
// Process process = new ProcessBuilder("/usr/local/bin/cwebp", "-q", (quality
// == 0 ? 80 : quality) + "",
// inputFilePath, "-o", outputFilePath).redirectErrorStream(true).start();
// if (0 != process.waitFor()) {
// throw new Exception("process wait for fail!");
// }
// }
/**
* 上传的图片转换成webpO
* 上传的图片转换成webpO
*
*
* @param localFilePath
* @param localFilePath
...
...
pcloud-common/src/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
View file @
9c92af97
package
com
.
pcloud
.
common
.
utils
.
aliyun
;
package
com
.
pcloud
.
common
.
utils
.
aliyun
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLDecoder
;
import
java.net.URLEncoder
;
import
java.util.List
;
import
org.apache.commons.codec.binary.Base64
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.aliyun.oss.OSSClient
;
import
com.aliyun.oss.OSSClient
;
import
com.aliyun.oss.common.utils.BinaryUtil
;
import
com.aliyun.oss.common.utils.IOUtils
;
import
com.aliyun.oss.model.GenericResult
;
import
com.aliyun.oss.model.GetObjectRequest
;
import
com.aliyun.oss.model.GetObjectRequest
;
import
com.aliyun.oss.model.OSSObject
;
import
com.aliyun.oss.model.OSSObject
;
import
com.aliyun.oss.model.ObjectMetadata
;
import
com.aliyun.oss.model.ObjectMetadata
;
import
com.aliyun.oss.model.ProcessObjectRequest
;
import
com.aliyun.oss.model.UploadFileRequest
;
import
com.aliyun.oss.model.UploadFileRequest
;
import
com.aliyuncs.DefaultAcsClient
;
import
com.aliyuncs.DefaultAcsClient
;
import
com.aliyuncs.mts.model.v20140618.QueryJobListRequest
;
import
com.aliyuncs.mts.model.v20140618.QueryJobListRequest
;
...
@@ -38,6 +29,8 @@ import com.aliyuncs.profile.DefaultProfile;
...
@@ -38,6 +29,8 @@ import com.aliyuncs.profile.DefaultProfile;
import
com.pcloud.common.constant.AliyunConstant
;
import
com.pcloud.common.constant.AliyunConstant
;
import
com.pcloud.common.constant.FilePathConst
;
import
com.pcloud.common.constant.FilePathConst
;
import
com.pcloud.common.constant.UrlConstant
;
import
com.pcloud.common.constant.UrlConstant
;
import
com.pcloud.common.dto.OssImageCropDTO
;
import
com.pcloud.common.dto.OssImageDTO
;
import
com.pcloud.common.dto.OssMediaInfoDTO
;
import
com.pcloud.common.dto.OssMediaInfoDTO
;
import
com.pcloud.common.entity.OSSFileDO
;
import
com.pcloud.common.entity.OSSFileDO
;
import
com.pcloud.common.entity.UploadResultInfo
;
import
com.pcloud.common.entity.UploadResultInfo
;
...
@@ -57,9 +50,21 @@ import com.pcloud.common.utils.rsa.MD5;
...
@@ -57,9 +50,21 @@ import com.pcloud.common.utils.rsa.MD5;
import
com.pcloud.common.utils.string.StringTools
;
import
com.pcloud.common.utils.string.StringTools
;
import
com.pcloud.common.utils.string.StringUtil
;
import
com.pcloud.common.utils.string.StringUtil
;
import
org.apache.commons.codec.binary.Base64
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLDecoder
;
import
java.net.URLEncoder
;
import
java.util.List
;
/**
/**
* @描述:
* @描述:
*
* @作者:songx
* @作者:songx
* @创建时间:2017年6月26日,下午12:03:44 @版本:1.0
* @创建时间:2017年6月26日,下午12:03:44 @版本:1.0
*/
*/
...
@@ -121,10 +126,8 @@ public class OssUtils {
...
@@ -121,10 +126,8 @@ public class OssUtils {
/**
/**
* 本地上传文件到aliOSS,isFlag=true,表示上传成功
* 本地上传文件到aliOSS,isFlag=true,表示上传成功
*
*
* @param filePath
* @param filePath 源文件物理地址
* 源文件物理地址
* @param fileName 源文件名称,可为null
* @param fileName
* 源文件名称,可为null
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -133,19 +136,16 @@ public class OssUtils {
...
@@ -133,19 +136,16 @@ public class OssUtils {
}
}
/**
/**
* 本地上传文件到aliOSS,isFlag=true,表示上传成功 <br>
* 本地上传文件到aliOSS,isFlag=true,表示上传成功 <br> 自定义上传后的文件名称
* 自定义上传后的文件名称
*
*
* @param filePath
* @param filePath 源文件物理地址
* 源文件物理地址
* @param fileName 源文件名称,可为null
* @param fileName
* 源文件名称,可为null
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
public
static
UploadResultInfo
uploadLocalFile4CustomName
(
String
filePath
,
String
fileName
)
throws
FileException
{
public
static
UploadResultInfo
uploadLocalFile4CustomName
(
String
filePath
,
String
fileName
)
throws
FileException
{
String
fileType
=
FileUtils
.
getFileType
(
filePath
);
String
fileType
=
FileUtils
.
getFileType
(
filePath
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
UPLOAD
.
value
,
fileName
,
fileType
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
UPLOAD
,
fileName
,
fileType
);
uploadPointFile2OSS
(
filePath
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
uploadPointFile2OSS
(
filePath
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
UploadResultInfo
uploadResultInfo
=
getResultInfo
(
outObjectKey
,
new
File
(
filePath
).
length
(),
null
,
UploadResultInfo
uploadResultInfo
=
getResultInfo
(
outObjectKey
,
new
File
(
filePath
).
length
(),
null
,
AliyunConstant
.
FILE_BUCKET
);
AliyunConstant
.
FILE_BUCKET
);
...
@@ -159,10 +159,8 @@ public class OssUtils {
...
@@ -159,10 +159,8 @@ public class OssUtils {
/**
/**
* 父子文件上传,子文件全路径和父文件相同,文件后缀类型自定义
* 父子文件上传,子文件全路径和父文件相同,文件后缀类型自定义
*
*
* @param filePath
* @param filePath 子文件本地路径
* 子文件本地路径
* @param parentFileUrl 仅限OSS的全路径,其他本地文件路径均不支持
* @param parentFileUrl
* 仅限OSS的全路径,其他本地文件路径均不支持
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -176,12 +174,9 @@ public class OssUtils {
...
@@ -176,12 +174,9 @@ public class OssUtils {
/**
/**
* byte[]数组文件流上传 ,isFlag=true,表示上传成功
* byte[]数组文件流上传 ,isFlag=true,表示上传成功
*
*
* @param buff
* @param buff 源文件字节数组
* 源文件字节数组
* @param fileName 源文件名称不带后缀,可为null
* @param fileName
* @param fileType 源文件后缀
* 源文件名称不带后缀,可为null
* @param fileType
* 源文件后缀
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -190,21 +185,17 @@ public class OssUtils {
...
@@ -190,21 +185,17 @@ public class OssUtils {
}
}
/**
/**
* 文件流上传到aliOSS, isFlag=true,表示上传成功 <br/>
* 文件流上传到aliOSS, isFlag=true,表示上传成功 <br/> 慎用,该方法稳定性不确定,曾出现过上传的文件流丢失的情况
* 慎用,该方法稳定性不确定,曾出现过上传的文件流丢失的情况
*
*
* @param is
* @param is 源文件流
* 源文件流
* @param fileName 源文件名称不带后缀,可为null
* @param fileName
* @param fileType 源文件后缀
* 源文件名称不带后缀,可为null
* @param fileType
* 源文件后缀
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
public
static
UploadResultInfo
uploadFileStream
(
InputStream
is
,
String
fileName
,
String
fileType
)
public
static
UploadResultInfo
uploadFileStream
(
InputStream
is
,
String
fileName
,
String
fileType
)
throws
FileException
{
throws
FileException
{
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
UPLOAD
.
value
,
null
,
fileType
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
UPLOAD
,
null
,
fileType
);
uploadFileStream2OSS
(
is
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
uploadFileStream2OSS
(
is
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
UploadResultInfo
uploadResultInfo
=
getResultInfo
(
outObjectKey
,
null
,
null
,
AliyunConstant
.
FILE_BUCKET
);
UploadResultInfo
uploadResultInfo
=
getResultInfo
(
outObjectKey
,
null
,
null
,
AliyunConstant
.
FILE_BUCKET
);
// 如果上传的是图片转换成webp
// 如果上传的是图片转换成webp
...
@@ -248,7 +239,8 @@ public class OssUtils {
...
@@ -248,7 +239,8 @@ public class OssUtils {
* 断点续传上传文件到aliOSS
* 断点续传上传文件到aliOSS
*
*
* @param filePath
* @param filePath
* @param fileName
* @param bucketName
* @param objectKey
* @return
* @return
*/
*/
private
static
OSSFileDO
uploadPointFile2OSS
(
String
filePath
,
String
bucketName
,
String
objectKey
)
private
static
OSSFileDO
uploadPointFile2OSS
(
String
filePath
,
String
bucketName
,
String
objectKey
)
...
@@ -285,10 +277,8 @@ public class OssUtils {
...
@@ -285,10 +277,8 @@ public class OssUtils {
/**
/**
* 提交MP4转码作业
* 提交MP4转码作业
*
*
* @param fileName
* @param fileName 源文件的名称不带后缀,可为null
* 源文件的名称不带后缀,可为null
* @param filePath 源文件本地路径
* @param filePath
* 源文件本地路径
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -308,30 +298,27 @@ public class OssUtils {
...
@@ -308,30 +298,27 @@ public class OssUtils {
public
static
String
[]
submitTranscodeJobMp4Ex
(
String
fileName
,
String
filePath
)
throws
FileException
{
public
static
String
[]
submitTranscodeJobMp4Ex
(
String
fileName
,
String
filePath
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交MP4转码作业.[filePath]="
+
filePath
);
LOGGER
.
info
(
"【aliOSS】提交MP4转码作业.[filePath]="
+
filePath
);
String
fileType
=
FileUtils
.
getFileType
(
filePath
);
String
fileType
=
FileUtils
.
getFileType
(
filePath
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
fileName
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
fileType
);
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
OSSFileDO
ossFileDO
=
uploadPointFile2OSS
(
filePath
,
inputBucketName
,
inObjectKey
);
OSSFileDO
ossFileDO
=
uploadPointFile2OSS
(
filePath
,
inputBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
null
,
"mp4"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp4"
);
return
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
return
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
}
}
/**
/**
* 提交MP4转码作业,文件流方式
* 提交MP4转码作业,文件流方式
*
*
* @param buff
* @param buff 源文件流
* 源文件流
* @param fileName 源文件的名称不带后缀,可为null
* @param fileName
* @param fileType 源文件的后缀
* 源文件的名称不带后缀,可为null
* @param fileType
* 源文件的后缀
* @return
* @return
*/
*/
public
static
String
submitTranscodeJobMp4
(
byte
[]
buff
,
String
fileName
,
String
fileType
)
throws
FileException
{
public
static
String
submitTranscodeJobMp4
(
byte
[]
buff
,
String
fileName
,
String
fileType
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交MP4转码作业."
);
LOGGER
.
info
(
"【aliOSS】提交MP4转码作业."
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
fileName
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
fileType
);
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
inputBucketName
,
inObjectKey
);
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
inputBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
null
,
"mp4"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp4"
);
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
return
result
[
0
];
return
result
[
0
];
...
@@ -340,10 +327,8 @@ public class OssUtils {
...
@@ -340,10 +327,8 @@ public class OssUtils {
/**
/**
* 提交MP3转码作业
* 提交MP3转码作业
*
*
* @param fileName
* @param fileName 源文件的名称不带后缀,可为null
* 源文件的名称不带后缀,可为null
* @param filePath 源文件本地路径
* @param filePath
* 源文件本地路径
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -363,31 +348,27 @@ public class OssUtils {
...
@@ -363,31 +348,27 @@ public class OssUtils {
public
static
String
[]
submitTranscodeJobMp3Ex
(
String
fileName
,
String
filePath
)
throws
FileException
{
public
static
String
[]
submitTranscodeJobMp3Ex
(
String
fileName
,
String
filePath
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交MP3转码作业.[filePath]="
+
filePath
);
LOGGER
.
info
(
"【aliOSS】提交MP3转码作业.[filePath]="
+
filePath
);
String
fileType
=
FileUtils
.
getFileType
(
filePath
);
String
fileType
=
FileUtils
.
getFileType
(
filePath
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
fileName
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
fileType
);
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
OSSFileDO
ossFileDO
=
uploadPointFile2OSS
(
filePath
,
AliyunConstant
.
INPUT_BUCKET
,
inObjectKey
);
OSSFileDO
ossFileDO
=
uploadPointFile2OSS
(
filePath
,
AliyunConstant
.
INPUT_BUCKET
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
null
,
"mp3"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp3"
);
return
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
return
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
}
}
/**
/**
* 提交MP3转码作业,文件流方式 <br/>
* 提交MP3转码作业,文件流方式 <br/> 使用byte[]方式上传,文件流上传容易导致读取长度0,造成转码失败
* 使用byte[]方式上传,文件流上传容易导致读取长度0,造成转码失败
*
*
* @param is
* @param is 源文件流
* 源文件流
* @param fileName 源文件的名称不带后缀,可为null
* @param fileName
* @param fileType 源文件的后缀
* 源文件的名称不带后缀,可为null
* @param fileType
* 源文件的后缀
* @return
* @return
*/
*/
@Deprecated
@Deprecated
public
static
String
submitTranscodeJobMp3
(
InputStream
is
,
String
fileName
,
String
fileType
)
throws
FileException
{
public
static
String
submitTranscodeJobMp3
(
InputStream
is
,
String
fileName
,
String
fileType
)
throws
FileException
{
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
fileName
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
fileType
);
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
is
,
inputBucketName
,
inObjectKey
);
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
is
,
inputBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
null
,
"mp3"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp3"
);
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
return
result
[
0
];
return
result
[
0
];
...
@@ -396,21 +377,18 @@ public class OssUtils {
...
@@ -396,21 +377,18 @@ public class OssUtils {
/**
/**
* 提交MP3转码作业
* 提交MP3转码作业
*
*
* @param buff
* @param buff 文件
* 文件
* @param fileName 源文件的名称不带后缀,可为null
* @param fileName
* @param fileType 上传的文件后缀
* 源文件的名称不带后缀,可为null
* @param fileType
* 上传的文件后缀
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
public
static
String
submitTranscodeJobMp3
(
byte
[]
buff
,
String
fileName
,
String
fileType
)
throws
FileException
{
public
static
String
submitTranscodeJobMp3
(
byte
[]
buff
,
String
fileName
,
String
fileType
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交MP3转码作业."
);
LOGGER
.
info
(
"【aliOSS】提交MP3转码作业."
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
fileName
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
fileType
);
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
String
inputBucketName
=
AliyunConstant
.
INPUT_BUCKET
;
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
inputBucketName
,
inObjectKey
);
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
inputBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
null
,
"mp3"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp3"
);
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
return
result
[
0
];
return
result
[
0
];
...
@@ -432,10 +410,8 @@ public class OssUtils {
...
@@ -432,10 +410,8 @@ public class OssUtils {
/**
/**
* 转码作业
* 转码作业
*
*
* @param fileName
* @param fileName 文件名称
* 文件名称
* @param filePath 文件地址
* @param filePath
* 文件地址
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -448,12 +424,9 @@ public class OssUtils {
...
@@ -448,12 +424,9 @@ public class OssUtils {
* 转码作业
* 转码作业
*
*
* @param fileName
* @param fileName
* @param fileUrl
* @param fileUrl 文件地址
* 文件地址
* @param outFileType 输出文件类型
* @param outFileType
* @param waterMarkContent 视频水印文本,如果不需要水印传null
* 输出文件类型
* @param waterMarkContent
* 视频水印文本,如果不需要水印传null
* @return [0]:jobId,[1]:转码后的地址
* @return [0]:jobId,[1]:转码后的地址
*/
*/
public
static
String
[]
transcodeJobEx
(
String
fileName
,
String
fileUrl
,
String
outFileType
,
public
static
String
[]
transcodeJobEx
(
String
fileName
,
String
fileUrl
,
String
outFileType
,
...
@@ -462,7 +435,7 @@ public class OssUtils {
...
@@ -462,7 +435,7 @@ public class OssUtils {
if
(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
OSS_CDN_URLS
))
{
if
(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
OSS_CDN_URLS
))
{
return
transcodeJobExistOSS
(
fileUrl
,
fileName
,
fileType
,
outFileType
,
waterMarkContent
);
return
transcodeJobExistOSS
(
fileUrl
,
fileName
,
fileType
,
outFileType
,
waterMarkContent
);
}
else
{
}
else
{
return
transcodeJobNotExistOSS
(
fileUrl
,
fileName
,
fileType
,
waterMarkContent
);
return
transcodeJobNotExistOSS
(
fileUrl
,
fileName
,
fileType
);
}
}
}
}
...
@@ -485,13 +458,13 @@ public class OssUtils {
...
@@ -485,13 +458,13 @@ public class OssUtils {
OSSFileDO
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inputBucketName
),
inputBucketName
,
inObjectKey
);
OSSFileDO
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inputBucketName
),
inputBucketName
,
inObjectKey
);
if
(
FileUtils
.
VIDEO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
if
(
FileUtils
.
VIDEO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
outFileType
=
StringUtil
.
isEmpty
(
outFileType
)
?
"mp4"
:
outFileType
;
outFileType
=
StringUtil
.
isEmpty
(
outFileType
)
?
"mp4"
:
outFileType
;
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
fileName
,
outFileType
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
outFileType
);
// 组装水印参数
// 组装水印参数
JSONArray
waterMarkConfigArray
=
getWaterMarkConfig
(
waterMarkContent
);
JSONArray
waterMarkConfigArray
=
getWaterMarkConfig
(
waterMarkContent
);
result
=
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getVideoTemplateId
(
inputBucketName
,
outFileType
),
result
=
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getVideoTemplateId
(
inputBucketName
,
outFileType
),
outObjectKey
,
waterMarkConfigArray
,
null
);
outObjectKey
,
waterMarkConfigArray
,
null
);
}
else
if
(
FileUtils
.
AUDIO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
}
else
if
(
FileUtils
.
AUDIO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
.
value
,
fileName
,
"mp3"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
"mp3"
);
result
=
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
result
=
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
}
}
...
@@ -504,12 +477,9 @@ public class OssUtils {
...
@@ -504,12 +477,9 @@ public class OssUtils {
* @param fileUrl
* @param fileUrl
* @param fileName
* @param fileName
* @param fileType
* @param fileType
* @param outFileType
* @param waterMarkContent
* @return
* @return
*/
*/
private
static
String
[]
transcodeJobNotExistOSS
(
String
fileUrl
,
String
fileName
,
String
fileType
,
private
static
String
[]
transcodeJobNotExistOSS
(
String
fileUrl
,
String
fileName
,
String
fileType
)
{
String
waterMarkContent
)
{
LOGGER
.
info
(
"【aliOSS】不存在于OSS中的文件转码.<START>.[fileUrl]="
+
fileUrl
);
LOGGER
.
info
(
"【aliOSS】不存在于OSS中的文件转码.<START>.[fileUrl]="
+
fileUrl
);
if
(
FileUtils
.
VIDEO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
if
(
FileUtils
.
VIDEO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
return
submitTranscodeJobMp4Ex
(
fileName
,
fileUrl
);
return
submitTranscodeJobMp4Ex
(
fileName
,
fileUrl
);
...
@@ -534,20 +504,18 @@ public class OssUtils {
...
@@ -534,20 +504,18 @@ public class OssUtils {
}
}
/**
/**
* 视频
文件合并,异步方法
*
文件合并,异步方法
*
*
* @param fileUrl
* @param fileUrl 主文件地址
* 主文件地址
* @param mergeUrls 需要合并的子文件列表,最多支持4个
* @param mergeUrls
* 需要合并的子文件列表,最多支持4个
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
public
static
String
submitMergeJobMp4
(
String
fileUrl
,
List
<
String
>
mergeUrls
)
throws
FileException
{
public
static
String
submitMergeJob
(
String
fileUrl
,
List
<
String
>
mergeUrls
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交MP4
合并作业.<START>.[fileUrl]="
+
fileUrl
+
",[mergeUrls]="
+
mergeUrls
);
LOGGER
.
info
(
"【aliOSS】提交
合并作业.<START>.[fileUrl]="
+
fileUrl
+
",[mergeUrls]="
+
mergeUrls
);
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
if
(!
FileUtils
.
VIDEO
.
equals
(
FileUtils
.
getGatherName
(
fileType
))
||
ListUtils
.
isEmpty
(
mergeUrls
))
{
if
(
ListUtils
.
isEmpty
(
mergeUrls
))
{
throw
new
FileException
(
FileException
.
FILE_TYPE_ERROR
,
"不是有效的
视频文件"
);
throw
new
FileException
(
FileException
.
FILE_TYPE_ERROR
,
"不是有效的音
视频文件"
);
}
}
OSSFileDO
ossFileDO
=
null
;
OSSFileDO
ossFileDO
=
null
;
String
inputBucketName
=
null
;
String
inputBucketName
=
null
;
...
@@ -557,29 +525,42 @@ public class OssUtils {
...
@@ -557,29 +525,42 @@ public class OssUtils {
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inputBucketName
),
inputBucketName
,
inObjectKey
);
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inputBucketName
),
inputBucketName
,
inObjectKey
);
}
else
{
}
else
{
inputBucketName
=
AliyunConstant
.
FILE_BUCKET
;
inputBucketName
=
AliyunConstant
.
FILE_BUCKET
;
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
MERGE
.
value
,
null
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
MERGE
,
null
,
fileType
);
ossFileDO
=
uploadPointFile2OSS
(
fileUrl
,
inputBucketName
,
inObjectKey
);
ossFileDO
=
uploadPointFile2OSS
(
fileUrl
,
inputBucketName
,
inObjectKey
);
}
}
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
MERGE
.
value
,
null
,
"mp4"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
MERGE
,
null
,
fileType
);
String
[]
result
=
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
String
templateId
=
"mp3"
.
equals
(
fileType
)
?
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
)
mergeUrls
);
:
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
);
LOGGER
.
info
(
"【aliOSS】提交MP4合并作业.<END>.[result]="
+
JSON
.
toJSONString
(
result
));
String
[]
result
=
transcodeJob
(
ossFileDO
,
templateId
,
outObjectKey
,
null
,
mergeUrls
);
LOGGER
.
info
(
"【aliOSS】提交合并作业.<END>.[result]="
+
JSON
.
toJSONString
(
result
));
return
result
[
0
];
return
result
[
0
];
}
}
/**
/**
* 视频文件合并,同步方法
* 视频文件合并,同步方法
*
*
* @param fileUrl
* @param fileUrl 主文件地址
* 主文件地址
* @param mergeUrls 需要合并的子文件列表,最多支持4个
* @param mergeUrls
* 需要合并的子文件列表,最多支持4个
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
public
static
UploadResultInfo
submitMergeJobMp4Sync
(
String
fileUrl
,
List
<
String
>
mergeUrls
)
throws
FileException
{
public
static
UploadResultInfo
submitMergeJobMp4Sync
(
String
fileUrl
,
List
<
String
>
mergeUrls
)
throws
FileException
{
String
bucketName
=
AliyunConstant
.
getBucketName
(
fileUrl
);
String
bucketName
=
AliyunConstant
.
getBucketName
(
fileUrl
);
String
jobId
=
submitMergeJobMp4
(
fileUrl
,
mergeUrls
);
String
jobId
=
submitMergeJob
(
fileUrl
,
mergeUrls
);
return
transcodeJobResultSync
(
jobId
,
bucketName
);
}
/**
* 视频文件合并,同步方法
*
* @param fileUrl 主文件地址
* @param mergeUrls 需要合并的子文件列表,最多支持4个
* @return
* @throws FileException
*/
public
static
UploadResultInfo
submitMergeJobSync
(
String
fileUrl
,
List
<
String
>
mergeUrls
)
throws
FileException
{
String
bucketName
=
AliyunConstant
.
getBucketName
(
fileUrl
);
String
jobId
=
submitMergeJob
(
fileUrl
,
mergeUrls
);
return
transcodeJobResultSync
(
jobId
,
bucketName
);
return
transcodeJobResultSync
(
jobId
,
bucketName
);
}
}
...
@@ -615,14 +596,10 @@ public class OssUtils {
...
@@ -615,14 +596,10 @@ public class OssUtils {
/**
/**
* 转码作业
* 转码作业
*
*
* @param inputFile
* @param inputFile 输入文件
* 输入文件
* @param templateId 转码模板
* @param templateId
* @param outObjectKey 转码输出对象地址
* 转码模板
* @param waterMarkConfigArray 水印参数
* @param outObjectKey
* 转码输出对象地址
* @param waterMarkConfigArray
* 水印参数
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -679,7 +656,8 @@ public class OssUtils {
...
@@ -679,7 +656,8 @@ public class OssUtils {
/**
/**
* 组装视频合并参数
* 组装视频合并参数
*
*
* @param text
* @param mergeUrls
* @param bucketName
* @return
* @return
*/
*/
private
static
JSONArray
generateMergeInput
(
List
<
String
>
mergeUrls
,
String
bucketName
)
{
private
static
JSONArray
generateMergeInput
(
List
<
String
>
mergeUrls
,
String
bucketName
)
{
...
@@ -690,7 +668,7 @@ public class OssUtils {
...
@@ -690,7 +668,7 @@ public class OssUtils {
JSONArray
mergeJsons
=
new
JSONArray
();
JSONArray
mergeJsons
=
new
JSONArray
();
for
(
String
mergeUrl
:
mergeUrls
)
{
for
(
String
mergeUrl
:
mergeUrls
)
{
// 如果是本地文件先上传
// 如果是本地文件先上传
if
(!
StringTools
.
startsWith
(
mergeUrl
,
new
String
[]
{
"http://"
,
"https://"
}))
{
if
(!
StringTools
.
startsWith
(
mergeUrl
,
new
String
[]{
"http://"
,
"https://"
}))
{
UploadResultInfo
uploadResultInfo
=
uploadLocalFile
(
mergeUrl
,
null
);
UploadResultInfo
uploadResultInfo
=
uploadLocalFile
(
mergeUrl
,
null
);
if
(!
uploadResultInfo
.
isFlag
())
{
if
(!
uploadResultInfo
.
isFlag
())
{
continue
;
continue
;
...
@@ -712,7 +690,7 @@ public class OssUtils {
...
@@ -712,7 +690,7 @@ public class OssUtils {
/**
/**
* 组装视频水印参数
* 组装视频水印参数
*
*
* @param tex
t
* @param conten
t
* @return
* @return
*/
*/
private
static
JSONArray
getWaterMarkConfig
(
String
content
)
{
private
static
JSONArray
getWaterMarkConfig
(
String
content
)
{
...
@@ -737,11 +715,9 @@ public class OssUtils {
...
@@ -737,11 +715,9 @@ public class OssUtils {
* 提交视频剪切作业
* 提交视频剪切作业
*
*
* @param fileName
* @param fileName
* @param filePath
* @param fileUrl
* @param seek
* @param seek 开始时间:[0.000,86399.999]
* 开始时间:[0.000,86399.999]
* @param duration 持续时长 [0.000,86399.999]
* @param duration
* 持续时长 [0.000,86399.999]
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -763,12 +739,12 @@ public class OssUtils {
...
@@ -763,12 +739,12 @@ public class OssUtils {
String
inBucketName
=
fileUrlExist001
(
fileUrl
)
?
AliyunConstant
.
INPUT_BUCKET
String
inBucketName
=
fileUrlExist001
(
fileUrl
)
?
AliyunConstant
.
INPUT_BUCKET
:
AliyunConstant
.
getBucketName
(
fileUrl
);
:
AliyunConstant
.
getBucketName
(
fileUrl
);
OSSFileDO
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inBucketName
),
inBucketName
,
inObjectKey
);
OSSFileDO
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inBucketName
),
inBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
.
value
,
fileName
,
outFileType
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
outFileType
);
resultFileUrl
=
clipJob
(
ossFileDO
,
outObjectKey
,
seek
,
duration
);
resultFileUrl
=
clipJob
(
ossFileDO
,
outObjectKey
,
seek
,
duration
);
}
else
{
}
else
{
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
CUT
.
value
,
fileName
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
fileType
);
OSSFileDO
ossFileDO
=
uploadPointFile2OSS
(
fileUrl
,
AliyunConstant
.
FILE_BUCKET
,
inObjectKey
);
OSSFileDO
ossFileDO
=
uploadPointFile2OSS
(
fileUrl
,
AliyunConstant
.
FILE_BUCKET
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
.
value
,
fileName
,
outFileType
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
outFileType
);
resultFileUrl
=
clipJob
(
ossFileDO
,
outObjectKey
,
seek
,
duration
);
resultFileUrl
=
clipJob
(
ossFileDO
,
outObjectKey
,
seek
,
duration
);
}
}
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<END>.[resultFileUrl]="
+
resultFileUrl
);
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<END>.[resultFileUrl]="
+
resultFileUrl
);
...
@@ -781,10 +757,8 @@ public class OssUtils {
...
@@ -781,10 +757,8 @@ public class OssUtils {
* @param buff
* @param buff
* @param fileName
* @param fileName
* @param fileType
* @param fileType
* @param seek
* @param seek 开始时间:[0.000,86399.999]
* 开始时间:[0.000,86399.999]
* @param duration 持续时长 [0.000,86399.999]
* @param duration
* 持续时长 [0.000,86399.999]
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -792,10 +766,10 @@ public class OssUtils {
...
@@ -792,10 +766,10 @@ public class OssUtils {
throws
FileException
{
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<START>.[fileName]="
+
fileName
+
",[seek]="
+
seek
+
",[duration]="
+
duration
);
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<START>.[fileName]="
+
fileName
+
",[seek]="
+
seek
+
",[duration]="
+
duration
);
fileName
=
MD5
.
getMD5StrLower
(
fileName
);
fileName
=
MD5
.
getMD5StrLower
(
fileName
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
CUT
.
value
,
fileName
,
fileType
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
fileType
);
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
AliyunConstant
.
FILE_BUCKET
,
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
AliyunConstant
.
FILE_BUCKET
,
inObjectKey
);
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
.
value
,
fileName
,
"mp4"
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
"mp4"
);
String
resultFileUrl
=
clipJob
(
inputFile
,
outObjectKey
,
seek
,
duration
);
String
resultFileUrl
=
clipJob
(
inputFile
,
outObjectKey
,
seek
,
duration
);
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<END>.[resultFileUrl]="
+
resultFileUrl
);
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<END>.[resultFileUrl]="
+
resultFileUrl
);
return
resultFileUrl
;
return
resultFileUrl
;
...
@@ -806,10 +780,8 @@ public class OssUtils {
...
@@ -806,10 +780,8 @@ public class OssUtils {
*
*
* @param inputFile
* @param inputFile
* @param outObjectKey
* @param outObjectKey
* @param seek
* @param seek 开始时间:[0.000,86399.999]
* 开始时间:[0.000,86399.999]
* @param duration 持续时长 [0.000,86399.999]
* @param duration
* 持续时长 [0.000,86399.999]
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -856,10 +828,8 @@ public class OssUtils {
...
@@ -856,10 +828,8 @@ public class OssUtils {
/**
/**
* 组装剪切参数
* 组装剪切参数
*
*
* @param seek
* @param seek 开始时间:sssss[.SSS]
* 开始时间:sssss[.SSS]
* @param duration 持续时长 sssss[.SSS]
* @param duration
* 持续时长 sssss[.SSS]
* @return
* @return
*/
*/
private
static
JSONObject
generateClipInput
(
String
seek
,
String
duration
)
{
private
static
JSONObject
generateClipInput
(
String
seek
,
String
duration
)
{
...
@@ -877,8 +847,7 @@ public class OssUtils {
...
@@ -877,8 +847,7 @@ public class OssUtils {
* 提交视频截图任务,成功返回图片地址 <br>
* 提交视频截图任务,成功返回图片地址 <br>
*
*
* @param fileUrl
* @param fileUrl
* @param time
* @param time 秒
* 秒
* @return 成功:图片地址,失败:null
* @return 成功:图片地址,失败:null
*/
*/
public
static
String
submitSnapshotJob
(
String
fileUrl
,
int
time
)
{
public
static
String
submitSnapshotJob
(
String
fileUrl
,
int
time
)
{
...
@@ -895,7 +864,7 @@ public class OssUtils {
...
@@ -895,7 +864,7 @@ public class OssUtils {
try
{
try
{
SubmitSnapshotJobRequest
request
=
new
SubmitSnapshotJobRequest
();
SubmitSnapshotJobRequest
request
=
new
SubmitSnapshotJobRequest
();
request
.
setInput
(
generateSnapshotInput
(
inputBucketName
,
objectKey
).
toJSONString
());
request
.
setInput
(
generateSnapshotInput
(
inputBucketName
,
objectKey
).
toJSONString
());
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
SNAPSHOT
.
value
,
null
,
ImageTypeEnum
.
JPG
.
value
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
SNAPSHOT
,
null
,
ImageTypeEnum
.
JPG
.
value
);
String
outputBucketName
=
AliyunConstant
.
FILE_BUCKET
;
String
outputBucketName
=
AliyunConstant
.
FILE_BUCKET
;
request
.
setSnapshotConfig
(
generateSnapshotConfig
(
outputBucketName
,
outObjectKey
,
time
).
toJSONString
());
request
.
setSnapshotConfig
(
generateSnapshotConfig
(
outputBucketName
,
outObjectKey
,
time
).
toJSONString
());
request
.
setPipelineId
(
AliyunConstant
.
getPipelineId
(
inputBucketName
));
request
.
setPipelineId
(
AliyunConstant
.
getPipelineId
(
inputBucketName
));
...
@@ -914,7 +883,7 @@ public class OssUtils {
...
@@ -914,7 +883,7 @@ public class OssUtils {
/**
/**
* 组装截图参数
* 组装截图参数
*
*
* @param osslocation
* @param bucketName
* @param outputobject
* @param outputobject
* @param time
* @param time
* @return
* @return
...
@@ -933,7 +902,7 @@ public class OssUtils {
...
@@ -933,7 +902,7 @@ public class OssUtils {
/**
/**
* 组装截图参数
* 组装截图参数
*
*
* @param ossLocation
* @param bucketName
* @param inputObject
* @param inputObject
* @return
* @return
*/
*/
...
@@ -948,8 +917,7 @@ public class OssUtils {
...
@@ -948,8 +917,7 @@ public class OssUtils {
/**
/**
* 组装转换后的地址(华东地区)
* 组装转换后的地址(华东地区)
*
*
* @param bucket
* @param transcodeJobId
* @param object
* @return
* @return
*/
*/
public
static
UploadResultInfo
getOSSResult
(
String
transcodeJobId
)
throws
FileException
{
public
static
UploadResultInfo
getOSSResult
(
String
transcodeJobId
)
throws
FileException
{
...
@@ -1051,7 +1019,7 @@ public class OssUtils {
...
@@ -1051,7 +1019,7 @@ public class OssUtils {
/**
/**
* 获取媒体信息,仅限经过转码的音视频文件
* 获取媒体信息,仅限经过转码的音视频文件
*
*
* @param oss
Url
* @param file
Url
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -1105,7 +1073,7 @@ public class OssUtils {
...
@@ -1105,7 +1073,7 @@ public class OssUtils {
/**
/**
* 下载文件成byte[]
* 下载文件成byte[]
*
*
* @param objectKey
* @param fileUrl
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -1150,8 +1118,8 @@ public class OssUtils {
...
@@ -1150,8 +1118,8 @@ public class OssUtils {
/**
/**
* 下载文件到本地
* 下载文件到本地
*
*
* @param objectKey
* @param fileUrl
* @param f
ilePath
* @param outF
ilePath
*/
*/
public
static
void
downloadFile
(
String
fileUrl
,
String
outFilePath
)
throws
FileException
{
public
static
void
downloadFile
(
String
fileUrl
,
String
outFilePath
)
throws
FileException
{
String
objectKey
=
splitObjectKey
(
fileUrl
);
String
objectKey
=
splitObjectKey
(
fileUrl
);
...
@@ -1172,11 +1140,12 @@ public class OssUtils {
...
@@ -1172,11 +1140,12 @@ public class OssUtils {
}
}
/**
/**
* 组装返回的文件信息 <br>
* 组装返回的文件信息 <br> isPrivateKey=true,表示地址可下载,但需要加上密钥,密钥生产使用方法getHashValue或者addUrlKey <br>
* isPrivateKey=true,表示地址可下载,但需要加上密钥,密钥生产使用方法getHashValue或者addUrlKey <br>
*
*
* @param objectKey
* @param objectKey
* @param isPrivateKey
* @param size
* @param duration
* @param outputBucketName
* @return
* @return
*/
*/
private
static
UploadResultInfo
getResultInfo
(
String
objectKey
,
Long
size
,
String
duration
,
private
static
UploadResultInfo
getResultInfo
(
String
objectKey
,
Long
size
,
String
duration
,
...
@@ -1202,8 +1171,8 @@ public class OssUtils {
...
@@ -1202,8 +1171,8 @@ public class OssUtils {
/**
/**
* 组装转换后的地址
* 组装转换后的地址
*
*
* @param bucket
* @param objectKey
* @param object
* @param outputBucketName
* @return
* @return
*/
*/
private
static
String
getOSSUrl
(
String
objectKey
,
String
outputBucketName
)
{
private
static
String
getOSSUrl
(
String
objectKey
,
String
outputBucketName
)
{
...
@@ -1216,7 +1185,7 @@ public class OssUtils {
...
@@ -1216,7 +1185,7 @@ public class OssUtils {
/**
/**
* 从完整的HTTP路径中截取objectName
* 从完整的HTTP路径中截取objectName
*
*
* @param objectName
* @param fileUrl
* @return
* @return
*/
*/
public
static
String
splitObjectKey
(
String
fileUrl
)
{
public
static
String
splitObjectKey
(
String
fileUrl
)
{
...
@@ -1249,40 +1218,41 @@ public class OssUtils {
...
@@ -1249,40 +1218,41 @@ public class OssUtils {
/**
/**
* 组装输入文件Object路径
* 组装输入文件Object路径
*
*
* @param filePath
* @param aliyunEnum
* @param fileName
* @param fileType
* @return
* @return
*/
*/
private
static
String
getInObjectKey
(
String
uploadType
,
String
fileName
,
String
fileType
)
{
private
static
String
getInObjectKey
(
AliyunEnum
aliyunEnum
,
String
fileName
,
String
fileType
)
{
fileName
=
StringUtil
.
isEmpty
(
fileName
)
?
UUIDUitl
.
taskName
()
fileName
=
StringUtil
.
isEmpty
(
fileName
)
?
UUIDUitl
.
taskName
()
:
(
FileUtils
.
formatName
(
fileName
)
+
"_"
+
LocalDateUtils
.
getYmdhmss
());
:
(
FileUtils
.
formatName
(
fileName
)
+
"_"
+
LocalDateUtils
.
getYmdhmss
());
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
gatherName
=
StringUtil
.
isEmpty
(
gatherName
)
?
"other"
:
gatherName
.
toLowerCase
();
gatherName
=
StringUtil
.
isEmpty
(
gatherName
)
?
"other"
:
gatherName
.
toLowerCase
();
return
new
StringBuilder
(
"oss/"
).
append
(
uploadTyp
e
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
).
append
(
fileType
)
return
new
StringBuilder
(
"oss/"
).
append
(
aliyunEnum
.
valu
e
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
).
append
(
fileType
)
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
}
}
/**
/**
* 组装输出文件Object路径
* 组装输出文件Object路径
*
*
* @param uploadType
* @param aliyunEnum
* @param fileName
* @param fileName
* @param fileType
* @param fileType
* @return
* @return
*/
*/
private
static
String
getOutObjectKey
(
String
uploadType
,
String
fileName
,
String
fileType
)
{
private
static
String
getOutObjectKey
(
AliyunEnum
aliyunEnum
,
String
fileName
,
String
fileType
)
{
fileName
=
StringUtil
.
isEmpty
(
fileName
)
?
UUIDUitl
.
taskName
()
fileName
=
StringUtil
.
isEmpty
(
fileName
)
?
UUIDUitl
.
taskName
()
:
(
FileUtils
.
formatName
(
fileName
)
+
"_"
+
LocalDateUtils
.
getYmdhmss
());
:
(
FileUtils
.
formatName
(
fileName
)
+
"_"
+
LocalDateUtils
.
getYmdhmss
());
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
gatherName
=
StringUtil
.
isEmpty
(
gatherName
)
?
"other"
:
gatherName
.
toLowerCase
();
gatherName
=
StringUtil
.
isEmpty
(
gatherName
)
?
"other"
:
gatherName
.
toLowerCase
();
return
new
StringBuilder
(
"oss/"
).
append
(
uploadTyp
e
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
).
append
(
fileType
)
return
new
StringBuilder
(
"oss/"
).
append
(
aliyunEnum
.
valu
e
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
).
append
(
fileType
)
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
}
}
/**
/**
* 获取文件下载加密密钥,默认有效期48小时 <br>
* 获取文件下载加密密钥,默认有效期48小时 <br> 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
* 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
*
*
* @param objectKey
* @param fileUrl
* @return
* @return
*/
*/
public
static
String
getHashValue
(
String
fileUrl
)
{
public
static
String
getHashValue
(
String
fileUrl
)
{
...
@@ -1290,8 +1260,7 @@ public class OssUtils {
...
@@ -1290,8 +1260,7 @@ public class OssUtils {
}
}
/**
/**
* 获取文件下载加密密钥,长期有效 <br>
* 获取文件下载加密密钥,长期有效 <br> 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
* 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
*
*
* @param fileUrl
* @param fileUrl
* @return
* @return
...
@@ -1301,12 +1270,10 @@ public class OssUtils {
...
@@ -1301,12 +1270,10 @@ public class OssUtils {
}
}
/**
/**
* 获取文件下载加密密钥 <br>
* 获取文件下载加密密钥 <br> 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
* 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
*
*
* @param fileUrl
* @param fileUrl
* @param second
* @param second 秒,地址有效期
* 秒,地址有效期
* @return
* @return
*/
*/
public
static
String
getHashValue
(
String
fileUrl
,
long
second
)
{
public
static
String
getHashValue
(
String
fileUrl
,
long
second
)
{
...
@@ -1323,11 +1290,9 @@ public class OssUtils {
...
@@ -1323,11 +1290,9 @@ public class OssUtils {
}
}
/**
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br>
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br> 默认有效期48小时
* 默认有效期48小时
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @return
* @return
*/
*/
public
static
String
urlAddKey
(
String
fileUrl
)
{
public
static
String
urlAddKey
(
String
fileUrl
)
{
...
@@ -1335,11 +1300,9 @@ public class OssUtils {
...
@@ -1335,11 +1300,9 @@ public class OssUtils {
}
}
/**
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br>
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br> 长期有效
* 长期有效
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @return
* @return
*/
*/
public
static
String
urlAddKeyLong
(
String
fileUrl
)
{
public
static
String
urlAddKeyLong
(
String
fileUrl
)
{
...
@@ -1349,10 +1312,8 @@ public class OssUtils {
...
@@ -1349,10 +1312,8 @@ public class OssUtils {
/**
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br>
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br>
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @param second 秒,地址有效期
* @param second
* 秒,地址有效期
* @return
* @return
*/
*/
public
static
String
urlAddKey
(
String
fileUrl
,
long
second
)
{
public
static
String
urlAddKey
(
String
fileUrl
,
long
second
)
{
...
@@ -1363,10 +1324,8 @@ public class OssUtils {
...
@@ -1363,10 +1324,8 @@ public class OssUtils {
/**
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问(不转义中文) <br>
* 将文件URL生成带密钥的地址,可以在浏览器直接访问(不转义中文) <br>
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @param second 秒,地址有效期
* @param second
* 秒,地址有效期
* @return
* @return
*/
*/
public
static
String
urlAddKeyChina
(
String
fileUrl
,
long
second
)
{
public
static
String
urlAddKeyChina
(
String
fileUrl
,
long
second
)
{
...
@@ -1390,11 +1349,9 @@ public class OssUtils {
...
@@ -1390,11 +1349,9 @@ public class OssUtils {
}
}
/**
/**
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br>
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br> 默认有效期48小时
* 默认有效期48小时
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @return
* @return
*/
*/
public
static
String
urlAddKey2Sms
(
String
fileUrl
)
{
public
static
String
urlAddKey2Sms
(
String
fileUrl
)
{
...
@@ -1402,11 +1359,9 @@ public class OssUtils {
...
@@ -1402,11 +1359,9 @@ public class OssUtils {
}
}
/**
/**
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br>
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br> 长期有效
* 长期有效
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @return
* @return
*/
*/
public
static
String
urlAddKeyLong2Sms
(
String
fileUrl
)
{
public
static
String
urlAddKeyLong2Sms
(
String
fileUrl
)
{
...
@@ -1416,10 +1371,8 @@ public class OssUtils {
...
@@ -1416,10 +1371,8 @@ public class OssUtils {
/**
/**
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br>
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br>
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @param second 秒,地址有效期
* @param second
* 秒,地址有效期
* @return
* @return
*/
*/
public
static
String
urlAddKey2Sms
(
String
fileUrl
,
long
second
)
{
public
static
String
urlAddKey2Sms
(
String
fileUrl
,
long
second
)
{
...
@@ -1443,8 +1396,7 @@ public class OssUtils {
...
@@ -1443,8 +1396,7 @@ public class OssUtils {
/**
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问,并转换为短链接<br>
* 将文件URL生成带密钥的地址,可以在浏览器直接访问,并转换为短链接<br>
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @return
* @return
*/
*/
public
static
String
urlAddKey2Short
(
String
fileUrl
)
{
public
static
String
urlAddKey2Short
(
String
fileUrl
)
{
...
@@ -1452,11 +1404,9 @@ public class OssUtils {
...
@@ -1452,11 +1404,9 @@ public class OssUtils {
}
}
/**
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 ,并转换为短链接<br>
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 ,并转换为短链接<br> 长期有效
* 长期有效
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @return
* @return
*/
*/
public
static
String
urlAddKeyLong2Short
(
String
fileUrl
)
{
public
static
String
urlAddKeyLong2Short
(
String
fileUrl
)
{
...
@@ -1466,10 +1416,8 @@ public class OssUtils {
...
@@ -1466,10 +1416,8 @@ public class OssUtils {
/**
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问,并转换为短链接<br>
* 将文件URL生成带密钥的地址,可以在浏览器直接访问,并转换为短链接<br>
*
*
* @param fileUrl
* @param fileUrl 文件URL全路径地址
* 文件URL全路径地址
* @param second 秒,地址有效期
* @param second
* 秒,地址有效期
* @return
* @return
*/
*/
public
static
String
urlAddKey2Short
(
String
fileUrl
,
long
second
)
{
public
static
String
urlAddKey2Short
(
String
fileUrl
,
long
second
)
{
...
@@ -1479,36 +1427,10 @@ public class OssUtils {
...
@@ -1479,36 +1427,10 @@ public class OssUtils {
}
}
/**
/**
* 图片增加水印
*
* @param fileUrl
* @return 返回处理后的图片网络地址
*/
public
static
String
imageWatermark
(
String
fileUrl
,
String
watermarkContent
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】图片增加水印,<START>.[fileUrl]="
+
fileUrl
+
",[watermarkContent]="
+
watermarkContent
);
if
(
StringUtil
.
isEmpty
(
watermarkContent
))
{
throw
new
FileException
(
FileException
.
PARAM_ERROR
,
"水印内容不能为空"
);
}
ImageUtils
.
checkIsImage
(
fileUrl
);
StringBuilder
style
=
new
StringBuilder
(
"image/watermark"
);
// 水印文本,需要转换成base64格式
style
.
append
(
",text_"
+
new
String
(
Base64
.
encodeBase64
(
watermarkContent
.
getBytes
())));
// 水印字体大小,位置,偏移量
style
.
append
(
",size_60,g_se,x_20,y_20"
);
// 生成新的图片地址
String
localPath
=
imageHandle
(
fileUrl
,
style
.
toString
());
// 上传处理后的图片,并删除本地文件
UploadResultInfo
uploadResultInfo
=
uploadLocalFile
(
localPath
,
null
);
FileUtils
.
deleteFile
(
localPath
);
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
}
/**
* 图片缩放,按宽度等比例缩放
* 图片缩放,按宽度等比例缩放
*
*
* @param fileUrl
* @param fileUrl
* @param width
* @param width 目前最大支持1024,超过1024或者小于0都按1024计算
* 目前最大支持1024,超过1024或者小于0都按1024计算
* @return 返回处理后的图片本地地址
* @return 返回处理后的图片本地地址
* @throws FileException
* @throws FileException
*/
*/
...
@@ -1531,8 +1453,7 @@ public class OssUtils {
...
@@ -1531,8 +1453,7 @@ public class OssUtils {
* 图片缩放,按倍数百分比缩放
* 图片缩放,按倍数百分比缩放
*
*
* @param fileUrl
* @param fileUrl
* @param p
* @param p 1-1000,倍数百分比。 小于100,即是缩小,大于100即是放大。
* 1-1000,倍数百分比。 小于100,即是缩小,大于100即是放大。
* @return 返回处理后的图片本地地址
* @return 返回处理后的图片本地地址
* @throws FileException
* @throws FileException
*/
*/
...
@@ -1555,8 +1476,7 @@ public class OssUtils {
...
@@ -1555,8 +1476,7 @@ public class OssUtils {
* 图片自动旋转
* 图片自动旋转
*
*
* @param fileUrl
* @param fileUrl
* @param width
* @param width 图片旋转以后的宽度,最大不能超过4096,传0则按原始宽度旋转
* 图片旋转以后的宽度,最大不能超过4096,传0则按原始宽度旋转
* @return 返回处理后的图片本地地址
* @return 返回处理后的图片本地地址
* @throws FileException
* @throws FileException
*/
*/
...
@@ -1582,14 +1502,10 @@ public class OssUtils {
...
@@ -1582,14 +1502,10 @@ public class OssUtils {
* 图片裁剪,如果图片带有旋转属性,会自动旋转纠正角度后在进行裁剪。
* 图片裁剪,如果图片带有旋转属性,会自动旋转纠正角度后在进行裁剪。
*
*
* @param fileUrl
* @param fileUrl
* @param width
* @param width 裁剪的宽度
* 裁剪的宽度
* @param height 裁剪的高度
* @param height
* @param x 左上角开始, 裁剪的X轴坐标
* 裁剪的高度
* @param y 左上角开始, 裁剪的Y轴坐标
* @param x
* 左上角开始, 裁剪的X轴坐标
* @param y
* 左上角开始, 裁剪的Y轴坐标
* @return 返回处理后的图片本地地址
* @return 返回处理后的图片本地地址
* @throws FileException
* @throws FileException
*/
*/
...
@@ -1606,11 +1522,57 @@ public class OssUtils {
...
@@ -1606,11 +1522,57 @@ public class OssUtils {
style
.
append
(
",y_"
).
append
(
y
);
style
.
append
(
",y_"
).
append
(
y
);
// 生成新的图片地址
// 生成新的图片地址
String
localPath
=
imageHandle
(
fileUrl
,
style
.
toString
());
String
localPath
=
imageHandle
(
fileUrl
,
style
.
toString
());
LOGGER
.
info
(
"【aliOSS】图片裁剪,<START
>.[localPath]="
+
localPath
);
LOGGER
.
info
(
"【aliOSS】图片裁剪,<END
>.[localPath]="
+
localPath
);
return
localPath
;
return
localPath
;
}
}
/**
/**
* <持久化>图片裁剪,如果图片带有旋转属性,会自动旋转纠正角度后在进行裁剪。
*
* @param fileUrl
* @param width
* @param height
* @param x
* @param y
* @return
* @throws FileException
*/
public
static
OssImageCropDTO
imageCropSaveas
(
String
fileUrl
,
int
width
,
int
height
,
int
x
,
int
y
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】图片裁剪持久化,<START>.[fileUrl]="
+
fileUrl
);
ImageUtils
.
checkIsImage
(
fileUrl
);
if
(!(
width
>=
0
&&
width
<=
4096
&&
height
>=
0
&&
height
<=
4096
&&
x
>=
0
&&
y
>=
0
))
{
throw
new
FileException
(
FileException
.
PARAM_ERROR
,
"裁剪的长宽或者坐标数值必须大于0且长宽必须小于4096"
);
}
String
fileName
=
FileUtils
.
getFileName
(
fileUrl
);
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
String
bucketName
=
AliyunConstant
.
getBucketName
(
fileUrl
);
String
sourceKey
=
splitObjectKey
(
fileUrl
);
StringBuilder
style
=
new
StringBuilder
(
"image/auto-orient,1/crop"
);
style
.
append
(
",w_"
).
append
(
width
);
style
.
append
(
",h_"
).
append
(
height
);
style
.
append
(
",x_"
).
append
(
x
);
style
.
append
(
",y_"
).
append
(
y
);
style
.
append
(
"|sys/saveas"
);
style
.
append
(
",o_"
).
append
(
BinaryUtil
.
toBase64String
(
getOutObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
fileType
).
getBytes
()));
style
.
append
(
",b_"
).
append
(
BinaryUtil
.
toBase64String
(
bucketName
.
getBytes
()));
ProcessObjectRequest
request
=
new
ProcessObjectRequest
(
bucketName
,
sourceKey
,
style
.
toString
());
// 创建OSSClient实例
OSSClient
ossClient
=
getOSSClient
(
bucketName
);
GenericResult
processResult
=
ossClient
.
processObject
(
request
);
OssImageCropDTO
ossImageCropDTO
=
null
;
try
{
String
json
=
IOUtils
.
readStreamAsString
(
processResult
.
getResponse
().
getContent
(),
"UTF-8"
);
processResult
.
getResponse
().
getContent
().
close
();
ossImageCropDTO
=
JSON
.
parseObject
(
json
,
OssImageCropDTO
.
class
);
ossImageCropDTO
.
setFileUrl
(
getOSSUrl
(
ossImageCropDTO
.
getObject
(),
bucketName
));
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【aliOSS】图片裁剪持久化失败:"
+
e
.
getMessage
(),
e
);
}
LOGGER
.
info
(
"【aliOSS】图片裁剪持久化,<END>.[ossImageCropDTO]="
+
ossImageCropDTO
);
return
ossImageCropDTO
;
}
/**
* 图片处理
* 图片处理
*
*
* @param fileUrl
* @param fileUrl
...
@@ -1639,34 +1601,67 @@ public class OssUtils {
...
@@ -1639,34 +1601,67 @@ public class OssUtils {
}
}
/**
/**
* 图片增加水印(文本)
* 图片增加水印
*
*
* @param fileUrl
* @param fileUrl
* 原图地址
* @return 返回处理后的图片网络地址
* @param fontSize
* 字体大小
* @param watermarkContent
* 水印内容
* @return
* @throws FileException
*/
*/
public
static
String
imageWatermark4Text
(
String
fileUrl
,
Integer
fontSize
,
String
watermarkContent
)
public
static
String
imageWatermark
(
String
fileUrl
,
String
watermarkContent
)
throws
FileException
{
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】图片增加水印,<START>.[fileUrl]="
+
fileUrl
+
",[watermarkContent]="
+
watermarkContent
);
LOGGER
.
info
(
"【aliOSS】图片增加水印(文本),<START>.[fileUrl]="
+
fileUrl
+
",[fontSize]="
+
fontSize
+
",[watermarkContent]="
+
watermarkContent
);
if
(
StringUtil
.
isEmpty
(
watermarkContent
))
{
if
(
StringUtil
.
isEmpty
(
watermarkContent
))
{
throw
new
FileException
(
FileException
.
PARAM_ERROR
,
"水印内容不能为空"
);
throw
new
FileException
(
FileException
.
PARAM_ERROR
,
"水印内容不能为空"
);
}
}
ImageUtils
.
checkIsImage
(
fileUrl
);
StringBuilder
style
=
new
StringBuilder
(
"image/watermark"
);
StringBuilder
style
=
new
StringBuilder
(
"image/watermark"
);
// 水印字体大小
// 水印文本,需要转换成base64格式
fontSize
=
caleImageFontSize
(
fileUrl
,
fontSize
);
style
.
append
(
",text_"
+
new
String
(
Base64
.
encodeBase64
(
watermarkContent
.
getBytes
())));
// 水印字体大小,位置,偏移量
style
.
append
(
",size_60,g_se,x_20,y_20"
);
// 生成新的图片地址
String
localPath
=
imageHandle
(
fileUrl
,
style
.
toString
());
// 上传处理后的图片,并删除本地文件
UploadResultInfo
uploadResultInfo
=
uploadLocalFile
(
localPath
,
null
);
FileUtils
.
deleteFile
(
localPath
);
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
}
/**
* 图片增加水印(文本)
*
* @param ossImageDTO 原图地址
* @return
* @throws FileException
*/
public
static
String
imageWatermark4Text
(
OssImageDTO
ossImageDTO
)
{
LOGGER
.
info
(
"【aliOSS】图片增加水印(文本),<START>.[ossImageDTO]="
+
ossImageDTO
);
String
text
=
ossImageDTO
.
getText
();
if
(
StringUtil
.
isEmpty
(
text
))
{
throw
new
IllegalArgumentException
(
"水印内容不能为空"
);
}
String
fileUrl
=
ossImageDTO
.
getFileUrl
();
// 检查文件合法性
fileUrl
=
checkImageFileUrl
(
fileUrl
);
// 水印文本
// 水印文本
watermarkContent
=
"©"
+
(
watermarkContent
.
length
()
>
7
?
watermarkContent
.
substring
(
0
,
7
)
:
watermarkContent
);
StringBuilder
style
=
new
StringBuilder
(
"image/watermark"
);
style
.
append
(
",text_"
+
new
String
(
Base64
.
encodeBase64URLSafe
(
watermarkContent
.
getBytes
())));
style
.
append
(
",text_"
+
new
String
(
Base64
.
encodeBase64URLSafe
(
text
.
getBytes
())));
// 水印字体大小
// 水印字体大小,如果小于1,自动计算文字大小2
style
.
append
(
",size_"
).
append
(
fontSize
);
int
size
=
ossImageDTO
.
getSize
();
// 水印颜色,位置,偏移量
style
.
append
(
",size_"
).
append
(
size
<
1
?
caleImageFontSize
(
fileUrl
)
:
size
);
style
.
append
(
",color_FFFFFF,shadow_100,g_sw,x_10,y_10"
);
// 水印颜色,默认使用白色
String
color
=
ossImageDTO
.
getColor
();
style
.
append
(
",color_"
).
append
(
StringUtil
.
isEmpty
(
color
)
?
"FFFFFF"
:
color
);
// 区域位置
String
g
=
ossImageDTO
.
getG
();
if
(!
StringUtil
.
isEmpty
(
g
))
{
style
.
append
(
",g_"
).
append
(
g
);
}
// 偏移量,x和y坐标
style
.
append
(
",x_"
).
append
(
ossImageDTO
.
getX
()).
append
(
",y_"
).
append
(
ossImageDTO
.
getY
());
// 水印文字阴影透明度
int
shadow
=
ossImageDTO
.
getShadow
();
if
(
shadow
>
0
)
{
style
.
append
(
",shadow_"
).
append
(
shadow
>
100
?
100
:
shadow
);
}
String
result
=
imageWatermarkHandle
(
fileUrl
,
style
.
toString
());
String
result
=
imageWatermarkHandle
(
fileUrl
,
style
.
toString
());
LOGGER
.
info
(
"【aliOSS】图片增加水印(文本),<END>.[result]="
+
result
);
LOGGER
.
info
(
"【aliOSS】图片增加水印(文本),<END>.[result]="
+
result
);
return
result
;
return
result
;
...
@@ -1675,10 +1670,8 @@ public class OssUtils {
...
@@ -1675,10 +1670,8 @@ public class OssUtils {
/**
/**
* 图片增加水印(图片)
* 图片增加水印(图片)
*
*
* @param fileUrl
* @param fileUrl 原图地址
* 原图地址
* @param watermarkImageUrl 水印图片地址
* @param watermarkImageUrl
* 水印图片地址
* @return
* @return
* @throws FileException
* @throws FileException
*/
*/
...
@@ -1705,9 +1698,8 @@ public class OssUtils {
...
@@ -1705,9 +1698,8 @@ public class OssUtils {
* @param style
* @param style
*/
*/
private
static
String
imageWatermarkHandle
(
String
fileUrl
,
String
style
)
{
private
static
String
imageWatermarkHandle
(
String
fileUrl
,
String
style
)
{
String
fileName
=
FileUtils
.
getFileName
(
fileUrl
);
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
// 检查文件合法性
fileUrl
=
checkFileUrl
(
fileUrl
,
fileType
);
String
key
=
splitObjectKey
(
fileUrl
);
String
key
=
splitObjectKey
(
fileUrl
);
String
bucketName
=
AliyunConstant
.
getBucketName
(
fileUrl
);
String
bucketName
=
AliyunConstant
.
getBucketName
(
fileUrl
);
GetObjectRequest
request
=
new
GetObjectRequest
(
bucketName
,
key
);
GetObjectRequest
request
=
new
GetObjectRequest
(
bucketName
,
key
);
...
@@ -1715,27 +1707,28 @@ public class OssUtils {
...
@@ -1715,27 +1707,28 @@ public class OssUtils {
// 创建OSSClient实例
// 创建OSSClient实例
OSSClient
ossClient
=
getOSSClient
(
bucketName
);
OSSClient
ossClient
=
getOSSClient
(
bucketName
);
// 生成带水印的本地图片地址
// 生成带水印的本地图片地址
String
localPath
=
FilePathConst
.
IMAGE_WATERMARK_PATH
+
UUIDUitl
.
generateString
(
32
)
+
"."
+
fileType
;
String
localPath
=
FilePathConst
.
IMAGE_WATERMARK_PATH
+
fileName
+
"_"
+
LocalDateUtils
.
getYmdhmss
()
+
"."
+
fileType
;
FileUtils
.
creatFiles
(
localPath
);
FileUtils
.
creatFiles
(
localPath
);
ossClient
.
getObject
(
request
,
new
File
(
localPath
));
ossClient
.
getObject
(
request
,
new
File
(
localPath
));
ossClient
.
shutdown
();
ossClient
.
shutdown
();
// 上传处理后的图片,并删除本地文件
// 上传处理后的图片,并删除本地文件
UploadResultInfo
uploadResultInfo
=
uploadLocalFile
(
localPath
,
null
);
UploadResultInfo
uploadResultInfo
=
uploadLocalFile4CustomName
(
localPath
,
fileName
);
FileUtils
.
deleteFile
(
localPath
);
FileUtils
.
deleteFile
(
localPath
);
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
}
}
/**
/**
* 检查
文件合法性,如果是本地文件先上传
* 检查图片
文件合法性,如果是本地文件先上传
*
*
* @param fileUrl
* @param fileUrl
* @param fileType
* @return
* @return
*/
*/
private
static
String
checkFileUrl
(
String
fileUrl
,
String
fileType
)
throws
BizException
{
private
static
String
checkImageFileUrl
(
String
fileUrl
)
throws
BizException
{
LOGGER
.
info
(
"【aliOSS】检查文件合法性,<START>.[fileUrl]="
+
fileUrl
);
LOGGER
.
info
(
"【aliOSS】检查文件合法性,<START>.[fileUrl]="
+
fileUrl
);
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
if
(!
FileUtils
.
IMAGE
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
if
(!
FileUtils
.
IMAGE
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
throw
new
FileException
(
FileException
.
FILE_TYPE_ERROR
,
"不是有效的
文件"
);
throw
new
FileException
(
FileException
.
FILE_TYPE_ERROR
,
"不是有效的图片
文件"
);
}
}
// 如果是本地文件先上传
// 如果是本地文件先上传
if
(!
fileUrl
.
startsWith
(
"http"
))
{
if
(!
fileUrl
.
startsWith
(
"http"
))
{
...
@@ -1754,15 +1747,10 @@ public class OssUtils {
...
@@ -1754,15 +1747,10 @@ public class OssUtils {
* 计算图片水印文字大小
* 计算图片水印文字大小
*
*
* @param fileUrl
* @param fileUrl
* @param fontSize
* @return
* @return
*/
*/
private
static
Integer
caleImageFontSize
(
String
fileUrl
,
Integer
fontSize
)
{
private
static
Integer
caleImageFontSize
(
String
fileUrl
)
{
LOGGER
.
info
(
"【aliOSS】计算图片水印文字大小,<START>.[fileUrl]="
+
fileUrl
+
",[fontSize]="
+
fontSize
);
LOGGER
.
info
(
"【aliOSS】计算图片水印文字大小,<START>.[fileUrl]="
+
fileUrl
);
if
(
fontSize
!=
null
&&
fontSize
>
1
&&
fontSize
<
100
)
{
return
fontSize
;
}
// 计算字体大小
float
imageWidth
=
ImageUtils
.
getWidthSize
(
fileUrl
);
float
imageWidth
=
ImageUtils
.
getWidthSize
(
fileUrl
);
Float
result
=
imageWidth
==
0
?
18
:
imageWidth
/
750
*
18
;
Float
result
=
imageWidth
==
0
?
18
:
imageWidth
/
750
*
18
;
LOGGER
.
info
(
"【aliOSS】计算图片水印文字大小,<END>.[result]="
+
result
);
LOGGER
.
info
(
"【aliOSS】计算图片水印文字大小,<END>.[result]="
+
result
);
...
@@ -1782,7 +1770,8 @@ public class OssUtils {
...
@@ -1782,7 +1770,8 @@ public class OssUtils {
/**
/**
* 判断object是否存在
* 判断object是否存在
*
*
* @param fileUrl
* @param bucketName
* @param objectKey
* @return
* @return
*/
*/
private
static
boolean
doesObjectExist
(
String
bucketName
,
String
objectKey
)
{
private
static
boolean
doesObjectExist
(
String
bucketName
,
String
objectKey
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment