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
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
796 additions
and
670 deletions
+796
-670
FileUtils.java
...mmon/src/main/java/com/pcloud/common/utils/FileUtils.java
+670
-659
ImageUtils.java
...mon/src/main/java/com/pcloud/common/utils/ImageUtils.java
+126
-11
OssUtils.java
...rc/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
+0
-0
No files found.
pcloud-common/src/main/java/com/pcloud/common/utils/FileUtils.java
View file @
9c92af97
...
...
@@ -290,151 +290,151 @@ public class FileUtils {
return
false
;
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做
*
* @param filePath
*/
public
static
boolean
creatDir
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
}
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
file
.
mkdir
();
return
true
;
}
return
false
;
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做 该方法能够实现创建整个路径
*
* @param filePath
*/
public
static
boolean
creatDirs
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
}
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
file
.
mkdirs
();
return
true
;
}
return
false
;
}
/**
* 获取文件类型
*
* @param filePath
*/
public
static
String
getFileType
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
int
index1
=
filePath
.
lastIndexOf
(
"."
);
if
(
index1
<
1
)
{
return
null
;
}
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
if
(
index2
>
index1
)
{
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
}
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
if
(
index3
>
index1
)
{
return
null
;
}
return
filePath
.
substring
(
filePath
.
lastIndexOf
(
"."
)
+
1
,
filePath
.
length
()).
toLowerCase
();
}
/**
* 获取文件名称
*
* @param filePath
*/
public
static
String
getFileName
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
String
fileName
=
null
;
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
int
index1
=
filePath
.
lastIndexOf
(
"."
);
if
(
index1
>
-
1
)
{
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
if
(
index2
>
index1
)
{
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
}
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
if
(
index3
>
index1
)
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
}
else
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
,
filePath
.
lastIndexOf
(
"."
));
}
}
else
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
}
return
formatName
(
fileName
);
}
/**
* 获取文件名称包括后缀
*
* @param filePath
* @return
*/
public
static
String
getFileNameAll
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
String
fileName
=
getFileName
(
filePath
);
if
(
StringUtil
.
isEmpty
(
fileName
))
{
return
null
;
}
String
fileType
=
getFileType
(
filePath
);
return
StringUtil
.
isEmpty
(
fileType
)
?
fileName
:
fileName
+
"."
+
fileType
;
}
/**
* 格式化文件名称,过滤特殊字符(名称太长进行截断)
*
* @param fileName
*/
public
static
String
formatName
(
String
fileName
)
{
if
(
StringUtil
.
isEmpty
(
fileName
))
{
return
null
;
}
try
{
String
regEx
=
"[*/\\\\:?\"<|>\\s+%#&=.()]"
;
Pattern
p
=
Pattern
.
compile
(
regEx
);
Matcher
m
=
p
.
matcher
(
fileName
);
String
result
=
m
.
replaceAll
(
""
).
trim
();
// 文件名称过长的话,限制40个字
return
result
.
length
()
>
40
?
result
.
substring
(
0
,
40
)
:
result
;
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】格式化文件名称.[formatName]:"
+
e
.
getMessage
(),
e
);
return
UUIDUitl
.
taskName
();
}
}
/**
* 获取文件所在的目录
*
* @param filePath
* @return
*/
public
static
String
getFileFolder
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
return
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
));
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做
*
* @param filePath
*/
public
static
boolean
creatDir
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
}
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
file
.
mkdir
();
return
true
;
}
return
false
;
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做 该方法能够实现创建整个路径
*
* @param filePath
*/
public
static
boolean
creatDirs
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
}
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
file
.
mkdirs
();
return
true
;
}
return
false
;
}
/**
* 获取文件类型
*
* @param filePath
*/
public
static
String
getFileType
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
int
index1
=
filePath
.
lastIndexOf
(
"."
);
if
(
index1
<
1
)
{
return
null
;
}
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
if
(
index2
>
index1
)
{
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
}
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
if
(
index3
>
index1
)
{
return
null
;
}
return
filePath
.
substring
(
filePath
.
lastIndexOf
(
"."
)
+
1
,
filePath
.
length
()).
toLowerCase
();
}
/**
* 获取文件名称
*
* @param filePath
*/
public
static
String
getFileName
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
String
fileName
=
null
;
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
int
index1
=
filePath
.
lastIndexOf
(
"."
);
if
(
index1
>
-
1
)
{
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
if
(
index2
>
index1
)
{
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
}
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
if
(
index3
>
index1
)
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
}
else
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
,
filePath
.
lastIndexOf
(
"."
));
}
}
else
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
}
return
formatName
(
fileName
);
}
/**
* 获取文件名称包括后缀
*
* @param filePath
* @return
*/
public
static
String
getFileNameAll
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
String
fileName
=
getFileName
(
filePath
);
if
(
StringUtil
.
isEmpty
(
fileName
))
{
return
null
;
}
String
fileType
=
getFileType
(
filePath
);
return
StringUtil
.
isEmpty
(
fileType
)
?
fileName
:
fileName
+
"."
+
fileType
;
}
/**
* 格式化文件名称,过滤特殊字符(名称太长进行截断)
*
* @param fileName
*/
public
static
String
formatName
(
String
fileName
)
{
if
(
StringUtil
.
isEmpty
(
fileName
))
{
return
null
;
}
try
{
String
regEx
=
"[*/\\\\:?\"<|>\\s+%#&=.()]"
;
Pattern
p
=
Pattern
.
compile
(
regEx
);
Matcher
m
=
p
.
matcher
(
fileName
);
String
result
=
m
.
replaceAll
(
""
).
trim
();
// 文件名称过长的话,限制40个字
return
result
.
length
()
>
40
?
result
.
substring
(
0
,
40
)
:
result
;
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】格式化文件名称.[formatName]:"
+
e
.
getMessage
(),
e
);
return
UUIDUitl
.
taskName
();
}
}
/**
* 获取文件所在的目录
*
* @param filePath
* @return
*/
public
static
String
getFileFolder
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
}
return
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
));
}
/**
* 获取http文件路径的域名
*
*
* @param fileUrl
* @return
*/
...
...
@@ -450,415 +450,425 @@ public class FileUtils {
return
null
;
}
/**
* FileUtils工具 File @throws
*/
public
static
File
copyURLToFile
(
String
url
,
String
dir
)
throws
Exception
{
try
{
URL
httpurl
=
new
URL
(
url
);
File
f
=
new
File
(
dir
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
return
f
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public
static
boolean
deleteFolder
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
}
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
// 判断目录或文件是否存在
if
(!
file
.
exists
())
{
// 不存在返回 false
return
flag
;
}
else
{
// 判断是否为文件
if
(
file
.
isFile
())
{
// 为文件时调用删除文件方法
return
deleteFile
(
sPath
);
}
else
{
// 为目录时调用删除目录方法
return
deleteDirectory
(
sPath
);
}
}
}
/**
* 删除单个文件
*
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public
static
boolean
deleteFile
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
}
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
// 路径为文件且不为空则进行删除
if
(
file
.
isFile
()
&&
file
.
exists
())
{
file
.
delete
();
flag
=
true
;
}
return
flag
;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public
static
boolean
deleteDirectory
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
}
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if
(!
sPath
.
endsWith
(
File
.
separator
))
{
sPath
=
sPath
+
File
.
separator
;
}
File
dirFile
=
new
File
(
sPath
);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if
(!
dirFile
.
exists
()
||
!
dirFile
.
isDirectory
())
{
return
false
;
}
boolean
flag
=
true
;
// 删除文件夹下的所有文件(包括子目录)
File
[]
files
=
dirFile
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
// 删除子文件
if
(
files
[
i
].
isFile
())
{
flag
=
deleteFile
(
files
[
i
].
getAbsolutePath
());
if
(!
flag
)
break
;
}
// 删除子目录
else
{
flag
=
deleteDirectory
(
files
[
i
].
getAbsolutePath
());
if
(!
flag
)
break
;
}
}
if
(!
flag
)
return
false
;
// 删除当前目录
if
(
dirFile
.
delete
())
{
return
true
;
}
else
{
return
false
;
}
}
/**
* 检查文件是否存在
*
* @param filePath
* @return
*/
public
static
boolean
checkFile
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
}
File
file
=
new
File
(
filePath
);
if
(!
file
.
isFile
())
{
return
false
;
}
return
true
;
}
/**
* | FileUtils工具
*/
public
static
void
downloadFileFromUrl
(
String
url
,
String
localFilePath
)
throws
FileException
{
LOGGER
.
info
(
"【文件API】下载文件[本地文件].<START>.[url]="
+
url
+
",[localFilePath]="
+
localFilePath
);
creatFiles
(
localFilePath
);
try
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
OssUtils
.
downloadFile
(
url
,
localFilePath
);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value), localFilePath);
}
else
{
File
f
=
new
File
(
localFilePath
);
URL
httpurl
=
new
URL
(
url
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[copyURLToFile]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
}
/**
* IOUtils工具
*/
public
static
byte
[]
downloadByteFromUrl
(
String
url
)
throws
FileException
{
LOGGER
.
info
(
"【文件API】下载文件[byte].<START>.[url]="
+
url
);
if
(
StringUtil
.
isEmpty
(
url
))
{
return
null
;
}
byte
[]
data
=
null
;
try
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
data
=
OssUtils
.
downloadFile2Byte
(
url
);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// data =
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value));
}
else
{
URL
httpurl
=
new
URL
(
url
);
data
=
IOUtils
.
toByteArray
(
httpurl
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[toByteArray]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
return
data
;
}
/**
* InputStream
*/
@Deprecated
public
static
InputStream
downloadStreamFromUrl
(
String
url
)
throws
FileException
{
try
{
URL
httpurl
=
new
URL
(
url
);
return
httpurl
.
openStream
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[openStream]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
}
/**
* 普通情况下保存字节数据到本地文件
*/
public
synchronized
static
boolean
saveBytesTofile
(
File
file
,
byte
[]
data
)
throws
BizException
{
FileOutputStream
fos
=
null
;
// 建立输出字节流
try
{
fos
=
new
FileOutputStream
(
file
);
fos
.
write
(
data
);
// 用FileOutputStream 的write方法写入字节数组
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
finally
{
try
{
fos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
/**
* 复制一个文件到另一个目录下
*
* @throws BizException
*/
public
static
void
copyFileToFolder
(
File
inputFile
,
File
outputFile
)
throws
BizException
{
FileInputStream
fis
=
null
;
FileOutputStream
fos
=
null
;
try
{
fis
=
new
FileInputStream
(
inputFile
);
fos
=
new
FileOutputStream
(
outputFile
);
int
len
=
0
;
byte
[]
buf
=
new
byte
[
1024
];
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
fos
.
write
(
buf
,
0
,
len
);
}
fis
.
close
();
fos
.
close
();
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
}
/**
* 合并两个文件
*
* @param outFile 目标文件
* @param leafFile 源文件
*/
public
static
void
mergeFiles
(
File
outFile
,
File
leafFile
)
{
FileOutputStream
fos
=
null
;
FileInputStream
fis
=
null
;
if
(!
outFile
.
exists
())
{
try
{
outFile
.
createNewFile
();
}
catch
(
Exception
e
)
{
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
}
}
try
{
// 合并其实就是文件的续写,写成true
fos
=
new
FileOutputStream
(
outFile
,
true
);
fis
=
new
FileInputStream
(
leafFile
);
int
len
=
0
;
for
(
byte
[]
buf
=
new
byte
[
1024
*
1024
];
(
len
=
fis
.
read
(
buf
))
!=
-
1
;
)
{
fos
.
write
(
buf
,
0
,
len
);
}
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
finally
{
try
{
if
(
fis
!=
null
)
fis
.
close
();
if
(
fos
!=
null
)
fos
.
close
();
}
catch
(
Exception
e
)
{
}
}
}
/**
* 合并多个文件
*
* @param outFile 输出文件,
* @param leafFiles 文件碎片集
*/
public
static
void
mergeFileList
(
File
outFile
,
List
<
File
>
leafFiles
)
{
FileOutputStream
fos
=
null
;
FileInputStream
fis
=
null
;
if
(!
outFile
.
exists
())
{
try
{
outFile
.
createNewFile
();
}
catch
(
Exception
e
)
{
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
}
}
try
{
// 合并其实就是文件的续写,写成true
fos
=
new
FileOutputStream
(
outFile
,
true
);
byte
[]
buf
=
new
byte
[
1024
*
1024
];
for
(
File
leafFile
:
leafFiles
)
{
fis
=
new
FileInputStream
(
leafFile
);
int
len
=
0
;
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
fos
.
write
(
buf
,
0
,
len
);
}
}
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
finally
{
try
{
if
(
fis
!=
null
)
fis
.
close
();
if
(
fos
!=
null
)
fos
.
close
();
}
catch
(
Exception
e
)
{
}
}
}
/**
* 音频文件转换
*
* @param data 音频byte数组
* @param sourExt 原始后缀
* @param ext 新后缀
* @return
* @author PENG
*/
public
static
String
audioFormatConvert
(
byte
[]
data
,
String
sourExt
,
String
ext
)
{
File
tmpdir
=
new
File
(
"tempdir/local"
);
if
(!
tmpdir
.
exists
())
{
try
{
tmpdir
.
mkdirs
();
}
catch
(
SecurityException
ex
)
{
System
.
out
.
println
(
"无法创建临时文件夹"
);
ex
.
printStackTrace
();
}
}
String
name
=
UUID
.
randomUUID
().
toString
();
File
source
=
new
File
(
tmpdir
,
name
+
"."
+
sourExt
);
boolean
creat
=
saveBytesTofile
(
source
,
data
);
if
(
creat
)
{
File
target
=
new
File
(
tmpdir
,
name
+
"."
+
ext
);
AudioAttributes
audio
=
new
AudioAttributes
();
Encoder
encoder
=
new
Encoder
();
audio
.
setCodec
(
"libmp3lame"
);
EncodingAttributes
attrs
=
new
EncodingAttributes
();
attrs
.
setFormat
(
ext
.
toLowerCase
());
attrs
.
setAudioAttributes
(
audio
);
try
{
encoder
.
encode
(
source
,
target
,
attrs
);
return
target
.
getPath
();
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
return
null
;
}
catch
(
InputFormatException
e
)
{
e
.
printStackTrace
();
return
null
;
}
catch
(
EncoderException
e
)
{
e
.
printStackTrace
();
return
target
.
getPath
();
}
}
else
{
return
null
;
}
}
/**
* 获取文本的内容
*
* @param url
* @return
*/
public
static
String
getTextString
(
String
url
)
{
LOGGER
.
info
(
"【文件API】获取文本的内容.<START>.[url]="
+
url
);
if
(
StringUtil
.
isEmpty
(
url
))
{
return
null
;
}
StringBuilder
lrcString
=
new
StringBuilder
();
InputStream
is
=
null
;
InputStreamReader
isr
=
null
;
try
{
byte
[]
bt
=
downloadByteFromUrl
(
url
);
is
=
new
ByteArrayInputStream
(
bt
);
boolean
isUTF8Bom
=
CpdetectorEncoding
.
isUTF8Bom
(
is
);
if
(
isUTF8Bom
)
{
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
}
else
{
Object
charSet
=
CpdetectorEncoding
.
getEncoding
(
bt
,
false
);
if
(
charSet
.
toString
().
equals
(
"UTF-8"
))
{
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
}
else
{
isr
=
new
InputStreamReader
(
new
ByteArrayInputStream
(
bt
),
"GBK"
);
}
}
BufferedReader
br
=
new
BufferedReader
(
isr
);
// 构造一个BufferedReader类来读取文件
String
s
;
while
((
s
=
br
.
readLine
())
!=
null
)
{
// 使用readLine方法,一次读一行
lrcString
.
append
(
System
.
lineSeparator
()
+
s
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】读取文件内容.<ERROR>:"
+
e
.
getMessage
(),
e
);
}
finally
{
try
{
if
(
isr
!=
null
)
isr
.
close
();
if
(
is
!=
null
)
is
.
close
();
}
catch
(
Exception
e2
)
{
}
}
LOGGER
.
info
(
"【文件API】获取文本的内容.<END>"
);
return
lrcString
.
toString
();
}
/**
* FileUtils工具 File @throws
*/
public
static
File
copyURLToFile
(
String
url
,
String
dir
)
throws
Exception
{
try
{
URL
httpurl
=
new
URL
(
url
);
File
f
=
new
File
(
dir
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
return
f
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath
* 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public
static
boolean
deleteFolder
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
}
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
// 判断目录或文件是否存在
if
(!
file
.
exists
())
{
// 不存在返回 false
return
flag
;
}
else
{
// 判断是否为文件
if
(
file
.
isFile
())
{
// 为文件时调用删除文件方法
return
deleteFile
(
sPath
);
}
else
{
// 为目录时调用删除目录方法
return
deleteDirectory
(
sPath
);
}
}
}
/**
* 删除单个文件
*
* @param sPath
* 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public
static
boolean
deleteFile
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
}
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
// 路径为文件且不为空则进行删除
if
(
file
.
isFile
()
&&
file
.
exists
())
{
file
.
delete
();
flag
=
true
;
}
return
flag
;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath
* 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public
static
boolean
deleteDirectory
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
}
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if
(!
sPath
.
endsWith
(
File
.
separator
))
{
sPath
=
sPath
+
File
.
separator
;
}
File
dirFile
=
new
File
(
sPath
);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if
(!
dirFile
.
exists
()
||
!
dirFile
.
isDirectory
())
{
return
false
;
}
boolean
flag
=
true
;
// 删除文件夹下的所有文件(包括子目录)
File
[]
files
=
dirFile
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
// 删除子文件
if
(
files
[
i
].
isFile
())
{
flag
=
deleteFile
(
files
[
i
].
getAbsolutePath
());
if
(!
flag
)
break
;
}
// 删除子目录
else
{
flag
=
deleteDirectory
(
files
[
i
].
getAbsolutePath
());
if
(!
flag
)
break
;
}
}
if
(!
flag
)
return
false
;
// 删除当前目录
if
(
dirFile
.
delete
())
{
return
true
;
}
else
{
return
false
;
}
}
/**
* 检查文件是否存在
*
* @param filePath
* @return
*/
public
static
boolean
checkFile
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
}
File
file
=
new
File
(
filePath
);
if
(!
file
.
isFile
())
{
return
false
;
}
return
true
;
}
/**
* | FileUtils工具
*/
public
static
void
downloadFileFromUrl
(
String
url
,
String
localFilePath
)
throws
FileException
{
LOGGER
.
info
(
"【文件API】下载文件[本地文件].<START>.[url]="
+
url
+
",[localFilePath]="
+
localFilePath
);
creatFiles
(
localFilePath
);
try
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
OssUtils
.
downloadFile
(
url
,
localFilePath
);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value), localFilePath);
}
else
{
File
f
=
new
File
(
localFilePath
);
URL
httpurl
=
new
URL
(
url
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[copyURLToFile]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
}
/**
* IOUtils工具
*/
public
static
byte
[]
downloadByteFromUrl
(
String
url
)
throws
FileException
{
LOGGER
.
info
(
"【文件API】下载文件[byte].<START>.[url]="
+
url
);
if
(
StringUtil
.
isEmpty
(
url
))
{
return
null
;
}
byte
[]
data
=
null
;
try
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
data
=
OssUtils
.
downloadFile2Byte
(
url
);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// data =
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value));
}
else
{
URL
httpurl
=
new
URL
(
url
);
data
=
IOUtils
.
toByteArray
(
httpurl
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[toByteArray]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
return
data
;
}
/**
* InputStream
*/
@Deprecated
public
static
InputStream
downloadStreamFromUrl
(
String
url
)
throws
FileException
{
try
{
URL
httpurl
=
new
URL
(
url
);
return
httpurl
.
openStream
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[openStream]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
}
/**
* 普通情况下保存字节数据到本地文件
*/
public
synchronized
static
boolean
saveBytesTofile
(
File
file
,
byte
[]
data
)
throws
BizException
{
FileOutputStream
fos
=
null
;
// 建立输出字节流
try
{
fos
=
new
FileOutputStream
(
file
);
fos
.
write
(
data
);
// 用FileOutputStream 的write方法写入字节数组
return
true
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
false
;
}
finally
{
try
{
fos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
/**
* 复制一个文件到另一个目录下
*
* @throws BizException
*/
public
static
void
copyFileToFolder
(
File
inputFile
,
File
outputFile
)
throws
BizException
{
FileInputStream
fis
=
null
;
FileOutputStream
fos
=
null
;
try
{
fis
=
new
FileInputStream
(
inputFile
);
fos
=
new
FileOutputStream
(
outputFile
);
int
len
=
0
;
byte
[]
buf
=
new
byte
[
1024
];
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
fos
.
write
(
buf
,
0
,
len
);
}
fis
.
close
();
fos
.
close
();
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
}
/**
* 合并两个文件
*
* @param outFile
* 目标文件
* @param leafFile
* 源文件
*/
public
static
void
mergeFiles
(
File
outFile
,
File
leafFile
)
{
FileOutputStream
fos
=
null
;
FileInputStream
fis
=
null
;
if
(!
outFile
.
exists
())
{
try
{
outFile
.
createNewFile
();
}
catch
(
Exception
e
)
{
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
}
}
try
{
// 合并其实就是文件的续写,写成true
fos
=
new
FileOutputStream
(
outFile
,
true
);
fis
=
new
FileInputStream
(
leafFile
);
int
len
=
0
;
for
(
byte
[]
buf
=
new
byte
[
1024
*
1024
];
(
len
=
fis
.
read
(
buf
))
!=
-
1
;)
{
fos
.
write
(
buf
,
0
,
len
);
}
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
finally
{
try
{
if
(
fis
!=
null
)
fis
.
close
();
if
(
fos
!=
null
)
fos
.
close
();
}
catch
(
Exception
e
)
{
}
}
}
/**
* 合并多个文件
*
* @param outFile
* 输出文件,
* @param leafFiles
* 文件碎片集
*/
public
static
void
mergeFileList
(
File
outFile
,
List
<
File
>
leafFiles
)
{
FileOutputStream
fos
=
null
;
FileInputStream
fis
=
null
;
if
(!
outFile
.
exists
())
{
try
{
outFile
.
createNewFile
();
}
catch
(
Exception
e
)
{
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
}
}
try
{
// 合并其实就是文件的续写,写成true
fos
=
new
FileOutputStream
(
outFile
,
true
);
byte
[]
buf
=
new
byte
[
1024
*
1024
];
for
(
File
leafFile
:
leafFiles
)
{
fis
=
new
FileInputStream
(
leafFile
);
int
len
=
0
;
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
fos
.
write
(
buf
,
0
,
len
);
}
}
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
}
finally
{
try
{
if
(
fis
!=
null
)
fis
.
close
();
if
(
fos
!=
null
)
fos
.
close
();
}
catch
(
Exception
e
)
{
}
}
}
/**
* 音频文件转换
*
* @param data
* 音频byte数组
* @param sourExt
* 原始后缀
* @param ext
* 新后缀
* @return
* @author PENG
*/
public
static
String
audioFormatConvert
(
byte
[]
data
,
String
sourExt
,
String
ext
)
{
File
tmpdir
=
new
File
(
"tempdir/local"
);
if
(!
tmpdir
.
exists
())
{
try
{
tmpdir
.
mkdirs
();
}
catch
(
SecurityException
ex
)
{
System
.
out
.
println
(
"无法创建临时文件夹"
);
ex
.
printStackTrace
();
}
}
String
name
=
UUID
.
randomUUID
().
toString
();
File
source
=
new
File
(
tmpdir
,
name
+
"."
+
sourExt
);
boolean
creat
=
saveBytesTofile
(
source
,
data
);
if
(
creat
)
{
File
target
=
new
File
(
tmpdir
,
name
+
"."
+
ext
);
AudioAttributes
audio
=
new
AudioAttributes
();
Encoder
encoder
=
new
Encoder
();
audio
.
setCodec
(
"libmp3lame"
);
EncodingAttributes
attrs
=
new
EncodingAttributes
();
attrs
.
setFormat
(
ext
.
toLowerCase
());
attrs
.
setAudioAttributes
(
audio
);
try
{
encoder
.
encode
(
source
,
target
,
attrs
);
return
target
.
getPath
();
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
return
null
;
}
catch
(
InputFormatException
e
)
{
e
.
printStackTrace
();
return
null
;
}
catch
(
EncoderException
e
)
{
e
.
printStackTrace
();
return
target
.
getPath
();
}
}
else
{
return
null
;
}
}
/**
* 获取文本的内容
*
* @param url
* @return
*/
public
static
String
getTextString
(
String
url
)
{
LOGGER
.
info
(
"【文件API】获取文本的内容.<START>.[url]="
+
url
);
if
(
StringUtil
.
isEmpty
(
url
))
{
return
null
;
}
StringBuilder
lrcString
=
new
StringBuilder
();
InputStream
is
=
null
;
InputStreamReader
isr
=
null
;
try
{
byte
[]
bt
=
downloadByteFromUrl
(
url
);
is
=
new
ByteArrayInputStream
(
bt
);
boolean
isUTF8Bom
=
CpdetectorEncoding
.
isUTF8Bom
(
is
);
if
(
isUTF8Bom
)
{
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
}
else
{
Object
charSet
=
CpdetectorEncoding
.
getEncoding
(
bt
,
false
);
if
(
charSet
.
toString
().
equals
(
"UTF-8"
))
{
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
}
else
{
isr
=
new
InputStreamReader
(
new
ByteArrayInputStream
(
bt
),
"GBK"
);
}
}
BufferedReader
br
=
new
BufferedReader
(
isr
);
// 构造一个BufferedReader类来读取文件
String
s
;
while
((
s
=
br
.
readLine
())
!=
null
)
{
// 使用readLine方法,一次读一行
lrcString
.
append
(
System
.
lineSeparator
()
+
s
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】读取文件内容.<ERROR>:"
+
e
.
getMessage
(),
e
);
}
finally
{
try
{
if
(
isr
!=
null
)
isr
.
close
();
if
(
is
!=
null
)
is
.
close
();
}
catch
(
Exception
e2
)
{
}
}
LOGGER
.
info
(
"【文件API】获取文本的内容.<END>"
);
return
lrcString
.
toString
();
}
/**
* java 8 读取文本内容
...
...
@@ -1004,113 +1014,114 @@ public class FileUtils {
return
outFilePath
;
}
/**
* 将文件转成base64 字符串
*
* @param data
* @return *
* @throws Exception
*/
public
static
String
encodeBase64File
(
byte
[]
data
)
throws
BizException
{
try
{
return
new
String
(
Base64
.
encodeBase64
(
data
));
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
return
null
;
}
}
/**
* 将base64字符解码保存文件
*
* @param base64Code
* @param targetPath
* @throws Exception
*/
public
static
boolean
decoderBase64File
(
String
base64Code
,
String
targetPath
)
throws
BizException
{
if
(
base64Code
==
null
)
return
false
;
OutputStream
out
=
null
;
try
{
// Base64解码
byte
[]
b
=
Base64
.
decodeBase64
(
targetPath
);
for
(
int
i
=
0
;
i
<
b
.
length
;
++
i
)
{
if
(
b
[
i
]
<
0
)
{
// 调整异常数据
b
[
i
]
+=
256
;
}
}
// 生成jpeg图片
out
=
new
FileOutputStream
(
targetPath
);
out
.
write
(
b
);
out
.
flush
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
return
false
;
}
finally
{
try
{
if
(
out
!=
null
)
out
.
close
();
}
catch
(
Exception
e
)
{
}
}
return
true
;
}
/**
* 本地文件到byte数组
*
* @param path
* @return
*/
public
static
byte
[]
file2byte
(
String
path
)
{
byte
[]
data
=
null
;
FileInputStream
fis
=
null
;
ByteArrayOutputStream
os
=
null
;
try
{
fis
=
new
FileInputStream
(
new
File
(
path
));
os
=
new
ByteArrayOutputStream
();
byte
[]
buf
=
new
byte
[
1024
];
int
numBytesRead
=
0
;
while
((
numBytesRead
=
fis
.
read
(
buf
))
!=
-
1
)
{
os
.
write
(
buf
,
0
,
numBytesRead
);
}
data
=
os
.
toByteArray
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】文件到byte数组:"
+
e
.
getMessage
(),
e
);
}
finally
{
try
{
if
(
fis
!=
null
)
fis
.
close
();
if
(
os
!=
null
)
os
.
close
();
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"【文件工具】文件流关闭失败:"
+
e
.
getMessage
(),
e
);
}
}
return
data
;
}
/**
* 从输入流中获取数据
*
* @param inStream 输入流
* @return
* @throws Exception
*/
public
static
byte
[]
readInputStream
(
InputStream
inStream
)
throws
Exception
{
ByteArrayOutputStream
outStream
=
new
ByteArrayOutputStream
();
byte
[]
buffer
=
new
byte
[
1024
];
int
len
=
0
;
try
{
while
((
len
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
outStream
.
write
(
buffer
,
0
,
len
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】文件流到byte数组:"
+
e
.
getMessage
(),
e
);
}
finally
{
inStream
.
close
();
}
return
outStream
.
toByteArray
();
}
/**
* 将文件转成base64 字符串
*
* @param data
* @return *
* @throws Exception
*/
public
static
String
encodeBase64File
(
byte
[]
data
)
throws
BizException
{
try
{
return
new
String
(
Base64
.
encodeBase64
(
data
));
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
return
null
;
}
}
/**
* 将base64字符解码保存文件
*
* @param base64Code
* @param targetPath
* @throws Exception
*/
public
static
boolean
decoderBase64File
(
String
base64Code
,
String
targetPath
)
throws
BizException
{
if
(
base64Code
==
null
)
return
false
;
OutputStream
out
=
null
;
try
{
// Base64解码
byte
[]
b
=
Base64
.
decodeBase64
(
targetPath
);
for
(
int
i
=
0
;
i
<
b
.
length
;
++
i
)
{
if
(
b
[
i
]
<
0
)
{
// 调整异常数据
b
[
i
]
+=
256
;
}
}
// 生成jpeg图片
out
=
new
FileOutputStream
(
targetPath
);
out
.
write
(
b
);
out
.
flush
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
return
false
;
}
finally
{
try
{
if
(
out
!=
null
)
out
.
close
();
}
catch
(
Exception
e
)
{
}
}
return
true
;
}
/**
* 本地文件到byte数组
*
* @param path
* @return
*/
public
static
byte
[]
file2byte
(
String
path
)
{
byte
[]
data
=
null
;
FileInputStream
fis
=
null
;
ByteArrayOutputStream
os
=
null
;
try
{
fis
=
new
FileInputStream
(
new
File
(
path
));
os
=
new
ByteArrayOutputStream
();
byte
[]
buf
=
new
byte
[
1024
];
int
numBytesRead
=
0
;
while
((
numBytesRead
=
fis
.
read
(
buf
))
!=
-
1
)
{
os
.
write
(
buf
,
0
,
numBytesRead
);
}
data
=
os
.
toByteArray
();
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】文件到byte数组:"
+
e
.
getMessage
(),
e
);
}
finally
{
try
{
if
(
fis
!=
null
)
fis
.
close
();
if
(
os
!=
null
)
os
.
close
();
}
catch
(
IOException
e
)
{
LOGGER
.
error
(
"【文件工具】文件流关闭失败:"
+
e
.
getMessage
(),
e
);
}
}
return
data
;
}
/**
* 从输入流中获取数据
*
* @param inStream
* 输入流
* @return
* @throws Exception
*/
public
static
byte
[]
readInputStream
(
InputStream
inStream
)
throws
Exception
{
ByteArrayOutputStream
outStream
=
new
ByteArrayOutputStream
();
byte
[]
buffer
=
new
byte
[
1024
];
int
len
=
0
;
try
{
while
((
len
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
outStream
.
write
(
buffer
,
0
,
len
);
}
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】文件流到byte数组:"
+
e
.
getMessage
(),
e
);
}
finally
{
inStream
.
close
();
}
return
outStream
.
toByteArray
();
}
}
pcloud-common/src/main/java/com/pcloud/common/utils/ImageUtils.java
View file @
9c92af97
...
...
@@ -11,10 +11,11 @@ import java.math.BigDecimal;
import
javax.imageio.ImageIO
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.dcg.util.StringUtils
;
import
com.itextpdf.text.Image
;
import
com.pcloud.common.constant.AliyunConstant
;
import
com.pcloud.common.constant.FilePathConst
;
...
...
@@ -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
...
...
@@ -653,29 +691,30 @@ public class ImageUtils {
LOGGER
.
error
(
"【图片】输出文件流关闭失败:"
+
e
.
getMessage
(),
e
);
}
}
/**
* 获取图片的宽度和高度
* 获取图片的宽度和高度
(未考虑图片旋转的情况。也就是说,获得的宽可能是宽也可能是高,高可能是高也肯是宽)
*
* @param img
* 图片文件
* @param fileUrl
* @return
*/
public
static
float
[]
getWidthHeight
Size
(
String
fileUrl
)
{
public
static
float
[]
getWidthHeight
NoOrient
(
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"
))
{
/*
* if (StringTools.contains(fileUrl, AliyunConstant.OSS_CDN_URLS)) { localFile =
* OssUtils.imageAutoOrient(fileUrl); 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】获取图片的宽度和高度:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【IMAGE API】获取图片的宽度和高度
[getWidthHeightNoOrient]
:"
+
e
.
getMessage
(),
e
);
}
finally
{
if
(!
StringUtil
.
isEmpty
(
localFile
))
{
FileUtils
.
deleteFile
(
localFile
);
...
...
@@ -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
*
* @param localFilePath
...
...
pcloud-common/src/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
View file @
9c92af97
This source diff could not be displayed because it is too large. You can
view the blob
instead.
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