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
391cbb85
Commit
391cbb85
authored
Jun 10, 2019
by
songxiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
胡老板的需求改的方法
parent
1cbb0669
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
387 additions
and
360 deletions
+387
-360
OssUtils.java
...rc/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
+196
-191
UrlUtils.java
...ain/java/com/pcloud/common/utils/httpclient/UrlUtils.java
+191
-169
No files found.
pcloud-common/src/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
View file @
391cbb85
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.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
...
...
@@ -50,41 +63,28 @@ import com.pcloud.common.utils.rsa.MD5;
import
com.pcloud.common.utils.string.StringTools
;
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
* @创建时间:2017年6月26日,下午12:03:44 @版本:1.0
*/
public
class
OssUtils
{
/**
* logger
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
OssUtils
.
class
);
/**
* ACS客户端,华东地区
*/
private
static
DefaultAcsClient
acsClient
;
/**
* ACS客户端,华北地区
*/
private
static
DefaultAcsClient
acsClientBj
;
/**
* 获取ACS客户端
*
...
...
@@ -96,13 +96,13 @@ public class OssUtils {
String
mtsRegion
=
AliyunConstant
.
getMtsRegion
(
bucketName
);
DefaultProfile
.
addEndpoint
(
mtsRegion
,
mtsRegion
,
"Mts"
,
AliyunConstant
.
getMtsEndPoint
(
bucketName
));
return
new
DefaultAcsClient
(
DefaultProfile
.
getProfile
(
mtsRegion
,
AliyunConstant
.
MAIN_ACCESS_KEY_ID
,
AliyunConstant
.
MAIN_ACCESS_KEY_SECRET
));
AliyunConstant
.
MAIN_ACCESS_KEY_SECRET
));
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【aliOSS】初始化媒体转码ACS客户端失败:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_READ_FAILURE
,
"初始化Acs客户端失败"
);
}
}
/**
* 获取ACS客户端
*
...
...
@@ -122,7 +122,7 @@ public class OssUtils {
return
acsClient
;
}
}
/**
* 本地上传文件到aliOSS,isFlag=true,表示上传成功
*
...
...
@@ -134,7 +134,7 @@ public class OssUtils {
public
static
UploadResultInfo
uploadLocalFile
(
String
filePath
,
String
fileName
)
throws
FileException
{
return
uploadLocalFile4CustomName
(
filePath
,
null
);
}
/**
* 本地上传文件到aliOSS,isFlag=true,表示上传成功 <br> 自定义上传后的文件名称
*
...
...
@@ -148,18 +148,18 @@ public class OssUtils {
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
UPLOAD
,
fileName
,
fileType
);
uploadPointFile2OSS
(
filePath
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
UploadResultInfo
uploadResultInfo
=
getResultInfo
(
outObjectKey
,
new
File
(
filePath
).
length
(),
null
,
AliyunConstant
.
FILE_BUCKET
);
AliyunConstant
.
FILE_BUCKET
);
// 如果上传的是图片转换成webp
// if (uploadResultInfo != null && FileTypeUtils.equalsGatherName(fileType, FileTypeUtils.IMAGE)) {
// ImageUtils.toWebp(uploadResultInfo.getUrl());
// }
return
uploadResultInfo
;
}
/**
* 父子文件上传,子文件全路径和父文件相同,文件后缀类型自定义
*
* @param filePath
子文件本地路径
* @param filePath 子文件本地路径
* @param parentFileUrl 仅限OSS的全路径,其他本地文件路径均不支持
* @return
* @throws FileException
...
...
@@ -170,11 +170,11 @@ public class OssUtils {
uploadPointFile2OSS
(
filePath
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
return
getResultInfo
(
outObjectKey
,
new
File
(
filePath
).
length
(),
null
,
AliyunConstant
.
FILE_BUCKET
);
}
/**
* byte[]数组文件流上传 ,isFlag=true,表示上传成功
*
* @param buff
源文件字节数组
* @param buff 源文件字节数组
* @param fileName 源文件名称不带后缀,可为null
* @param fileType 源文件后缀
* @return
...
...
@@ -183,18 +183,18 @@ public class OssUtils {
public
static
UploadResultInfo
uploadFileByte
(
byte
[]
buff
,
String
fileName
,
String
fileType
)
throws
FileException
{
return
uploadFileStream
(
new
ByteArrayInputStream
(
buff
),
fileName
,
fileType
);
}
/**
* 文件流上传到aliOSS, isFlag=true,表示上传成功 <br/> 慎用,该方法稳定性不确定,曾出现过上传的文件流丢失的情况
*
* @param is
源文件流
* @param is 源文件流
* @param fileName 源文件名称不带后缀,可为null
* @param fileType 源文件后缀
* @return
* @throws FileException
*/
public
static
UploadResultInfo
uploadFileStream
(
InputStream
is
,
String
fileName
,
String
fileType
)
throws
FileException
{
throws
FileException
{
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
UPLOAD
,
null
,
fileType
);
uploadFileStream2OSS
(
is
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
UploadResultInfo
uploadResultInfo
=
getResultInfo
(
outObjectKey
,
null
,
null
,
AliyunConstant
.
FILE_BUCKET
);
...
...
@@ -204,7 +204,7 @@ public class OssUtils {
// }
return
uploadResultInfo
;
}
/**
* 文件流上传到aliOSS
*
...
...
@@ -215,7 +215,7 @@ public class OssUtils {
* @throws FileException
*/
private
static
OSSFileDO
uploadFileStream2OSS
(
InputStream
is
,
String
bucketName
,
String
objectKey
)
throws
FileException
{
throws
FileException
{
if
(
is
==
null
)
{
throw
new
FileException
(
FileException
.
FILE_CONTENT_NOT_EXIST
,
"InputStream is null!"
);
}
...
...
@@ -224,7 +224,7 @@ public class OssUtils {
try
{
client
.
putObject
(
bucketName
,
objectKey
,
is
);
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
bucketName
),
bucketName
,
URLEncoder
.
encode
(
objectKey
,
"utf-8"
));
URLEncoder
.
encode
(
objectKey
,
"utf-8"
));
is
.
close
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【aliOSS】上传文件失败:"
+
e
.
getMessage
(),
e
);
...
...
@@ -234,7 +234,7 @@ public class OssUtils {
}
return
ossFileDO
;
}
/**
* 断点续传上传文件到aliOSS
*
...
...
@@ -244,7 +244,7 @@ public class OssUtils {
* @return
*/
private
static
OSSFileDO
uploadPointFile2OSS
(
String
filePath
,
String
bucketName
,
String
objectKey
)
throws
FileException
{
throws
FileException
{
if
(
filePath
==
null
)
{
throw
new
RuntimeException
(
"filePath is null!"
);
}
...
...
@@ -264,7 +264,7 @@ public class OssUtils {
// 断点续传上传
ossClient
.
uploadFile
(
uploadFileRequest
);
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
bucketName
),
bucketName
,
URLEncoder
.
encode
(
objectKey
,
"utf-8"
));
URLEncoder
.
encode
(
objectKey
,
"utf-8"
));
}
catch
(
Throwable
e
)
{
LOGGER
.
error
(
"【aliOSS】上传文件失败:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_UPLOAD_FAILURE
,
"上传文件失败"
);
...
...
@@ -273,7 +273,7 @@ public class OssUtils {
}
return
ossFileDO
;
}
/**
* 提交MP4转码作业
*
...
...
@@ -286,7 +286,7 @@ public class OssUtils {
String
[]
result
=
submitTranscodeJobMp4Ex
(
fileName
,
filePath
);
return
result
[
0
];
}
/**
* 提交MP4转码作业
*
...
...
@@ -304,11 +304,11 @@ public class OssUtils {
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp4"
);
return
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
}
/**
* 提交MP4转码作业,文件流方式
*
* @param buff
源文件流
* @param buff 源文件流
* @param fileName 源文件的名称不带后缀,可为null
* @param fileType 源文件的后缀
* @return
...
...
@@ -320,10 +320,10 @@ public class OssUtils {
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
inputBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp4"
);
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
return
result
[
0
];
}
/**
* 提交MP3转码作业
*
...
...
@@ -336,7 +336,7 @@ public class OssUtils {
String
[]
result
=
submitTranscodeJobMp3Ex
(
fileName
,
filePath
);
return
result
[
0
];
}
/**
* 提交MP3转码作业
*
...
...
@@ -354,11 +354,11 @@ public class OssUtils {
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp3"
);
return
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
}
/**
* 提交MP3转码作业,文件流方式 <br/> 使用byte[]方式上传,文件流上传容易导致读取长度0,造成转码失败
*
* @param is
源文件流
* @param is 源文件流
* @param fileName 源文件的名称不带后缀,可为null
* @param fileType 源文件的后缀
* @return
...
...
@@ -370,14 +370,14 @@ public class OssUtils {
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
is
,
inputBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp3"
);
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
return
result
[
0
];
}
/**
* 提交MP3转码作业
*
* @param buff
文件
* @param buff 文件
* @param fileName 源文件的名称不带后缀,可为null
* @param fileType 上传的文件后缀
* @return
...
...
@@ -390,10 +390,10 @@ public class OssUtils {
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
inputBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
null
,
"mp3"
);
String
[]
result
=
transcodeJob
(
inputFile
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
return
result
[
0
];
}
/**
* 转码作业,同步的方式
*
...
...
@@ -406,7 +406,7 @@ public class OssUtils {
String
jobId
=
transcodeJob
(
null
,
fileUrl
);
return
transcodeJobResultSync
(
jobId
,
bucketName
);
}
/**
* 转码作业
*
...
...
@@ -419,18 +419,18 @@ public class OssUtils {
String
[]
result
=
transcodeJobEx
(
fileName
,
filePath
,
null
,
null
);
return
result
[
0
];
}
/**
* 转码作业
*
* @param fileName
* @param fileUrl
文件地址
* @param outFileType
输出文件类型
* @param fileUrl 文件地址
* @param outFileType 输出文件类型
* @param waterMarkContent 视频水印文本,如果不需要水印传null
* @return [0]:jobId,[1]:转码后的地址
*/
public
static
String
[]
transcodeJobEx
(
String
fileName
,
String
fileUrl
,
String
outFileType
,
String
waterMarkContent
)
{
String
waterMarkContent
)
{
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
if
(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
OSS_CDN_URLS
))
{
return
transcodeJobExistOSS
(
fileUrl
,
fileName
,
fileType
,
outFileType
,
waterMarkContent
);
...
...
@@ -438,7 +438,7 @@ public class OssUtils {
return
transcodeJobNotExistOSS
(
fileUrl
,
fileName
,
fileType
);
}
}
/**
* 已经存在于OSS中的文件转码
*
...
...
@@ -450,7 +450,7 @@ public class OssUtils {
* @return
*/
private
static
String
[]
transcodeJobExistOSS
(
String
fileUrl
,
String
fileName
,
String
fileType
,
String
outFileType
,
String
waterMarkContent
)
{
String
waterMarkContent
)
{
LOGGER
.
info
(
"【aliOSS】已经存在于OSS中的文件转码.<START>.[fileUrl]="
+
fileUrl
+
",[outFileType]="
+
outFileType
);
String
[]
result
=
null
;
String
inObjectKey
=
splitObjectKey
(
fileUrl
);
...
...
@@ -462,15 +462,15 @@ public class OssUtils {
// 组装水印参数
JSONArray
waterMarkConfigArray
=
getWaterMarkConfig
(
waterMarkContent
);
result
=
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getVideoTemplateId
(
inputBucketName
,
outFileType
),
outObjectKey
,
waterMarkConfigArray
,
null
);
outObjectKey
,
waterMarkConfigArray
,
null
);
}
else
if
(
FileUtils
.
AUDIO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
TRANSCODE
,
fileName
,
"mp3"
);
result
=
transcodeJob
(
ossFileDO
,
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
),
outObjectKey
,
null
,
null
);
null
);
}
return
result
;
}
/**
* 不存在于OSS中的文件转码,要先上传
*
...
...
@@ -488,7 +488,7 @@ public class OssUtils {
}
return
null
;
}
/**
* 获取文件所在的BUCKET名称
*
...
...
@@ -502,11 +502,11 @@ public class OssUtils {
}
return
inputBucketName
;
}
/**
* 文件合并,异步方法
*
* @param fileUrl
主文件地址
* @param fileUrl 主文件地址
* @param mergeUrls 需要合并的子文件列表,最多支持4个
* @return
* @throws FileException
...
...
@@ -530,16 +530,16 @@ public class OssUtils {
}
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
MERGE
,
null
,
fileType
);
String
templateId
=
"mp3"
.
equals
(
fileType
)
?
AliyunConstant
.
getMp3TemplateId
(
inputBucketName
)
:
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
);
:
AliyunConstant
.
getMp4TemplateId
(
inputBucketName
);
String
[]
result
=
transcodeJob
(
ossFileDO
,
templateId
,
outObjectKey
,
null
,
mergeUrls
);
LOGGER
.
info
(
"【aliOSS】提交合并作业.<END>.[result]="
+
JSON
.
toJSONString
(
result
));
return
result
[
0
];
}
/**
* 视频文件合并,同步方法
*
* @param fileUrl
主文件地址
* @param fileUrl 主文件地址
* @param mergeUrls 需要合并的子文件列表,最多支持4个
* @return
* @throws FileException
...
...
@@ -549,11 +549,11 @@ public class OssUtils {
String
jobId
=
submitMergeJob
(
fileUrl
,
mergeUrls
);
return
transcodeJobResultSync
(
jobId
,
bucketName
);
}
/**
* 视频文件合并,同步方法
*
* @param fileUrl
主文件地址
* @param fileUrl 主文件地址
* @param mergeUrls 需要合并的子文件列表,最多支持4个
* @return
* @throws FileException
...
...
@@ -563,7 +563,7 @@ public class OssUtils {
String
jobId
=
submitMergeJob
(
fileUrl
,
mergeUrls
);
return
transcodeJobResultSync
(
jobId
,
bucketName
);
}
/**
* 同步的方式获取转码后的结果
*
...
...
@@ -592,19 +592,19 @@ public class OssUtils {
total
+=
3
;
}
}
/**
* 转码作业
*
* @param inputFile
输入文件
* @param templateId
转码模板
* @param outObjectKey
转码输出对象地址
* @param inputFile 输入文件
* @param templateId 转码模板
* @param outObjectKey 转码输出对象地址
* @param waterMarkConfigArray 水印参数
* @return
* @throws FileException
*/
private
static
String
[]
transcodeJob
(
OSSFileDO
inputFile
,
String
templateId
,
String
outObjectKey
,
JSONArray
waterMarkConfigArray
,
List
<
String
>
mergeUrls
)
throws
FileException
{
JSONArray
waterMarkConfigArray
,
List
<
String
>
mergeUrls
)
throws
FileException
{
try
{
outObjectKey
=
URLEncoder
.
encode
(
outObjectKey
,
"utf-8"
);
}
catch
(
UnsupportedEncodingException
e
)
{
...
...
@@ -643,7 +643,7 @@ public class OssUtils {
throw
new
RuntimeException
(
"SubmitJobsRequest Size failed"
);
}
com
.
aliyuncs
.
mts
.
model
.
v20140618
.
SubmitJobsResponse
.
JobResult
.
Job
job
=
response
.
getJobResultList
().
get
(
0
)
.
getJob
();
.
getJob
();
result
[
0
]
=
job
.
getJobId
();
result
[
1
]
=
getOSSUrl
(
URLDecoder
.
decode
(
outObjectKey
,
"utf-8"
),
outputBucketName
);
}
catch
(
Exception
e
)
{
...
...
@@ -652,7 +652,7 @@ public class OssUtils {
}
return
result
;
}
/**
* 组装视频合并参数
*
...
...
@@ -686,7 +686,7 @@ public class OssUtils {
mergeInput
.
put
(
"MergeList"
,
mergeJsons
);
return
mergeJsons
;
}
/**
* 组装视频水印参数
*
...
...
@@ -710,19 +710,19 @@ public class OssUtils {
waterMarkConfigArray
.
add
(
waterMarkConfig
);
return
waterMarkConfigArray
;
}
/**
* 提交视频剪切作业
*
* @param fileName
* @param fileUrl
* @param seek
开始时间:[0.000,86399.999]
* @param seek 开始时间:[0.000,86399.999]
* @param duration 持续时长 [0.000,86399.999]
* @return
* @throws FileException
*/
public
static
String
submitClipJob
(
String
fileName
,
String
fileUrl
,
String
seek
,
String
duration
)
throws
FileException
{
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<START>.[fileUrl]="
+
fileUrl
+
",[seek]="
+
seek
+
",[duration]="
+
duration
);
if
(
StringUtil
.
isEmpty
(
duration
))
{
return
null
;
...
...
@@ -737,7 +737,7 @@ public class OssUtils {
if
(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
OSS_CDN_URLS
))
{
String
inObjectKey
=
splitObjectKey
(
fileUrl
);
String
inBucketName
=
fileUrlExist001
(
fileUrl
)
?
AliyunConstant
.
INPUT_BUCKET
:
AliyunConstant
.
getBucketName
(
fileUrl
);
:
AliyunConstant
.
getBucketName
(
fileUrl
);
OSSFileDO
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inBucketName
),
inBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
outFileType
);
resultFileUrl
=
clipJob
(
ossFileDO
,
outObjectKey
,
seek
,
duration
);
...
...
@@ -750,43 +750,43 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<END>.[resultFileUrl]="
+
resultFileUrl
);
return
resultFileUrl
;
}
/**
* 提交视频剪切作业
*
* @param buff
* @param fileName
* @param fileType
* @param seek
开始时间:[0.000,86399.999]
* @param seek 开始时间:[0.000,86399.999]
* @param duration 持续时长 [0.000,86399.999]
* @return
* @throws FileException
*/
public
static
String
submitClipJob
(
byte
[]
buff
,
String
fileName
,
String
fileType
,
String
seek
,
String
duration
)
throws
FileException
{
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<START>.[fileName]="
+
fileName
+
",[seek]="
+
seek
+
",[duration]="
+
duration
);
fileName
=
MD5
.
getMD5StrLower
(
fileName
);
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
fileType
);
OSSFileDO
inputFile
=
uploadFileStream2OSS
(
new
ByteArrayInputStream
(
buff
),
AliyunConstant
.
FILE_BUCKET
,
inObjectKey
);
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
,
fileName
,
"mp4"
);
String
resultFileUrl
=
clipJob
(
inputFile
,
outObjectKey
,
seek
,
duration
);
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<END>.[resultFileUrl]="
+
resultFileUrl
);
return
resultFileUrl
;
}
/**
* 视频剪切作业
*
* @param inputFile
* @param outObjectKey
* @param seek
开始时间:[0.000,86399.999]
* @param duration
持续时长 [0.000,86399.999]
* @param seek 开始时间:[0.000,86399.999]
* @param duration 持续时长 [0.000,86399.999]
* @return
* @throws FileException
*/
private
static
String
clipJob
(
OSSFileDO
inputFile
,
String
outObjectKey
,
String
seek
,
String
duration
)
throws
FileException
{
throws
FileException
{
try
{
outObjectKey
=
URLEncoder
.
encode
(
outObjectKey
,
"utf-8"
);
}
catch
(
UnsupportedEncodingException
e
)
{
...
...
@@ -810,7 +810,7 @@ public class OssUtils {
request
.
setPipelineId
(
AliyunConstant
.
getPipelineId
(
inputBucketName
));
request
.
setOutputLocation
(
AliyunConstant
.
getOssRegion
(
inputBucketName
));
Integer
outputJobCount
=
1
;
String
object
=
null
;
try
{
SubmitJobsResponse
response
=
getAcsClient
(
inputBucketName
).
getAcsResponse
(
request
);
...
...
@@ -824,11 +824,11 @@ public class OssUtils {
}
return
getOSSUrl
(
object
,
outputBucketName
);
}
/**
* 组装剪切参数
*
* @param seek
开始时间:sssss[.SSS]
* @param seek 开始时间:sssss[.SSS]
* @param duration 持续时长 sssss[.SSS]
* @return
*/
...
...
@@ -842,12 +842,12 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】组装剪切参数.<END>"
);
return
timeSpanObject
;
}
/**
* 提交视频截图任务,成功返回图片地址 <br>
*
* @param fileUrl
* @param time
秒
* @param time 秒
* @return 成功:图片地址,失败:null
*/
public
static
String
submitSnapshotJob
(
String
fileUrl
,
int
time
)
{
...
...
@@ -871,7 +871,7 @@ public class OssUtils {
SnapshotJob
snapshotJob
=
getAcsClient
(
inputBucketName
).
getAcsResponse
(
request
).
getSnapshotJob
();
if
(
"Success"
.
equalsIgnoreCase
(
snapshotJob
.
getState
()))
{
String
outObject
=
URLDecoder
.
decode
(
snapshotJob
.
getSnapshotConfig
().
getOutputFile
().
getObject
(),
"UTF-8"
);
"UTF-8"
);
return
getOSSUrl
(
outObject
,
outputBucketName
);
}
}
catch
(
Exception
e
)
{
...
...
@@ -879,7 +879,7 @@ public class OssUtils {
}
return
null
;
}
/**
* 组装截图参数
*
...
...
@@ -898,7 +898,7 @@ public class OssUtils {
snapshotConfig
.
put
(
"FrameType"
,
"normal"
);
return
snapshotConfig
;
}
/**
* 组装截图参数
*
...
...
@@ -913,7 +913,7 @@ public class OssUtils {
input
.
put
(
"Object"
,
inputObject
);
return
input
;
}
/**
* 组装转换后的地址(华东地区)
*
...
...
@@ -924,7 +924,7 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】组装转换后的地址(华东地区),<START>.[transcodeJobId]="
+
transcodeJobId
);
return
getOSSResult
(
transcodeJobId
,
AliyunConstant
.
FILE_BUCKET
);
}
/**
* 组装转换后的地址,根据BUCKETNAME
*
...
...
@@ -933,13 +933,14 @@ public class OssUtils {
* @throws FileException
*/
public
static
UploadResultInfo
getOSSResultByRegion
(
String
transcodeJobId
,
String
buckectName
)
throws
FileException
{
if
(
AliyunConstant
.
LIVE_BUCKET
.
equals
(
buckectName
))
throws
FileException
{
if
(
AliyunConstant
.
LIVE_BUCKET
.
equals
(
buckectName
))
{
return
getOSSResult
(
transcodeJobId
,
AliyunConstant
.
LIVE_BUCKET
);
else
}
else
{
return
getOSSResult
(
transcodeJobId
,
AliyunConstant
.
FILE_BUCKET
);
}
}
/**
* 组装转换后的地址
*
...
...
@@ -954,7 +955,7 @@ public class OssUtils {
Job
transcodeJob
=
getTranscodeJob
(
transcodeJobId
,
outputBucketName
);
return
explainTranscodeResult
(
transcodeJob
);
}
/**
* 获取转换作业情况
*
...
...
@@ -973,7 +974,7 @@ public class OssUtils {
throw
new
FileException
(
FileException
.
FILE_CONVERT_FAIL
,
"获取转换作业情况失败"
);
}
}
/**
* 解析转换后的结果
*
...
...
@@ -1015,7 +1016,7 @@ public class OssUtils {
}
return
null
;
}
/**
* 获取媒体信息,仅限经过转码的音视频文件
*
...
...
@@ -1050,11 +1051,11 @@ public class OssUtils {
ossMediaInfoDTO
.
setObejctKey
(
objectKey
);
ossMediaInfoDTO
.
setSize
(
StringUtil
.
isEmpty
(
format
.
getSize
())
?
0L
:
Long
.
parseLong
(
format
.
getSize
()));
ossMediaInfoDTO
.
setDuration
(
StringUtil
.
isEmpty
(
format
.
getDuration
())
?
0
:
new
Double
(
format
.
getDuration
()).
intValue
());
StringUtil
.
isEmpty
(
format
.
getDuration
())
?
0
:
new
Double
(
format
.
getDuration
()).
intValue
());
LOGGER
.
info
(
"【aliOSS】获取媒体信息,仅限经过转码的音视频文件,<END>.[ossMediaInfoDTO]="
+
ossMediaInfoDTO
);
return
ossMediaInfoDTO
;
}
/**
* 获取文件的元信息
*
...
...
@@ -1069,7 +1070,7 @@ public class OssUtils {
ossClient
.
shutdown
();
return
objectMetadata
;
}
/**
* 下载文件成byte[]
*
...
...
@@ -1096,25 +1097,28 @@ public class OssUtils {
os
.
write
(
buff
,
0
,
rc
);
}
result
=
os
.
toByteArray
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【aliOSS】下载文件失败:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
finally
{
try
{
if
(
is
!=
null
)
if
(
is
!=
null
)
{
is
.
close
();
if
(
os
!=
null
)
}
if
(
os
!=
null
)
{
os
.
close
();
if
(
ossClient
!=
null
)
}
if
(
ossClient
!=
null
)
{
ossClient
.
shutdown
();
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【aliOSS】下载文件,关闭文件流失败:"
+
e
.
getMessage
(),
e
);
}
}
return
result
;
}
/**
* 下载文件到本地
*
...
...
@@ -1134,11 +1138,12 @@ public class OssUtils {
LOGGER
.
error
(
"【aliOSS】下载文件失败:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
finally
{
if
(
ossClient
!=
null
)
if
(
ossClient
!=
null
)
{
ossClient
.
shutdown
();
}
}
}
/**
* 组装返回的文件信息 <br> isPrivateKey=true,表示地址可下载,但需要加上密钥,密钥生产使用方法getHashValue或者addUrlKey <br>
*
...
...
@@ -1149,7 +1154,7 @@ public class OssUtils {
* @return
*/
private
static
UploadResultInfo
getResultInfo
(
String
objectKey
,
Long
size
,
String
duration
,
String
outputBucketName
)
{
String
outputBucketName
)
{
UploadResultInfo
uploadResultInfo
=
new
UploadResultInfo
();
uploadResultInfo
.
setObjectKey
(
objectKey
);
uploadResultInfo
.
setUrl
(
getOSSUrl
(
objectKey
,
outputBucketName
));
...
...
@@ -1167,7 +1172,7 @@ public class OssUtils {
}
return
uploadResultInfo
;
}
/**
* 组装转换后的地址
*
...
...
@@ -1181,7 +1186,7 @@ public class OssUtils {
}
return
AliyunConstant
.
getOSSCDNUrl
(
outputBucketName
)
+
objectKey
;
}
/**
* 从完整的HTTP路径中截取objectName
*
...
...
@@ -1214,7 +1219,7 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】从完整的HTTP路径中截取objectKey,<END>.[fileUrl]="
+
fileUrl
);
return
fileUrl
;
}
/**
* 组装输入文件Object路径
*
...
...
@@ -1224,14 +1229,9 @@ public class OssUtils {
* @return
*/
private
static
String
getInObjectKey
(
AliyunEnum
aliyunEnum
,
String
fileName
,
String
fileType
)
{
fileName
=
StringUtil
.
isEmpty
(
fileName
)
?
UUIDUitl
.
taskName
()
:
(
FileUtils
.
formatName
(
fileName
)
+
"_"
+
LocalDateUtils
.
getYmdhmss
());
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
gatherName
=
StringUtil
.
isEmpty
(
gatherName
)
?
"other"
:
gatherName
.
toLowerCase
();
return
new
StringBuilder
(
"oss/"
).
append
(
aliyunEnum
.
value
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
).
append
(
fileType
)
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
return
getObjectKey
(
aliyunEnum
,
fileName
,
fileType
);
}
/**
* 组装输出文件Object路径
*
...
...
@@ -1241,14 +1241,26 @@ public class OssUtils {
* @return
*/
private
static
String
getOutObjectKey
(
AliyunEnum
aliyunEnum
,
String
fileName
,
String
fileType
)
{
return
getObjectKey
(
aliyunEnum
,
fileName
,
fileType
);
}
/**
* 组装ObjectKey
*
* @param aliyunEnum
* @param fileName
* @param fileType
* @return
*/
private
static
String
getObjectKey
(
AliyunEnum
aliyunEnum
,
String
fileName
,
String
fileType
)
{
fileName
=
StringUtil
.
isEmpty
(
fileName
)
?
UUIDUitl
.
taskName
()
:
(
FileUtils
.
formatName
(
fileName
)
+
"_"
+
LocalDateUtils
.
getYmdhmss
());
:
(
FileUtils
.
formatName
(
fileName
)
+
"_"
+
LocalDateUtils
.
getYmdhmss
());
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
gatherName
=
StringUtil
.
isEmpty
(
gatherName
)
?
"other"
:
gatherName
.
toLowerCase
();
return
new
StringBuilder
(
"oss/"
).
append
(
aliyunEnum
.
value
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
)
.
append
(
fileType
)
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
return
new
StringBuilder
(
"oss/"
).
append
(
aliyunEnum
.
value
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
)
.
append
(
fileType
)
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
}
/**
* 获取文件下载加密密钥,默认有效期48小时 <br> 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
*
...
...
@@ -1258,7 +1270,7 @@ public class OssUtils {
public
static
String
getHashValue
(
String
fileUrl
)
{
return
getHashValue
(
fileUrl
,
AliyunConstant
.
OSS_DEFAULT_TIME
);
}
/**
* 获取文件下载加密密钥,长期有效 <br> 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
*
...
...
@@ -1268,12 +1280,12 @@ public class OssUtils {
public
static
String
getHashValueLong
(
String
fileUrl
)
{
return
getHashValue
(
fileUrl
,
AliyunConstant
.
OSS_LONG_TIME
);
}
/**
* 获取文件下载加密密钥 <br> 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
*
* @param fileUrl
* @param second
秒,地址有效期
* @param second 秒,地址有效期
* @return
*/
public
static
String
getHashValue
(
String
fileUrl
,
long
second
)
{
...
...
@@ -1288,7 +1300,7 @@ public class OssUtils {
String
key
=
MD5
.
getMD5StrLower
(
objectKey
+
"-"
+
timestamp
+
"-0-0-"
+
AliyunConstant
.
OSS_PRIVATE_KEY
);
return
"?auth_key="
+
timestamp
+
"-0-0-"
+
key
;
}
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br> 默认有效期48小时
*
...
...
@@ -1298,7 +1310,7 @@ public class OssUtils {
public
static
String
urlAddKey
(
String
fileUrl
)
{
return
urlAddKey
(
fileUrl
,
AliyunConstant
.
OSS_DEFAULT_TIME
);
}
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br> 长期有效
*
...
...
@@ -1308,37 +1320,37 @@ public class OssUtils {
public
static
String
urlAddKeyLong
(
String
fileUrl
)
{
return
urlAddKey
(
fileUrl
,
AliyunConstant
.
OSS_LONG_TIME
);
}
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 <br>
*
* @param fileUrl 文件URL全路径地址
* @param second
秒,地址有效期
* @param second 秒,地址有效期
* @return
*/
public
static
String
urlAddKey
(
String
fileUrl
,
long
second
)
{
String
result
=
keyCheckFile
(
fileUrl
);
return
ZSEncode
.
encodeURI
(
result
)
+
getHashValue
(
result
,
second
);
}
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问(不转义中文) <br>
*
* @param fileUrl 文件URL全路径地址
* @param second
秒,地址有效期
* @param second 秒,地址有效期
* @return
*/
public
static
String
urlAddKeyChina
(
String
fileUrl
,
long
second
)
{
String
result
=
keyCheckFile
(
fileUrl
);
return
result
+
getHashValue
(
result
,
second
);
}
private
static
String
keyCheckFile
(
String
fileUrl
)
{
if
(
StringUtil
.
isEmpty
(
fileUrl
))
{
return
null
;
}
if
(!(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
RAYS_CDN_URLS
)
||
fileUrl
.
contains
(
AliyunConstant
.
FILE_CDN_URL_DOWNLOAD
)))
{
||
fileUrl
.
contains
(
AliyunConstant
.
FILE_CDN_URL_DOWNLOAD
)))
{
return
fileUrl
;
}
if
(
fileUrl
.
contains
(
"?auth_key="
))
{
...
...
@@ -1347,7 +1359,7 @@ public class OssUtils {
// 替换域名为可访问的
return
StringTools
.
replace
(
fileUrl
,
AliyunConstant
.
RAYS_CDN_URLS
,
AliyunConstant
.
FILE_CDN_URL_DOWNLOAD
);
}
/**
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br> 默认有效期48小时
*
...
...
@@ -1357,7 +1369,7 @@ public class OssUtils {
public
static
String
urlAddKey2Sms
(
String
fileUrl
)
{
return
urlAddKey2Sms
(
fileUrl
,
AliyunConstant
.
OSS_DEFAULT_TIME
);
}
/**
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br> 长期有效
*
...
...
@@ -1367,12 +1379,12 @@ public class OssUtils {
public
static
String
urlAddKeyLong2Sms
(
String
fileUrl
)
{
return
urlAddKey2Sms
(
fileUrl
,
AliyunConstant
.
OSS_LONG_TIME
);
}
/**
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br>
*
* @param fileUrl 文件URL全路径地址
* @param second
秒,地址有效期
* @param second 秒,地址有效期
* @return
*/
public
static
String
urlAddKey2Sms
(
String
fileUrl
,
long
second
)
{
...
...
@@ -1380,7 +1392,7 @@ public class OssUtils {
String
shortUrl
=
UrlUtils
.
getShortUrl
(
keyUrl
);
return
StringUtil
.
isEmpty
(
shortUrl
)
?
keyUrl
:
shortUrl
.
replace
(
UrlConstant
.
TB_SHORT_URL
,
""
);
}
/**
* 将文件URL生成带密钥的下载地址,返回的地址不带域名 <br>
*
...
...
@@ -1392,7 +1404,7 @@ public class OssUtils {
String
shortUrl
=
UrlUtils
.
getShortUrl4Own
(
keyUrl
);
return
StringUtil
.
isEmpty
(
shortUrl
)
?
keyUrl
:
shortUrl
.
replace
(
UrlConstant
.
OWN_SHORT_URL
,
""
);
}
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问,并转换为短链接<br>
*
...
...
@@ -1402,7 +1414,7 @@ public class OssUtils {
public
static
String
urlAddKey2Short
(
String
fileUrl
)
{
return
urlAddKey2Short
(
fileUrl
,
AliyunConstant
.
OSS_DEFAULT_TIME
);
}
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问 ,并转换为短链接<br> 长期有效
*
...
...
@@ -1412,12 +1424,12 @@ public class OssUtils {
public
static
String
urlAddKeyLong2Short
(
String
fileUrl
)
{
return
urlAddKey2Short
(
fileUrl
,
AliyunConstant
.
OSS_LONG_TIME
);
}
/**
* 将文件URL生成带密钥的地址,可以在浏览器直接访问,并转换为短链接<br>
*
* @param fileUrl 文件URL全路径地址
* @param second
秒,地址有效期
* @param second 秒,地址有效期
* @return
*/
public
static
String
urlAddKey2Short
(
String
fileUrl
,
long
second
)
{
...
...
@@ -1425,12 +1437,12 @@ public class OssUtils {
String
shortUrl
=
UrlUtils
.
getShortUrl4Own
(
keyUrl
);
return
StringUtil
.
isEmpty
(
shortUrl
)
?
keyUrl
:
shortUrl
;
}
/**
* 图片缩放,按宽度等比例缩放
*
* @param fileUrl
* @param width
目前最大支持1024,超过1024或者小于0都按1024计算
* @param width 目前最大支持1024,超过1024或者小于0都按1024计算
* @return 返回处理后的图片本地地址
* @throws FileException
*/
...
...
@@ -1448,12 +1460,12 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】图片缩放,<START>.[localPath]="
+
localPath
);
return
localPath
;
}
/**
* 图片缩放,按倍数百分比缩放
*
* @param fileUrl
* @param p
1-1000,倍数百分比。 小于100,即是缩小,大于100即是放大。
* @param p 1-1000,倍数百分比。 小于100,即是缩小,大于100即是放大。
* @return 返回处理后的图片本地地址
* @throws FileException
*/
...
...
@@ -1471,12 +1483,12 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】图片缩放,<START>.[localPath]="
+
localPath
);
return
localPath
;
}
/**
* 图片自动旋转
*
* @param fileUrl
* @param width
图片旋转以后的宽度,最大不能超过4096,传0则按原始宽度旋转
* @param width 图片旋转以后的宽度,最大不能超过4096,传0则按原始宽度旋转
* @return 返回处理后的图片本地地址
* @throws FileException
*/
...
...
@@ -1497,15 +1509,15 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】图片自动旋转,<START>.[localPath]="
+
localPath
);
return
localPath
;
}
/**
* 图片裁剪,如果图片带有旋转属性,会自动旋转纠正角度后在进行裁剪。
*
* @param fileUrl
* @param width
裁剪的宽度
* @param height
裁剪的高度
* @param x
左上角开始, 裁剪的X轴坐标
* @param y
左上角开始, 裁剪的Y轴坐标
* @param width 裁剪的宽度
* @param height 裁剪的高度
* @param x 左上角开始, 裁剪的X轴坐标
* @param y 左上角开始, 裁剪的Y轴坐标
* @return 返回处理后的图片本地地址
* @throws FileException
*/
...
...
@@ -1525,7 +1537,7 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】图片裁剪,<END>.[localPath]="
+
localPath
);
return
localPath
;
}
/**
* <持久化>图片裁剪,如果图片带有旋转属性,会自动旋转纠正角度后在进行裁剪。
*
...
...
@@ -1537,13 +1549,13 @@ public class OssUtils {
* @return
* @throws FileException
*/
public
static
OssImageCropDTO
imageCropSaveas
(
String
fileUrl
,
int
width
,
int
height
,
int
x
,
int
y
)
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
);
...
...
@@ -1553,7 +1565,8 @@ public class OssUtils {
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
()));
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
,
null
,
fileType
);
style
.
append
(
",o_"
).
append
(
BinaryUtil
.
toBase64String
(
outObjectKey
.
getBytes
()));
style
.
append
(
",b_"
).
append
(
BinaryUtil
.
toBase64String
(
bucketName
.
getBytes
()));
ProcessObjectRequest
request
=
new
ProcessObjectRequest
(
bucketName
,
sourceKey
,
style
.
toString
());
// 创建OSSClient实例
...
...
@@ -1571,7 +1584,7 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】图片裁剪持久化,<END>.[ossImageCropDTO]="
+
ossImageCropDTO
);
return
ossImageCropDTO
;
}
/**
* 图片处理
*
...
...
@@ -1599,7 +1612,7 @@ public class OssUtils {
}
return
localPath
;
}
/**
* 图片增加水印
*
...
...
@@ -1624,7 +1637,7 @@ public class OssUtils {
FileUtils
.
deleteFile
(
localPath
);
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
}
/**
* 图片增加水印(文本)
*
...
...
@@ -1666,11 +1679,11 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】图片增加水印(文本),<END>.[result]="
+
result
);
return
result
;
}
/**
* 图片增加水印(图片)
*
* @param fileUrl
原图地址
* @param fileUrl 原图地址
* @param watermarkImageUrl 水印图片地址
* @return
* @throws FileException
...
...
@@ -1690,7 +1703,7 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】图片增加水印(图片),<END>.[result]="
+
result
);
return
result
;
}
/**
* 图片水印处理
*
...
...
@@ -1708,7 +1721,7 @@ public class OssUtils {
OSSClient
ossClient
=
getOSSClient
(
bucketName
);
// 生成带水印的本地图片地址
String
localPath
=
FilePathConst
.
IMAGE_WATERMARK_PATH
+
fileName
+
"_"
+
LocalDateUtils
.
getYmdhmss
()
+
"."
+
fileType
;
+
fileType
;
FileUtils
.
creatFiles
(
localPath
);
ossClient
.
getObject
(
request
,
new
File
(
localPath
));
ossClient
.
shutdown
();
...
...
@@ -1717,7 +1730,7 @@ public class OssUtils {
FileUtils
.
deleteFile
(
localPath
);
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
}
/**
* 检查图片文件合法性,如果是本地文件先上传
*
...
...
@@ -1742,7 +1755,7 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】检查文件合法性,<END>.[fileUrl]="
+
fileUrl
);
return
fileUrl
;
}
/**
* 计算图片水印文字大小
*
...
...
@@ -1756,7 +1769,7 @@ public class OssUtils {
LOGGER
.
info
(
"【aliOSS】计算图片水印文字大小,<END>.[result]="
+
result
);
return
Math
.
round
(
result
);
}
/**
* 获取OSS客户端
*
...
...
@@ -1764,9 +1777,9 @@ public class OssUtils {
*/
private
static
OSSClient
getOSSClient
(
String
bucketName
)
{
return
new
OSSClient
(
AliyunConstant
.
getOSSEndPoint
(
bucketName
),
AliyunConstant
.
MAIN_ACCESS_KEY_ID
,
AliyunConstant
.
MAIN_ACCESS_KEY_SECRET
);
AliyunConstant
.
MAIN_ACCESS_KEY_SECRET
);
}
/**
* 判断object是否存在
*
...
...
@@ -1774,14 +1787,14 @@ public class OssUtils {
* @param objectKey
* @return
*/
private
static
boolean
doesO
bjectExist
(
String
bucketName
,
String
objectKey
)
{
private
static
boolean
o
bjectExist
(
String
bucketName
,
String
objectKey
)
{
OSSClient
ossClient
=
getOSSClient
(
bucketName
);
boolean
found
=
ossClient
.
doesObjectExist
(
bucketName
,
objectKey
);
// 关闭client
ossClient
.
shutdown
();
return
found
;
}
/**
* 判断文件是存在001中
*
...
...
@@ -1789,14 +1802,10 @@ public class OssUtils {
* @return true:001,false:002
*/
public
static
boolean
fileUrlExist001
(
String
fileUrl
)
{
boolean
isUploadfe
=
false
;
String
objectKey
=
splitObjectKey
(
fileUrl
);
if
(
doesObjectExist
(
AliyunConstant
.
INPUT_BUCKET
,
objectKey
))
{
isUploadfe
=
true
;
}
return
isUploadfe
;
return
objectExist
(
AliyunConstant
.
INPUT_BUCKET
,
objectKey
);
}
/**
* 判断文件是存在002中
*
...
...
@@ -1804,12 +1813,8 @@ public class OssUtils {
* @return true:001,false:002
*/
public
static
boolean
fileUrlExist002
(
String
fileUrl
)
{
boolean
isUploadfe
=
false
;
String
objectKey
=
splitObjectKey
(
fileUrl
);
if
(
doesObjectExist
(
AliyunConstant
.
FILE_BUCKET
,
objectKey
))
{
isUploadfe
=
true
;
}
return
isUploadfe
;
return
objectExist
(
AliyunConstant
.
FILE_BUCKET
,
objectKey
);
}
}
pcloud-common/src/main/java/com/pcloud/common/utils/httpclient/UrlUtils.java
View file @
391cbb85
package
com
.
pcloud
.
common
.
utils
.
httpclient
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.pcloud.common.utils.rsa.MD5
;
import
com.pcloud.common.utils.string.StringUtil
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
...
...
@@ -15,175 +14,198 @@ import org.apache.http.util.EntityUtils;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.pcloud.common.utils.rsa.MD5
;
import
com.pcloud.common.utils.string.StringUtil
;
/**
* @author:songx
* @date:2017年12月4日,下午4:58:39
*/
public
class
UrlUtils
{
/**
*
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
UrlUtils
.
class
);
/**
* 请求地址(新郎接口)
*/
private
static
final
String
REQUEST_URL
=
"https://api.weibo.com/2/short_url/shorten.json?source=1681459862&url_long="
;
/**
* 请求地址(OWN)
*/
private
static
final
String
REQUEST_URL2
=
"https://rays.5rs.me/convert/v1.0/url/shorten"
;
/**
* 自定义生成 MD5 加密字符传前的混合 KEY
*/
public
static
final
String
PRIVATE_KEY
=
"lgsc1205"
;
/**
* 获取短链接(新浪)
*
* @param long_url
* @return
*/
public
static
String
getShortUrl
(
String
long_url
)
{
String
result
=
callHttp
(
long_url
);
if
(
StringUtil
.
isEmpty
(
result
))
{
return
null
;
}
JSONObject
resultObject
=
JSONObject
.
parseObject
(
result
);
JSONArray
array
=
resultObject
.
getJSONArray
(
"urls"
);
JSONObject
object
=
array
.
getJSONObject
(
0
);
return
object
.
getString
(
"url_short"
);
}
/**
* 发送请求(新浪)
*
* @param long_url
* @return
*/
private
static
String
callHttp
(
String
long_url
)
{
LOGGER
.
info
(
"【URL】短链接转换(新浪),<START>.[long_url]="
+
long_url
);
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
HttpGet
httpGet
=
new
HttpGet
(
REQUEST_URL
+
long_url
);
String
resContent
=
null
;
try
{
CloseableHttpResponse
response
=
httpclient
.
execute
(
httpGet
);
// 获取响应实体
HttpEntity
entity
=
response
.
getEntity
();
if
(
entity
!=
null
)
{
resContent
=
EntityUtils
.
toString
(
entity
);
}
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"【URL】短链接转换失败(新浪):"
+
e
.
getMessage
(),
e
);
}
LOGGER
.
info
(
"【URL】短链接转换(新浪),<END>.[resContent]="
+
resContent
);
return
resContent
;
}
/**
* 获取短链接(OWN)
*
* @param long_url
* @return
*/
public
static
String
getShortUrl4Own
(
String
long_url
)
{
String
result
=
callHttp4Own
(
long_url
);
if
(
StringUtil
.
isEmpty
(
result
))
{
return
null
;
}
JSONObject
resultObject
=
JSONObject
.
parseObject
(
result
);
JSONObject
dataObject
=
resultObject
.
getJSONObject
(
"data"
);
return
dataObject
==
null
?
null
:
dataObject
.
getString
(
"shortUrl"
);
}
/**
* 发送请求(OWN)
*
* @param long_url
* @return
*/
private
static
String
callHttp4Own
(
String
long_url
)
{
LOGGER
.
info
(
"【URL】短链接转换(OWN),<START>.[long_url]="
+
long_url
);
if
(
StringUtil
.
isEmpty
(
long_url
))
{
return
null
;
}
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
HttpPost
httpPost
=
new
HttpPost
(
REQUEST_URL2
);
String
resContent
=
null
;
try
{
// 设置参数到请求对象中
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"originUrl"
,
long_url
);
StringEntity
stringEntity
=
new
StringEntity
(
jsonObject
.
toJSONString
(),
Charset
.
forName
(
"UTF-8"
));
httpPost
.
setEntity
(
stringEntity
);
// 设置header信息
// 指定报文头【Content-type】、【User-Agent】
httpPost
.
setHeader
(
"Content-type"
,
"application/json;charset=UTF-8"
);
CloseableHttpResponse
response
=
httpclient
.
execute
(
httpPost
);
// 获取响应实体
HttpEntity
entity
=
response
.
getEntity
();
if
(
entity
!=
null
)
{
resContent
=
EntityUtils
.
toString
(
entity
);
}
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"【URL】短链接转换失败(OWN):"
+
e
.
getMessage
(),
e
);
}
LOGGER
.
info
(
"【URL】短链接转换(OWN),<END>.[resContent]="
+
resContent
);
return
resContent
;
}
/**
* 对传入的链接进行加密
*
* @param url
* @return
*/
public
static
String
[]
shortenUrl
(
String
url
)
{
// 对传入网址进行 MD5 加密
String
hex
=
MD5
.
getMD5Str
(
PRIVATE_KEY
+
url
);
return
shortenUrlMd5
(
hex
);
}
/**
* 缩短链接
*
* @param url
* @return
*/
public
static
String
[]
shortenUrlMd5
(
String
hex
)
{
// 要使用生成 URL 的字符
String
[]
chars
=
new
String
[]
{
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
,
"g"
,
"h"
,
"i"
,
"j"
,
"k"
,
"l"
,
"m"
,
"n"
,
"o"
,
"p"
,
"q"
,
"r"
,
"s"
,
"t"
,
"u"
,
"v"
,
"w"
,
"x"
,
"y"
,
"z"
,
"0"
,
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"A"
,
"B"
,
"C"
,
"D"
,
"E"
,
"F"
,
"G"
,
"H"
,
"I"
,
"J"
,
"K"
,
"L"
,
"M"
,
"N"
,
"O"
,
"P"
,
"Q"
,
"R"
,
"S"
,
"T"
,
"U"
,
"V"
,
"W"
,
"X"
,
"Y"
,
"Z"
};
String
[]
resUrl
=
new
String
[
4
];
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
// 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算
String
sTempSubString
=
hex
.
substring
(
i
*
8
,
i
*
8
+
8
);
// 这里需要使用 long 型来转换,因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 ,
// 如果不用long ,则会越界
long
lHexLong
=
0x3FFFFFFF
&
Long
.
parseLong
(
sTempSubString
,
16
);
String
outChars
=
""
;
for
(
int
j
=
0
;
j
<
7
;
j
++)
{
// 把得到的值与 0x0000003D 进行位与运算,取得字符数组 chars 索引
long
index
=
0x0000003D
&
lHexLong
;
// 把取得的字符相加
outChars
+=
chars
[(
int
)
index
];
// 每次循环按位右移 4 位
lHexLong
=
lHexLong
>>
4
;
}
// 把字符串存入对应索引的输出数组
resUrl
[
i
]
=
outChars
;
}
return
resUrl
;
}
/**
*
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
UrlUtils
.
class
);
/**
* 请求地址(新郎接口)
*/
private
static
final
String
REQUEST_URL
=
"https://api.weibo.com/2/short_url/shorten.json?source=1681459862&url_long="
;
/**
* 请求地址(OWN)
*/
private
static
final
String
REQUEST_URL2
=
"https://rays.5rs.me/convert/v1.0/url/shorten"
;
/**
* 请求地址(OWN)
*/
private
static
final
String
REQUEST_URL3
=
"https://rays.5rs.me/convert/v1.0/url/expand?hex="
;
/**
* 自定义生成 MD5 加密字符传前的混合 KEY
*/
public
static
final
String
PRIVATE_KEY
=
"lgsc1205"
;
/**
* 获取短链接(新浪)
*
* @param long_url
* @return
*/
public
static
String
getShortUrl
(
String
long_url
)
{
String
result
=
callHttp
(
REQUEST_URL
+
long_url
);
if
(
StringUtil
.
isEmpty
(
result
))
{
return
null
;
}
JSONObject
resultObject
=
JSONObject
.
parseObject
(
result
);
JSONArray
array
=
resultObject
.
getJSONArray
(
"urls"
);
JSONObject
object
=
array
.
getJSONObject
(
0
);
return
object
.
getString
(
"url_short"
);
}
/**
* 发送请求(新浪)
*
* @param request
* @return
*/
private
static
String
callHttp
(
String
request
)
{
LOGGER
.
info
(
"【URL】短链接转换(新浪),<START>.[request]="
+
request
);
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
HttpGet
httpGet
=
new
HttpGet
(
request
);
String
resContent
=
null
;
try
{
CloseableHttpResponse
response
=
httpclient
.
execute
(
httpGet
);
// 获取响应实体
HttpEntity
entity
=
response
.
getEntity
();
if
(
entity
!=
null
)
{
resContent
=
EntityUtils
.
toString
(
entity
);
}
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"【URL】短链接转换失败(新浪):"
+
e
.
getMessage
(),
e
);
}
LOGGER
.
info
(
"【URL】短链接转换(新浪),<END>.[resContent]="
+
resContent
);
return
resContent
;
}
/**
* 获取长链接(OWN)
*
* @param hex
* @return
*/
public
static
String
getUrl4Own
(
String
hex
)
{
String
result
=
callHttp
(
REQUEST_URL3
+
hex
);
if
(
StringUtil
.
isEmpty
(
result
))
{
return
null
;
}
JSONObject
resultObject
=
JSONObject
.
parseObject
(
result
);
JSONObject
dataObject
=
resultObject
.
getJSONObject
(
"data"
);
return
dataObject
.
getString
(
"originUrl"
);
}
/**
* 获取短链接(OWN)
*
* @param long_url
* @return
*/
public
static
String
getShortUrl4Own
(
String
long_url
)
{
String
result
=
callHttp4Own
(
long_url
);
if
(
StringUtil
.
isEmpty
(
result
))
{
return
null
;
}
JSONObject
resultObject
=
JSONObject
.
parseObject
(
result
);
JSONObject
dataObject
=
resultObject
.
getJSONObject
(
"data"
);
return
dataObject
==
null
?
null
:
dataObject
.
getString
(
"shortUrl"
);
}
/**
* 发送请求(OWN)
*
* @param long_url
* @return
*/
private
static
String
callHttp4Own
(
String
long_url
)
{
LOGGER
.
info
(
"【URL】短链接转换(OWN),<START>.[long_url]="
+
long_url
);
if
(
StringUtil
.
isEmpty
(
long_url
))
{
return
null
;
}
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
HttpPost
httpPost
=
new
HttpPost
(
REQUEST_URL2
);
String
resContent
=
null
;
try
{
// 设置参数到请求对象中
JSONObject
jsonObject
=
new
JSONObject
();
jsonObject
.
put
(
"originUrl"
,
long_url
);
StringEntity
stringEntity
=
new
StringEntity
(
jsonObject
.
toJSONString
(),
Charset
.
forName
(
"UTF-8"
));
httpPost
.
setEntity
(
stringEntity
);
// 设置header信息
// 指定报文头【Content-type】、【User-Agent】
httpPost
.
setHeader
(
"Content-type"
,
"application/json;charset=UTF-8"
);
CloseableHttpResponse
response
=
httpclient
.
execute
(
httpPost
);
// 获取响应实体
HttpEntity
entity
=
response
.
getEntity
();
if
(
entity
!=
null
)
{
resContent
=
EntityUtils
.
toString
(
entity
);
}
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"【URL】短链接转换失败(OWN):"
+
e
.
getMessage
(),
e
);
}
LOGGER
.
info
(
"【URL】短链接转换(OWN),<END>.[resContent]="
+
resContent
);
return
resContent
;
}
/**
* 对传入的链接进行加密
*
* @param url
* @return
*/
public
static
String
[]
shortenUrl
(
String
url
)
{
// 对传入网址进行 MD5 加密
String
hex
=
MD5
.
getMD5Str
(
PRIVATE_KEY
+
url
);
return
shortenUrlMd5
(
hex
);
}
/**
* 缩短链接
*
* @param hex
* @return
*/
public
static
String
[]
shortenUrlMd5
(
String
hex
)
{
// 要使用生成 URL 的字符
String
[]
chars
=
new
String
[]{
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
,
"g"
,
"h"
,
"i"
,
"j"
,
"k"
,
"l"
,
"m"
,
"n"
,
"o"
,
"p"
,
"q"
,
"r"
,
"s"
,
"t"
,
"u"
,
"v"
,
"w"
,
"x"
,
"y"
,
"z"
,
"0"
,
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
,
"A"
,
"B"
,
"C"
,
"D"
,
"E"
,
"F"
,
"G"
,
"H"
,
"I"
,
"J"
,
"K"
,
"L"
,
"M"
,
"N"
,
"O"
,
"P"
,
"Q"
,
"R"
,
"S"
,
"T"
,
"U"
,
"V"
,
"W"
,
"X"
,
"Y"
,
"Z"
};
String
[]
resUrl
=
new
String
[
4
];
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
// 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算
String
sTempSubString
=
hex
.
substring
(
i
*
8
,
i
*
8
+
8
);
// 这里需要使用 long 型来转换,因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 ,
// 如果不用long ,则会越界
long
lHexLong
=
0x3FFFFFFF
&
Long
.
parseLong
(
sTempSubString
,
16
);
String
outChars
=
""
;
for
(
int
j
=
0
;
j
<
7
;
j
++)
{
// 把得到的值与 0x0000003D 进行位与运算,取得字符数组 chars 索引
long
index
=
0x0000003D
&
lHexLong
;
// 把取得的字符相加
outChars
+=
chars
[(
int
)
index
];
// 每次循环按位右移 4 位
lHexLong
=
lHexLong
>>
4
;
}
// 把字符串存入对应索引的输出数组
resUrl
[
i
]
=
outChars
;
}
return
resUrl
;
}
}
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