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
44b1f4bd
Commit
44b1f4bd
authored
Dec 03, 2018
by
songxiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
图片转webp格式
parent
fd523fce
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
1117 additions
and
1133 deletions
+1117
-1133
FileUtils.java
...mmon/src/main/java/com/pcloud/common/utils/FileUtils.java
+1100
-1095
OssUtils.java
...rc/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
+17
-38
No files found.
pcloud-common/src/main/java/com/pcloud/common/utils/FileUtils.java
View file @
44b1f4bd
package
com
.
pcloud
.
common
.
utils
;
package
com
.
pcloud
.
common
.
utils
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.net.URL
;
import
java.net.URL
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.Files
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.nio.file.Paths
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.UUID
;
import
java.util.regex.Matcher
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
java.util.regex.Pattern
;
import
java.util.stream.Stream
;
import
java.util.stream.Stream
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
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.OSConstant
;
import
com.pcloud.common.constant.OSConstant
;
import
com.pcloud.common.entity.UploadResultInfo
;
import
com.pcloud.common.entity.UploadResultInfo
;
import
com.pcloud.common.exceptions.BizException
;
import
com.pcloud.common.exceptions.BizException
;
import
com.pcloud.common.exceptions.FileException
;
import
com.pcloud.common.exceptions.FileException
;
import
com.pcloud.common.utils.aliyun.OssUtils
;
import
com.pcloud.common.utils.aliyun.OssUtils
;
import
com.pcloud.common.utils.encode.CpdetectorEncoding
;
import
com.pcloud.common.utils.encode.CpdetectorEncoding
;
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
it.sauronsoftware.jave.AudioAttributes
;
import
it.sauronsoftware.jave.AudioAttributes
;
import
it.sauronsoftware.jave.Encoder
;
import
it.sauronsoftware.jave.Encoder
;
import
it.sauronsoftware.jave.EncoderException
;
import
it.sauronsoftware.jave.EncoderException
;
import
it.sauronsoftware.jave.EncodingAttributes
;
import
it.sauronsoftware.jave.EncodingAttributes
;
import
it.sauronsoftware.jave.InputFormatException
;
import
it.sauronsoftware.jave.InputFormatException
;
/**
/**
* @描述:文件工具类. @作者:DiSeng.H
* @描述:文件工具类. @作者:DiSeng.H
* @创建时间:2016年3月10日,上午11:56:26 @版本:1.0
* @创建时间:2016年3月10日,上午11:56:26 @版本:1.0
*/
*/
public
class
FileUtils
{
public
class
FileUtils
{
/**
/**
* logger
* logger
*/
*/
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
FileUtils
.
class
);
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
FileUtils
.
class
);
/**
/**
* PDF
* PDF
*/
*/
public
final
static
String
PDF
=
"PDF"
;
public
final
static
String
PDF
=
"PDF"
;
/**
/**
* XML
* XML
*/
*/
public
final
static
String
XML
=
"XML"
;
public
final
static
String
XML
=
"XML"
;
/**
/**
* IMAGE
* IMAGE
*/
*/
public
final
static
String
IMAGE
=
"IMAGE"
;
public
final
static
String
IMAGE
=
"IMAGE"
;
/**
/**
* 音频
* 音频
*/
*/
public
final
static
String
AUDIO
=
"AUDIO"
;
public
final
static
String
AUDIO
=
"AUDIO"
;
/**
/**
* 视频格式
* 视频格式
*/
*/
public
final
static
String
VIDEO
=
"VIDEO"
;
public
final
static
String
VIDEO
=
"VIDEO"
;
/**
/**
* 压缩格式
* 压缩格式
*/
*/
public
final
static
String
COMPRESS
=
"COMPRESS"
;
public
final
static
String
COMPRESS
=
"COMPRESS"
;
/**
/**
* 办公软件
* 办公软件
*/
*/
public
final
static
String
EDITOR
=
"EDITOR"
;
public
final
static
String
EDITOR
=
"EDITOR"
;
/**
/**
* 文本格式
* 文本格式
*/
*/
public
final
static
String
TEXT
=
"TEXT"
;
public
final
static
String
TEXT
=
"TEXT"
;
/**
/**
* PDF文件格式
* PDF文件格式
*/
*/
public
static
final
String
[]
PDF_GATHER
=
{
"PDF"
};
public
static
final
String
[]
PDF_GATHER
=
{
"PDF"
};
/**
/**
* XML文件格式
* XML文件格式
*/
*/
public
static
final
String
[]
XML_GATHER
=
{
"XML"
};
public
static
final
String
[]
XML_GATHER
=
{
"XML"
};
/**
/**
* 图片文件格式
* 图片文件格式
*/
*/
public
static
final
String
[]
IMAGE_GATHER
=
{
"BMP"
,
"JPG"
,
"GIF"
,
"PNG"
,
"PCX"
,
"TIF"
,
"JPEG"
,
"SVG"
};
public
static
final
String
[]
IMAGE_GATHER
=
{
"BMP"
,
"JPG"
,
"GIF"
,
"PNG"
,
"PCX"
,
"TIF"
,
"JPEG"
,
"SVG"
,
"WEBP"
};
/**
/**
* 音頻文件格式 【"LRC" 为歌词字幕(这里作为音頻文件上传)】
* 音頻文件格式 【"LRC" 为歌词字幕(这里作为音頻文件上传)】
*/
*/
public
static
final
String
[]
AUDIO_GATHER
=
{
"AMR"
,
"MP3"
,
"WMA"
,
"WAV"
,
"AAC"
,
"APE"
,
"M4A"
};
public
static
final
String
[]
AUDIO_GATHER
=
{
"AMR"
,
"MP3"
,
"WMA"
,
"WAV"
,
"AAC"
,
"APE"
,
"M4A"
};
/**
/**
* 視頻文件格式 【"SRT", "SSA", "SMI", "SUB" 为视频“外挂型”字幕(这里作为视频文件上传)】
* 視頻文件格式 【"SRT", "SSA", "SMI", "SUB" 为视频“外挂型”字幕(这里作为视频文件上传)】
*/
*/
public
static
final
String
[]
VIDEO_GATHER
=
{
"MPG"
,
"MPEG"
,
"AVI"
,
"MOV"
,
"ASF"
,
"WMV"
,
"NAVI"
,
"3GP"
,
"MKV"
,
public
static
final
String
[]
VIDEO_GATHER
=
{
"MPG"
,
"MPEG"
,
"AVI"
,
"MOV"
,
"ASF"
,
"WMV"
,
"NAVI"
,
"3GP"
,
"MKV"
,
"FLV"
,
"F4V"
,
"RMVB"
,
"RM"
,
"MP4"
,
"SRT"
,
"SSA"
,
"SMI"
,
"SUB"
,
"ASX"
,
"M3U8"
};
"FLV"
,
"F4V"
,
"RMVB"
,
"RM"
,
"MP4"
,
"SRT"
,
"SSA"
,
"SMI"
,
"SUB"
,
"ASX"
,
"M3U8"
};
/**
/**
* 压缩包文件格式
* 压缩包文件格式
*/
*/
public
static
final
String
[]
COMPRESS_GATHER
=
{
"RAR"
,
"ZIP"
};
public
static
final
String
[]
COMPRESS_GATHER
=
{
"RAR"
,
"ZIP"
};
/**
/**
* 文件格式
* 文件格式
*/
*/
public
static
final
String
[]
TEXT_GATHER
=
{
"TXT"
,
"LRC"
};
public
static
final
String
[]
TEXT_GATHER
=
{
"TXT"
,
"LRC"
};
/**
/**
* 编辑器格式
* 编辑器格式
*/
*/
public
static
final
String
[]
EDITOR_GATHER
=
{
"DOC"
,
"DOCX"
,
"XLS"
,
"XLSX"
,
"PPT"
,
"PPTX"
};
public
static
final
String
[]
EDITOR_GATHER
=
{
"DOC"
,
"DOCX"
,
"XLS"
,
"XLSX"
,
"PPT"
,
"PPTX"
};
/**
/**
* word格式
* word格式
*/
*/
public
static
final
String
[]
WORD_GATHER
=
{
"DOC"
,
"DOCX"
};
public
static
final
String
[]
WORD_GATHER
=
{
"DOC"
,
"DOCX"
};
/**
/**
* excel格式
* excel格式
*/
*/
public
static
final
String
[]
EXCEL_GATHER
=
{
"XLS"
,
"XLSX"
,
"XLSM"
,
"XLT"
,
"XLTX"
,
"XLTM"
,
"CSV"
};
public
static
final
String
[]
EXCEL_GATHER
=
{
"XLS"
,
"XLSX"
,
"XLSM"
,
"XLT"
,
"XLTX"
,
"XLTM"
,
"CSV"
};
/**
/**
* PPT格式
* PPT格式
*/
*/
public
static
final
String
[]
PPT_GATHER
=
{
"PPT"
,
"PPTX"
};
public
static
final
String
[]
PPT_GATHER
=
{
"PPT"
,
"PPTX"
};
/**
/**
* 根据文件类型获取文件分类
* 根据文件类型获取文件分类
*
*
* @param fileType
* @param fileType
* @return
* @return
*/
*/
public
static
String
getGatherName
(
String
fileType
)
{
public
static
String
getGatherName
(
String
fileType
)
{
if
(
StringUtil
.
isEmpty
(
fileType
))
{
if
(
StringUtil
.
isEmpty
(
fileType
))
{
return
null
;
return
null
;
}
}
// IMAGE
// IMAGE
if
(
Arrays
.
asList
(
IMAGE_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
IMAGE_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
IMAGE
;
return
IMAGE
;
}
}
// AUDIO
// AUDIO
if
(
Arrays
.
asList
(
AUDIO_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
AUDIO_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
AUDIO
;
return
AUDIO
;
}
}
// VIDEO
// VIDEO
if
(
Arrays
.
asList
(
VIDEO_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
VIDEO_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
VIDEO
;
return
VIDEO
;
}
}
// PDF
// PDF
if
(
Arrays
.
asList
(
PDF_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
PDF_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
PDF
;
return
PDF
;
}
}
// XML
// XML
if
(
Arrays
.
asList
(
XML_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
XML_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
XML
;
return
XML
;
}
}
// COMPRESS
// COMPRESS
if
(
Arrays
.
asList
(
COMPRESS_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
COMPRESS_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
COMPRESS
;
return
COMPRESS
;
}
}
// EDITOR
// EDITOR
if
(
Arrays
.
asList
(
EDITOR_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
EDITOR_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
EDITOR
;
return
EDITOR
;
}
}
// COMPRESS
// COMPRESS
if
(
Arrays
.
asList
(
TEXT_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
if
(
Arrays
.
asList
(
TEXT_GATHER
).
contains
(
fileType
.
toUpperCase
()))
{
return
TEXT
;
return
TEXT
;
}
}
return
null
;
return
null
;
}
}
/**
/**
* 传入文件夹路径,该方法能够实现创建整个路径
* 传入文件夹路径,该方法能够实现创建整个路径
*
*
* @param path 文件夹路径,不包含文件名称及后缀名
* @param path 文件夹路径,不包含文件名称及后缀名
*/
*/
public
static
boolean
isDir
(
String
filePath
)
{
public
static
boolean
isDir
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
return
false
;
}
}
String
[]
paths
=
filePath
.
split
(
"/"
);
String
[]
paths
=
filePath
.
split
(
"/"
);
String
temp
=
""
;
String
temp
=
""
;
for
(
int
i
=
0
;
i
<
paths
.
length
;
i
++)
{
for
(
int
i
=
0
;
i
<
paths
.
length
;
i
++)
{
if
(
i
==
0
)
{
if
(
i
==
0
)
{
temp
=
paths
[
0
];
temp
=
paths
[
0
];
}
else
{
}
else
{
temp
+=
"/"
+
paths
[
i
];
temp
+=
"/"
+
paths
[
i
];
}
}
creatDir
(
temp
);
creatDir
(
temp
);
}
}
return
true
;
return
true
;
}
}
/**
/**
* 该方法用来判断文件是否存在,如果不存在则创建
* 该方法用来判断文件是否存在,如果不存在则创建
*
*
* @param filePath
* @param filePath
*/
*/
public
static
boolean
creatFile
(
String
filePath
)
{
public
static
boolean
creatFile
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
return
false
;
}
}
File
file
=
new
File
(
filePath
);
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
if
(!
file
.
exists
())
{
try
{
try
{
file
.
createNewFile
();
file
.
createNewFile
();
return
true
;
return
true
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】创建文件失败,<ERROR>:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件工具】创建文件失败,<ERROR>:"
+
e
.
getMessage
(),
e
);
}
}
}
}
return
false
;
return
false
;
}
}
/**
/**
* 该方法用来判断文件是否存在,如果不存在则创建,该方法能够实现创建整个路径
* 该方法用来判断文件是否存在,如果不存在则创建,该方法能够实现创建整个路径
*
*
* @param filePath
* @param filePath
*/
*/
public
static
boolean
creatFiles
(
String
filePath
)
{
public
static
boolean
creatFiles
(
String
filePath
)
{
LOGGER
.
info
(
"【文件API】创建文件,同时创建整个路径.<START>.[filePath]="
+
filePath
);
LOGGER
.
info
(
"【文件API】创建文件,同时创建整个路径.<START>.[filePath]="
+
filePath
);
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
return
false
;
}
}
filePath
=
filePath
.
replace
(
File
.
separator
,
"/"
);
filePath
=
filePath
.
replace
(
File
.
separator
,
"/"
);
String
dirPath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"/"
));
String
dirPath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"/"
));
File
dirFile
=
new
File
(
dirPath
);
File
dirFile
=
new
File
(
dirPath
);
if
(!
dirFile
.
exists
())
{
if
(!
dirFile
.
exists
())
{
try
{
try
{
dirFile
.
mkdirs
();
dirFile
.
mkdirs
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】创建文件夹失败,<ERROR>:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件工具】创建文件夹失败,<ERROR>:"
+
e
.
getMessage
(),
e
);
return
false
;
return
false
;
}
}
}
}
File
file
=
new
File
(
filePath
);
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
if
(!
file
.
exists
())
{
try
{
try
{
file
.
createNewFile
();
file
.
createNewFile
();
return
true
;
return
true
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】创建文件失败,<ERROR>:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件工具】创建文件失败,<ERROR>:"
+
e
.
getMessage
(),
e
);
}
}
}
}
LOGGER
.
info
(
"【文件API】创建文件,同时创建整个路径.<END>"
);
LOGGER
.
info
(
"【文件API】创建文件,同时创建整个路径.<END>"
);
return
false
;
return
false
;
}
}
/**
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做
*
*
* @param filePath
* @param filePath
*/
*/
public
static
boolean
creatDir
(
String
filePath
)
{
public
static
boolean
creatDir
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
return
false
;
}
}
File
file
=
new
File
(
filePath
);
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
if
(!
file
.
exists
())
{
file
.
mkdir
();
file
.
mkdir
();
return
true
;
return
true
;
}
}
return
false
;
return
false
;
}
}
/**
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做 该方法能够实现创建整个路径
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做 该方法能够实现创建整个路径
*
*
* @param filePath
* @param filePath
*/
*/
public
static
boolean
creatDirs
(
String
filePath
)
{
public
static
boolean
creatDirs
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
return
false
;
}
}
File
file
=
new
File
(
filePath
);
File
file
=
new
File
(
filePath
);
if
(!
file
.
exists
())
{
if
(!
file
.
exists
())
{
file
.
mkdirs
();
file
.
mkdirs
();
return
true
;
return
true
;
}
}
return
false
;
return
false
;
}
}
/**
/**
* 获取文件类型
* 获取文件类型
*
*
* @param filePath
* @param filePath
*/
*/
public
static
String
getFileType
(
String
filePath
)
{
public
static
String
getFileType
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
return
null
;
}
}
int
index1
=
filePath
.
lastIndexOf
(
"."
);
int
index1
=
filePath
.
lastIndexOf
(
"."
);
if
(
index1
<
1
)
{
if
(
index1
<
1
)
{
return
null
;
return
null
;
}
}
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
if
(
index2
>
index1
)
{
if
(
index2
>
index1
)
{
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
}
}
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
if
(
index3
>
index1
)
{
if
(
index3
>
index1
)
{
return
null
;
return
null
;
}
}
return
filePath
.
substring
(
filePath
.
lastIndexOf
(
"."
)
+
1
,
filePath
.
length
()).
toLowerCase
();
return
filePath
.
substring
(
filePath
.
lastIndexOf
(
"."
)
+
1
,
filePath
.
length
()).
toLowerCase
();
}
}
/**
/**
* 获取文件名称
* 获取文件名称
*
*
* @param filePath
* @param filePath
*/
*/
public
static
String
getFileName
(
String
filePath
)
{
public
static
String
getFileName
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
return
null
;
}
}
String
fileName
=
null
;
String
fileName
=
null
;
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
filePath
=
filePath
.
replace
(
"/"
,
OSConstant
.
SEPARATOR
);
int
index1
=
filePath
.
lastIndexOf
(
"."
);
int
index1
=
filePath
.
lastIndexOf
(
"."
);
if
(
index1
>
-
1
)
{
if
(
index1
>
-
1
)
{
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
int
index2
=
filePath
.
lastIndexOf
(
"?"
);
if
(
index2
>
index1
)
{
if
(
index2
>
index1
)
{
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
filePath
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"?"
));
}
}
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
int
index3
=
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
);
if
(
index3
>
index1
)
{
if
(
index3
>
index1
)
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
}
else
{
}
else
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
,
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
,
filePath
.
lastIndexOf
(
"."
));
filePath
.
lastIndexOf
(
"."
));
}
}
}
else
{
}
else
{
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
fileName
=
filePath
.
substring
(
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
)
+
1
);
}
}
return
formatName
(
fileName
);
return
formatName
(
fileName
);
}
}
/**
/**
* 获取文件名称包括后缀
* 获取文件名称包括后缀
*
*
* @param filePath
* @param filePath
* @return
* @return
*/
*/
public
static
String
getFileNameAll
(
String
filePath
)
{
public
static
String
getFileNameAll
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
return
null
;
}
}
String
fileName
=
getFileName
(
filePath
);
String
fileName
=
getFileName
(
filePath
);
if
(
StringUtil
.
isEmpty
(
fileName
))
{
if
(
StringUtil
.
isEmpty
(
fileName
))
{
return
null
;
return
null
;
}
}
String
fileType
=
getFileType
(
filePath
);
String
fileType
=
getFileType
(
filePath
);
return
StringUtil
.
isEmpty
(
fileType
)
?
fileName
:
fileName
+
"."
+
fileType
;
return
StringUtil
.
isEmpty
(
fileType
)
?
fileName
:
fileName
+
"."
+
fileType
;
}
}
/**
/**
* 格式化文件名称,过滤特殊字符(名称太长进行截断)
* 格式化文件名称,过滤特殊字符(名称太长进行截断)
*
*
* @param fileName
* @param fileName
*/
*/
public
static
String
formatName
(
String
fileName
)
{
public
static
String
formatName
(
String
fileName
)
{
if
(
StringUtil
.
isEmpty
(
fileName
))
{
if
(
StringUtil
.
isEmpty
(
fileName
))
{
return
null
;
return
null
;
}
}
try
{
try
{
String
regEx
=
"[*/\\\\:?\"<|>\\s+%#&=.()]"
;
String
regEx
=
"[*/\\\\:?\"<|>\\s+%#&=.()]"
;
Pattern
p
=
Pattern
.
compile
(
regEx
);
Pattern
p
=
Pattern
.
compile
(
regEx
);
Matcher
m
=
p
.
matcher
(
fileName
);
Matcher
m
=
p
.
matcher
(
fileName
);
String
result
=
m
.
replaceAll
(
""
).
trim
();
String
result
=
m
.
replaceAll
(
""
).
trim
();
// 文件名称过长的话,限制40个字
// 文件名称过长的话,限制40个字
return
result
.
length
()
>
40
?
result
.
substring
(
0
,
40
)
:
result
;
return
result
.
length
()
>
40
?
result
.
substring
(
0
,
40
)
:
result
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】格式化文件名称.[formatName]:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件API】格式化文件名称.[formatName]:"
+
e
.
getMessage
(),
e
);
return
UUIDUitl
.
taskName
();
return
UUIDUitl
.
taskName
();
}
}
}
}
/**
/**
* 获取文件所在的目录
* 获取文件所在的目录
*
*
* @param filePath
* @param filePath
* @return
* @return
*/
*/
public
static
String
getFileFolder
(
String
filePath
)
{
public
static
String
getFileFolder
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
null
;
return
null
;
}
}
return
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
));
return
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
OSConstant
.
SEPARATOR
));
}
}
/**
/**
* 获取http文件路径的域名
* 获取http文件路径的域名
*
*
* @param fileUrl
* @param fileUrl
* @return
* @return
*/
*/
public
static
String
getDomainName
(
String
fileUrl
)
{
public
static
String
getDomainName
(
String
fileUrl
)
{
if
(
StringUtil
.
isEmpty
(
fileUrl
))
{
if
(
StringUtil
.
isEmpty
(
fileUrl
))
{
return
null
;
return
null
;
}
}
Pattern
p
=
Pattern
.
compile
(
"(?<=//|)((\\w)+\\.)+\\w+"
);
Pattern
p
=
Pattern
.
compile
(
"(?<=//|)((\\w)+\\.)+\\w+"
);
Matcher
m
=
p
.
matcher
(
fileUrl
);
Matcher
m
=
p
.
matcher
(
fileUrl
);
if
(
m
.
find
())
{
if
(
m
.
find
())
{
return
m
.
group
();
return
m
.
group
();
}
}
return
null
;
return
null
;
}
}
/**
/**
* FileUtils工具 File @throws
* FileUtils工具 File @throws
*/
*/
public
static
File
copyURLToFile
(
String
url
,
String
dir
)
throws
Exception
{
public
static
File
copyURLToFile
(
String
url
,
String
dir
)
throws
Exception
{
try
{
try
{
URL
httpurl
=
new
URL
(
url
);
URL
httpurl
=
new
URL
(
url
);
File
f
=
new
File
(
dir
);
File
f
=
new
File
(
dir
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
return
f
;
return
f
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
return
null
;
return
null
;
}
}
/**
/**
* 根据路径删除指定的目录或文件,无论存在与否
* 根据路径删除指定的目录或文件,无论存在与否
*
*
* @param sPath 要删除的目录或文件
* @param sPath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
* @return 删除成功返回 true,否则返回 false。
*/
*/
public
static
boolean
deleteFolder
(
String
sPath
)
{
public
static
boolean
deleteFolder
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
return
false
;
}
}
boolean
flag
=
false
;
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
File
file
=
new
File
(
sPath
);
// 判断目录或文件是否存在
// 判断目录或文件是否存在
if
(!
file
.
exists
())
{
// 不存在返回 false
if
(!
file
.
exists
())
{
// 不存在返回 false
return
flag
;
return
flag
;
}
else
{
}
else
{
// 判断是否为文件
// 判断是否为文件
if
(
file
.
isFile
())
{
// 为文件时调用删除文件方法
if
(
file
.
isFile
())
{
// 为文件时调用删除文件方法
return
deleteFile
(
sPath
);
return
deleteFile
(
sPath
);
}
else
{
// 为目录时调用删除目录方法
}
else
{
// 为目录时调用删除目录方法
return
deleteDirectory
(
sPath
);
return
deleteDirectory
(
sPath
);
}
}
}
}
}
}
/**
/**
* 删除单个文件
* 删除单个文件
*
*
* @param sPath 被删除文件的文件名
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
* @return 单个文件删除成功返回true,否则返回false
*/
*/
public
static
boolean
deleteFile
(
String
sPath
)
{
public
static
boolean
deleteFile
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
return
false
;
}
}
boolean
flag
=
false
;
boolean
flag
=
false
;
File
file
=
new
File
(
sPath
);
File
file
=
new
File
(
sPath
);
// 路径为文件且不为空则进行删除
// 路径为文件且不为空则进行删除
if
(
file
.
isFile
()
&&
file
.
exists
())
{
if
(
file
.
isFile
()
&&
file
.
exists
())
{
file
.
delete
();
file
.
delete
();
flag
=
true
;
flag
=
true
;
}
}
return
flag
;
return
flag
;
}
}
/**
/**
* 删除目录(文件夹)以及目录下的文件
* 删除目录(文件夹)以及目录下的文件
*
*
* @param sPath 被删除目录的文件路径
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
* @return 目录删除成功返回true,否则返回false
*/
*/
public
static
boolean
deleteDirectory
(
String
sPath
)
{
public
static
boolean
deleteDirectory
(
String
sPath
)
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
if
(
StringUtil
.
isEmpty
(
sPath
))
{
return
false
;
return
false
;
}
}
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if
(!
sPath
.
endsWith
(
File
.
separator
))
{
if
(!
sPath
.
endsWith
(
File
.
separator
))
{
sPath
=
sPath
+
File
.
separator
;
sPath
=
sPath
+
File
.
separator
;
}
}
File
dirFile
=
new
File
(
sPath
);
File
dirFile
=
new
File
(
sPath
);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if
(!
dirFile
.
exists
()
||
!
dirFile
.
isDirectory
())
{
if
(!
dirFile
.
exists
()
||
!
dirFile
.
isDirectory
())
{
return
false
;
return
false
;
}
}
boolean
flag
=
true
;
boolean
flag
=
true
;
// 删除文件夹下的所有文件(包括子目录)
// 删除文件夹下的所有文件(包括子目录)
File
[]
files
=
dirFile
.
listFiles
();
File
[]
files
=
dirFile
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
// 删除子文件
// 删除子文件
if
(
files
[
i
].
isFile
())
{
if
(
files
[
i
].
isFile
())
{
flag
=
deleteFile
(
files
[
i
].
getAbsolutePath
());
flag
=
deleteFile
(
files
[
i
].
getAbsolutePath
());
if
(!
flag
)
if
(!
flag
)
break
;
break
;
}
// 删除子目录
}
// 删除子目录
else
{
else
{
flag
=
deleteDirectory
(
files
[
i
].
getAbsolutePath
());
flag
=
deleteDirectory
(
files
[
i
].
getAbsolutePath
());
if
(!
flag
)
if
(!
flag
)
break
;
break
;
}
}
}
}
if
(!
flag
)
if
(!
flag
)
return
false
;
return
false
;
// 删除当前目录
// 删除当前目录
if
(
dirFile
.
delete
())
{
if
(
dirFile
.
delete
())
{
return
true
;
return
true
;
}
else
{
}
else
{
return
false
;
return
false
;
}
}
}
}
/**
/**
* 检查文件是否存在
* 检查文件是否存在
*
*
* @param filePath
* @param filePath
* @return
* @return
*/
*/
public
static
boolean
checkFile
(
String
filePath
)
{
public
static
boolean
checkFile
(
String
filePath
)
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
if
(
StringUtil
.
isEmpty
(
filePath
))
{
return
false
;
return
false
;
}
}
File
file
=
new
File
(
filePath
);
File
file
=
new
File
(
filePath
);
if
(!
file
.
isFile
())
{
if
(!
file
.
isFile
())
{
return
false
;
return
false
;
}
}
return
true
;
return
true
;
}
}
/**
/**
* | FileUtils工具
* | FileUtils工具
*/
*/
public
static
void
downloadFileFromUrl
(
String
url
,
String
localFilePath
)
throws
FileException
{
public
static
void
downloadFileFromUrl
(
String
url
,
String
localFilePath
)
throws
FileException
{
LOGGER
.
info
(
"【文件API】下载文件[本地文件].<START>.[url]="
+
url
+
",[localFilePath]="
+
localFilePath
);
LOGGER
.
info
(
"【文件API】下载文件[本地文件].<START>.[url]="
+
url
+
",[localFilePath]="
+
localFilePath
);
creatFiles
(
localFilePath
);
creatFiles
(
localFilePath
);
try
{
try
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
OssUtils
.
downloadFile
(
url
,
localFilePath
);
OssUtils
.
downloadFile
(
url
,
localFilePath
);
// } else if (url.contains("/group")) {
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value), localFilePath);
// paramMap.get(FdfsEnum.FILE_ID.value), localFilePath);
}
else
{
}
else
{
File
f
=
new
File
(
localFilePath
);
File
f
=
new
File
(
localFilePath
);
URL
httpurl
=
new
URL
(
url
);
URL
httpurl
=
new
URL
(
url
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
org
.
apache
.
commons
.
io
.
FileUtils
.
copyURLToFile
(
httpurl
,
f
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[copyURLToFile]:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件API】下载文件.[copyURLToFile]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
}
}
}
/**
/**
* IOUtils工具
* IOUtils工具
*/
*/
public
static
byte
[]
downloadByteFromUrl
(
String
url
)
throws
FileException
{
public
static
byte
[]
downloadByteFromUrl
(
String
url
)
throws
FileException
{
LOGGER
.
info
(
"【文件API】下载文件[byte].<START>.[url]="
+
url
);
LOGGER
.
info
(
"【文件API】下载文件[byte].<START>.[url]="
+
url
);
if
(
StringUtil
.
isEmpty
(
url
))
{
if
(
StringUtil
.
isEmpty
(
url
))
{
return
null
;
return
null
;
}
}
byte
[]
data
=
null
;
byte
[]
data
=
null
;
try
{
try
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
if
(
StringTools
.
contains
(
url
,
AliyunConstant
.
OSS_CDN_URLS
))
{
data
=
OssUtils
.
downloadFile2Byte
(
url
);
data
=
OssUtils
.
downloadFile2Byte
(
url
);
// } else if (url.contains("/group")) {
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// data =
// data =
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value));
// paramMap.get(FdfsEnum.FILE_ID.value));
}
else
{
}
else
{
URL
httpurl
=
new
URL
(
url
);
URL
httpurl
=
new
URL
(
url
);
data
=
IOUtils
.
toByteArray
(
httpurl
);
data
=
IOUtils
.
toByteArray
(
httpurl
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[toByteArray]:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件API】下载文件.[toByteArray]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
}
return
data
;
return
data
;
}
}
/**
/**
* InputStream
* InputStream
*/
*/
@Deprecated
@Deprecated
public
static
InputStream
downloadStreamFromUrl
(
String
url
)
throws
FileException
{
public
static
InputStream
downloadStreamFromUrl
(
String
url
)
throws
FileException
{
try
{
try
{
URL
httpurl
=
new
URL
(
url
);
URL
httpurl
=
new
URL
(
url
);
return
httpurl
.
openStream
();
return
httpurl
.
openStream
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件API】下载文件.[openStream]:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件API】下载文件.[openStream]:"
+
e
.
getMessage
(),
e
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
throw
new
FileException
(
FileException
.
FILE_DOWNLOAD_FAILURE
,
"下载文件失败"
);
}
}
}
}
/**
/**
* 普通情况下保存字节数据到本地文件
* 普通情况下保存字节数据到本地文件
*/
*/
public
synchronized
static
boolean
saveBytesTofile
(
File
file
,
byte
[]
data
)
throws
BizException
{
public
synchronized
static
boolean
saveBytesTofile
(
File
file
,
byte
[]
data
)
throws
BizException
{
FileOutputStream
fos
=
null
;
FileOutputStream
fos
=
null
;
// 建立输出字节流
// 建立输出字节流
try
{
try
{
fos
=
new
FileOutputStream
(
file
);
fos
=
new
FileOutputStream
(
file
);
fos
.
write
(
data
);
// 用FileOutputStream 的write方法写入字节数组
fos
.
write
(
data
);
// 用FileOutputStream 的write方法写入字节数组
return
true
;
return
true
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
return
false
;
return
false
;
}
finally
{
}
finally
{
try
{
try
{
fos
.
close
();
fos
.
close
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
}
}
/**
/**
* 复制一个文件到另一个目录下
* 复制一个文件到另一个目录下
*
*
* @throws BizException
* @throws BizException
*/
*/
public
static
void
copyFileToFolder
(
File
inputFile
,
File
outputFile
)
throws
BizException
{
public
static
void
copyFileToFolder
(
File
inputFile
,
File
outputFile
)
throws
BizException
{
FileInputStream
fis
=
null
;
FileInputStream
fis
=
null
;
FileOutputStream
fos
=
null
;
FileOutputStream
fos
=
null
;
try
{
try
{
fis
=
new
FileInputStream
(
inputFile
);
fis
=
new
FileInputStream
(
inputFile
);
fos
=
new
FileOutputStream
(
outputFile
);
fos
=
new
FileOutputStream
(
outputFile
);
int
len
=
0
;
int
len
=
0
;
byte
[]
buf
=
new
byte
[
1024
];
byte
[]
buf
=
new
byte
[
1024
];
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
fos
.
write
(
buf
,
0
,
len
);
fos
.
write
(
buf
,
0
,
len
);
}
}
fis
.
close
();
fis
.
close
();
fos
.
close
();
fos
.
close
();
}
catch
(
IOException
ioe
)
{
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
ioe
.
printStackTrace
();
}
}
}
}
/**
/**
* 合并两个文件
* 合并两个文件
*
*
* @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
;
FileInputStream
fis
=
null
;
FileInputStream
fis
=
null
;
if
(!
outFile
.
exists
())
{
if
(!
outFile
.
exists
())
{
try
{
try
{
outFile
.
createNewFile
();
outFile
.
createNewFile
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
}
}
}
}
try
{
try
{
// 合并其实就是文件的续写,写成true
// 合并其实就是文件的续写,写成true
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
)
{
ioe
.
printStackTrace
();
ioe
.
printStackTrace
();
}
finally
{
}
finally
{
try
{
try
{
if
(
fis
!=
null
)
if
(
fis
!=
null
)
fis
.
close
();
fis
.
close
();
if
(
fos
!=
null
)
if
(
fos
!=
null
)
fos
.
close
();
fos
.
close
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
}
}
}
}
}
}
/**
/**
* 合并多个文件
* 合并多个文件
*
*
* @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
;
FileInputStream
fis
=
null
;
FileInputStream
fis
=
null
;
if
(!
outFile
.
exists
())
{
if
(!
outFile
.
exists
())
{
try
{
try
{
outFile
.
createNewFile
();
outFile
.
createNewFile
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"创建临时存储文件失败"
);
}
}
}
}
try
{
try
{
// 合并其实就是文件的续写,写成true
// 合并其实就是文件的续写,写成true
fos
=
new
FileOutputStream
(
outFile
,
true
);
fos
=
new
FileOutputStream
(
outFile
,
true
);
byte
[]
buf
=
new
byte
[
1024
*
1024
];
byte
[]
buf
=
new
byte
[
1024
*
1024
];
for
(
File
leafFile
:
leafFiles
)
{
for
(
File
leafFile
:
leafFiles
)
{
fis
=
new
FileInputStream
(
leafFile
);
fis
=
new
FileInputStream
(
leafFile
);
int
len
=
0
;
int
len
=
0
;
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
while
((
len
=
fis
.
read
(
buf
))
!=
-
1
)
{
fos
.
write
(
buf
,
0
,
len
);
fos
.
write
(
buf
,
0
,
len
);
}
}
}
}
}
catch
(
IOException
ioe
)
{
}
catch
(
IOException
ioe
)
{
ioe
.
printStackTrace
();
ioe
.
printStackTrace
();
}
finally
{
}
finally
{
try
{
try
{
if
(
fis
!=
null
)
if
(
fis
!=
null
)
fis
.
close
();
fis
.
close
();
if
(
fos
!=
null
)
if
(
fos
!=
null
)
fos
.
close
();
fos
.
close
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
}
}
}
}
}
}
/**
/**
* 音频文件转换
* 音频文件转换
*
*
* @param data 音频byte数组
* @param data 音频byte数组
* @param sourExt 原始后缀
* @param sourExt 原始后缀
* @param ext 新后缀
* @param ext 新后缀
* @return
* @return
* @author PENG
* @author PENG
*/
*/
public
static
String
audioFormatConvert
(
byte
[]
data
,
String
sourExt
,
String
ext
)
{
public
static
String
audioFormatConvert
(
byte
[]
data
,
String
sourExt
,
String
ext
)
{
File
tmpdir
=
new
File
(
"tempdir/local"
);
File
tmpdir
=
new
File
(
"tempdir/local"
);
if
(!
tmpdir
.
exists
())
{
if
(!
tmpdir
.
exists
())
{
try
{
try
{
tmpdir
.
mkdirs
();
tmpdir
.
mkdirs
();
}
catch
(
SecurityException
ex
)
{
}
catch
(
SecurityException
ex
)
{
System
.
out
.
println
(
"无法创建临时文件夹"
);
System
.
out
.
println
(
"无法创建临时文件夹"
);
ex
.
printStackTrace
();
ex
.
printStackTrace
();
}
}
}
}
String
name
=
UUID
.
randomUUID
().
toString
();
String
name
=
UUID
.
randomUUID
().
toString
();
File
source
=
new
File
(
tmpdir
,
name
+
"."
+
sourExt
);
File
source
=
new
File
(
tmpdir
,
name
+
"."
+
sourExt
);
boolean
creat
=
saveBytesTofile
(
source
,
data
);
boolean
creat
=
saveBytesTofile
(
source
,
data
);
if
(
creat
)
{
if
(
creat
)
{
File
target
=
new
File
(
tmpdir
,
name
+
"."
+
ext
);
File
target
=
new
File
(
tmpdir
,
name
+
"."
+
ext
);
AudioAttributes
audio
=
new
AudioAttributes
();
AudioAttributes
audio
=
new
AudioAttributes
();
Encoder
encoder
=
new
Encoder
();
Encoder
encoder
=
new
Encoder
();
audio
.
setCodec
(
"libmp3lame"
);
audio
.
setCodec
(
"libmp3lame"
);
EncodingAttributes
attrs
=
new
EncodingAttributes
();
EncodingAttributes
attrs
=
new
EncodingAttributes
();
attrs
.
setFormat
(
ext
.
toLowerCase
());
attrs
.
setFormat
(
ext
.
toLowerCase
());
attrs
.
setAudioAttributes
(
audio
);
attrs
.
setAudioAttributes
(
audio
);
try
{
try
{
encoder
.
encode
(
source
,
target
,
attrs
);
encoder
.
encode
(
source
,
target
,
attrs
);
return
target
.
getPath
();
return
target
.
getPath
();
}
catch
(
IllegalArgumentException
e
)
{
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
return
null
;
return
null
;
}
catch
(
InputFormatException
e
)
{
}
catch
(
InputFormatException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
return
null
;
return
null
;
}
catch
(
EncoderException
e
)
{
}
catch
(
EncoderException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
return
target
.
getPath
();
return
target
.
getPath
();
}
}
}
else
{
}
else
{
return
null
;
return
null
;
}
}
}
}
/**
/**
* 获取文本的内容
* 获取文本的内容
*
*
* @param url
* @param url
* @return
* @return
*/
*/
public
static
String
getTextString
(
String
url
)
{
public
static
String
getTextString
(
String
url
)
{
LOGGER
.
info
(
"【文件API】获取文本的内容.<START>.[url]="
+
url
);
LOGGER
.
info
(
"【文件API】获取文本的内容.<START>.[url]="
+
url
);
if
(
StringUtil
.
isEmpty
(
url
))
{
if
(
StringUtil
.
isEmpty
(
url
))
{
return
null
;
return
null
;
}
}
StringBuilder
lrcString
=
new
StringBuilder
();
StringBuilder
lrcString
=
new
StringBuilder
();
InputStream
is
=
null
;
InputStream
is
=
null
;
InputStreamReader
isr
=
null
;
InputStreamReader
isr
=
null
;
try
{
try
{
byte
[]
bt
=
downloadByteFromUrl
(
url
);
byte
[]
bt
=
downloadByteFromUrl
(
url
);
is
=
new
ByteArrayInputStream
(
bt
);
is
=
new
ByteArrayInputStream
(
bt
);
boolean
isUTF8Bom
=
CpdetectorEncoding
.
isUTF8Bom
(
is
);
boolean
isUTF8Bom
=
CpdetectorEncoding
.
isUTF8Bom
(
is
);
if
(
isUTF8Bom
)
{
if
(
isUTF8Bom
)
{
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
}
else
{
}
else
{
Object
charSet
=
CpdetectorEncoding
.
getEncoding
(
bt
,
false
);
Object
charSet
=
CpdetectorEncoding
.
getEncoding
(
bt
,
false
);
if
(
charSet
.
toString
().
equals
(
"UTF-8"
))
{
if
(
charSet
.
toString
().
equals
(
"UTF-8"
))
{
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
isr
=
new
InputStreamReader
(
is
,
"UTF-8"
);
}
else
{
}
else
{
isr
=
new
InputStreamReader
(
new
ByteArrayInputStream
(
bt
),
"GBK"
);
isr
=
new
InputStreamReader
(
new
ByteArrayInputStream
(
bt
),
"GBK"
);
}
}
}
}
BufferedReader
br
=
new
BufferedReader
(
isr
);
// 构造一个BufferedReader类来读取文件
BufferedReader
br
=
new
BufferedReader
(
isr
);
// 构造一个BufferedReader类来读取文件
String
s
;
String
s
;
while
((
s
=
br
.
readLine
())
!=
null
)
{
// 使用readLine方法,一次读一行
while
((
s
=
br
.
readLine
())
!=
null
)
{
// 使用readLine方法,一次读一行
lrcString
.
append
(
System
.
lineSeparator
()
+
s
);
lrcString
.
append
(
System
.
lineSeparator
()
+
s
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"【文件工具】读取文件内容.<ERROR>:"
+
e
.
getMessage
(),
e
);
LOGGER
.
error
(
"【文件工具】读取文件内容.<ERROR>:"
+
e
.
getMessage
(),
e
);
}
finally
{
}
finally
{
try
{
try
{
if
(
isr
!=
null
)
if
(
isr
!=
null
)
isr
.
close
();
isr
.
close
();
if
(
is
!=
null
)
if
(
is
!=
null
)
is
.
close
();
is
.
close
();
}
catch
(
Exception
e2
)
{
}
catch
(
Exception
e2
)
{
}
}
}
}
LOGGER
.
info
(
"【文件API】获取文本的内容.<END>"
);
LOGGER
.
info
(
"【文件API】获取文本的内容.<END>"
);
return
lrcString
.
toString
();
return
lrcString
.
toString
();
}
}
/**
/**
* java 8 读取文本内容
* java 8 读取文本内容
*
*
* @param url
* @param url
* @return
* @return
*/
*/
public
static
String
getTextString8
(
String
url
)
{
public
static
String
getTextString8
(
String
url
)
{
LOGGER
.
info
(
"【文件API】获取文本的内容8.<START>.[url]="
+
url
);
LOGGER
.
info
(
"【文件API】获取文本的内容8.<START>.[url]="
+
url
);
StringBuilder
result
=
new
StringBuilder
();
StringBuilder
result
=
new
StringBuilder
();
String
fileType
=
getFileType
(
url
);
String
fileType
=
getFileType
(
url
);
String
localFilePath
=
url
;
String
localFilePath
=
url
;
if
(
url
.
startsWith
(
"http"
))
{
if
(
url
.
startsWith
(
"http"
))
{
localFilePath
=
FilePathConst
.
TEXT_PATH
+
UUIDUitl
.
taskName
()
+
"."
+
fileType
;
localFilePath
=
FilePathConst
.
TEXT_PATH
+
UUIDUitl
.
taskName
()
+
"."
+
fileType
;
downloadFileFromUrl
(
url
,
localFilePath
);
downloadFileFromUrl
(
url
,
localFilePath
);
}
}
try
{
try
{
Stream
<
String
>
lines
=
Files
.
lines
(
Paths
.
get
(
localFilePath
),
StandardCharsets
.
UTF_8
);
Stream
<
String
>
lines
=
Files
.
lines
(
Paths
.
get
(
localFilePath
),
StandardCharsets
.
UTF_8
);
lines
.
forEach
(
line
->
{
lines
.
forEach
(
line
->
{
result
.
append
(
line
+
System
.
lineSeparator
());
result
.
append
(
line
+
System
.
lineSeparator
());
});
});
}
catch
(
Exception
e
)
{
lines
.
close
();
LOGGER
.
error
(
"【文件工具】读取文件内容8.<ERROR>:"
+
e
.
getMessage
(),
e
);
}
catch
(
Exception
e
)
{
}
finally
{
LOGGER
.
error
(
"【文件工具】读取文件内容8.<ERROR>:"
+
e
.
getMessage
(),
e
);
deleteFile
(
localFilePath
);
}
finally
{
}
deleteFile
(
localFilePath
);
LOGGER
.
info
(
"【文件API】获取文本的内容8.<END>"
);
}
return
result
.
toString
();
LOGGER
.
info
(
"【文件API】获取文本的内容8.<END>"
);
}
return
result
.
toString
();
}
/**
* 文件剪切,网络路径、按文件大小的百分比
/**
*
* 文件剪切,网络路径、按文件大小的百分比
* @param url
*
* @param percent 1~100的整数
* @param url
* @return 返回剪切后上传后的文件路径
* @param percent
* @throws BizException
* 1~100的整数
*/
* @return 返回剪切后上传后的文件路径
public
static
String
clipByPercent
(
String
url
,
int
percent
)
throws
BizException
{
* @throws BizException
LOGGER
.
info
(
"【文件API】文件剪切,按百分比.<START>.[url]="
+
url
);
*/
if
(
StringUtil
.
isEmpty
(
url
))
{
public
static
String
clipByPercent
(
String
url
,
int
percent
)
throws
BizException
{
return
null
;
LOGGER
.
info
(
"【文件API】文件剪切,按百分比.<START>.[url]="
+
url
);
}
if
(
StringUtil
.
isEmpty
(
url
))
{
String
fileType
=
FileUtils
.
getFileType
(
url
);
return
null
;
if
(!
FileUtils
.
VIDEO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
}
return
null
;
String
fileType
=
FileUtils
.
getFileType
(
url
);
}
if
(!
FileUtils
.
VIDEO
.
equals
(
FileUtils
.
getGatherName
(
fileType
)))
{
// 下载文件
return
null
;
String
downloadPath
=
FilePathConst
.
DOWNLOAD_PATH
+
UUIDUitl
.
generateString
(
32
)
+
"."
+
fileType
;
}
FileUtils
.
downloadFileFromUrl
(
url
,
downloadPath
);
// 下载文件
// 剪切文件
String
downloadPath
=
FilePathConst
.
DOWNLOAD_PATH
+
UUIDUitl
.
generateString
(
32
)
+
"."
+
fileType
;
String
cutPath
=
clipByPercent
(
new
File
(
downloadPath
),
percent
);
FileUtils
.
downloadFileFromUrl
(
url
,
downloadPath
);
// 重新上传文件
// 剪切文件
UploadResultInfo
uploadResultInfo
=
OssUtils
.
uploadLocalFile
(
cutPath
,
null
);
String
cutPath
=
clipByPercent
(
new
File
(
downloadPath
),
percent
);
// 删除本地文件
// 重新上传文件
FileUtils
.
deleteFile
(
downloadPath
);
UploadResultInfo
uploadResultInfo
=
OssUtils
.
uploadLocalFile
(
cutPath
,
null
);
FileUtils
.
deleteFile
(
cutPath
);
// 删除本地文件
LOGGER
.
info
(
"【文件API】文件剪切,按百分比.<END>.[uploadResultInfo]="
+
uploadResultInfo
);
FileUtils
.
deleteFile
(
downloadPath
);
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
FileUtils
.
deleteFile
(
cutPath
);
}
LOGGER
.
info
(
"【文件API】文件剪切,按百分比.<END>.[uploadResultInfo]="
+
uploadResultInfo
);
return
uploadResultInfo
==
null
?
null
:
uploadResultInfo
.
getUrl
();
/**
}
* 文件剪切,本地路径、按百分比
*
/**
* @param inputFile
* 文件剪切,本地路径、按百分比
* @param cutPercent 1~100的整数
*
* @return 返回本地路径地址
* @param inputFile
* @throws BizException
* @param cutPercent
*/
* 1~100的整数
public
static
String
clipByPercent
(
File
inputFile
,
int
cutPercent
)
throws
BizException
{
* @return 返回本地路径地址
LOGGER
.
info
(
"【文件API】文件剪切.<START>.[cutPercent]="
+
cutPercent
);
* @throws BizException
if
(
inputFile
==
null
||
inputFile
.
length
()
<=
0
)
{
*/
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"不是有效的文件"
);
public
static
String
clipByPercent
(
File
inputFile
,
int
cutPercent
)
throws
BizException
{
}
LOGGER
.
info
(
"【文件API】文件剪切.<START>.[cutPercent]="
+
cutPercent
);
if
(
cutPercent
>
100
||
cutPercent
<
1
)
{
if
(
inputFile
==
null
||
inputFile
.
length
()
<=
0
)
{
throw
new
FileException
(
FileException
.
FILE_READ_FAILURE
,
"百分比必须是1~100之间的整数"
);
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"不是有效的文件"
);
}
}
return
clipByLength
(
inputFile
,
Math
.
round
(
inputFile
.
length
()
*
cutPercent
/
100.0d
));
if
(
cutPercent
>
100
||
cutPercent
<
1
)
{
}
throw
new
FileException
(
FileException
.
FILE_READ_FAILURE
,
"百分比必须是1~100之间的整数"
);
}
/**
return
clipByLength
(
inputFile
,
Math
.
round
(
inputFile
.
length
()
*
cutPercent
/
100.0d
));
* 文件剪切
}
*
* @param inputFile 待剪切的文件
/**
* @param cutLength 剪切长度(Byte)
* 文件剪切
*/
*
public
static
String
clipByLength
(
File
inputFile
,
long
cutLength
)
throws
BizException
{
* @param inputFile
LOGGER
.
info
(
"【文件API】文件剪切.<START>.[cutLength]="
+
cutLength
);
* 待剪切的文件
if
(
inputFile
==
null
||
inputFile
.
length
()
<=
0
)
{
* @param cutLength
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"不是有效的文件"
);
* 剪切长度(Byte)
}
*/
if
(
cutLength
<
1024
)
{
public
static
String
clipByLength
(
File
inputFile
,
long
cutLength
)
throws
BizException
{
throw
new
FileException
(
FileException
.
FILE_READ_FAILURE
,
"剪切的文件必须大于1KB"
);
LOGGER
.
info
(
"【文件API】文件剪切.<START>.[cutLength]="
+
cutLength
);
}
if
(
inputFile
==
null
||
inputFile
.
length
()
<=
0
)
{
// 输出文件路径
throw
new
FileException
(
FileException
.
FILE_NOT_EXIST
,
"不是有效的文件"
);
String
outFilePath
=
FilePathConst
.
CUT_PATH
+
UUIDUitl
.
generateString
(
12
)
+
"_"
+
inputFile
.
getName
();
}
creatFiles
(
outFilePath
);
if
(
cutLength
<
1024
)
{
// 剪切长度
throw
new
FileException
(
FileException
.
FILE_READ_FAILURE
,
"剪切的文件必须大于1KB"
);
cutLength
=
cutLength
>
inputFile
.
length
()
?
inputFile
.
length
()
:
cutLength
;
}
BufferedInputStream
bis
=
null
;
// 输出文件路径
BufferedOutputStream
bos
=
null
;
String
outFilePath
=
FilePathConst
.
CUT_PATH
+
UUIDUitl
.
generateString
(
12
)
+
"_"
+
inputFile
.
getName
();
try
{
creatFiles
(
outFilePath
);
// 两个输入流
// 剪切长度
bis
=
new
BufferedInputStream
(
new
FileInputStream
(
inputFile
));
cutLength
=
cutLength
>
inputFile
.
length
()
?
inputFile
.
length
()
:
cutLength
;
// 缓冲字节输出流
BufferedInputStream
bis
=
null
;
bos
=
new
BufferedOutputStream
(
new
FileOutputStream
(
outFilePath
));
BufferedOutputStream
bos
=
null
;
// 读取多少次
try
{
long
index
=
cutLength
%
1024
>
0
?
cutLength
/
1024
+
1
:
cutLength
/
1024
;
// 两个输入流
int
mod
=
(
int
)
cutLength
%
1024
;
bis
=
new
BufferedInputStream
(
new
FileInputStream
(
inputFile
));
byte
[]
bt
=
new
byte
[
1024
];
// 缓冲字节输出流
int
len
=
0
;
bos
=
new
BufferedOutputStream
(
new
FileOutputStream
(
outFilePath
));
for
(
int
i
=
0
;
i
<
index
;
i
++)
{
// 读取多少次
if
(
i
+
1
==
index
&&
mod
!=
0
)
{
long
index
=
cutLength
%
1024
>
0
?
cutLength
/
1024
+
1
:
cutLength
/
1024
;
bt
=
new
byte
[
mod
];
int
mod
=
(
int
)
cutLength
%
1024
;
len
=
bis
.
read
(
bt
);
byte
[]
bt
=
new
byte
[
1024
];
}
else
{
int
len
=
0
;
len
=
bis
.
read
(
bt
);
for
(
int
i
=
0
;
i
<
index
;
i
++)
{
}
if
(
i
+
1
==
index
&&
mod
!=
0
)
{
if
(
len
!=
-
1
)
{
bt
=
new
byte
[
mod
];
bos
.
write
(
bt
,
0
,
len
);
len
=
bis
.
read
(
bt
);
}
}
else
{
}
len
=
bis
.
read
(
bt
);
bos
.
flush
();
}
}
catch
(
IOException
e
)
{
if
(
len
!=
-
1
)
{
LOGGER
.
error
(
"【文件工具】剪切文件内容:"
+
e
.
getMessage
(),
e
);
bos
.
write
(
bt
,
0
,
len
);
throw
new
FileException
(
FileException
.
FILE_READ_FAILURE
,
"文件剪切失败"
);
}
}
finally
{
}
try
{
bos
.
flush
();
if
(
bis
!=
null
)
}
catch
(
IOException
e
)
{
bis
.
close
();
LOGGER
.
error
(
"【文件工具】剪切文件内容:"
+
e
.
getMessage
(),
e
);
if
(
bos
!=
null
)
throw
new
FileException
(
FileException
.
FILE_READ_FAILURE
,
"文件剪切失败"
);
bos
.
close
();
}
finally
{
}
catch
(
IOException
e
)
{
try
{
LOGGER
.
error
(
"【文件工具】文件流关闭失败:"
+
e
.
getMessage
(),
e
);
if
(
bis
!=
null
)
}
bis
.
close
();
}
if
(
bos
!=
null
)
LOGGER
.
info
(
"【文件API】文件剪切.<END>"
);
bos
.
close
();
return
outFilePath
;
}
catch
(
IOException
e
)
{
}
LOGGER
.
error
(
"【文件工具】文件流关闭失败:"
+
e
.
getMessage
(),
e
);
}
/**
}
* 将文件转成base64 字符串
LOGGER
.
info
(
"【文件API】文件剪切.<END>"
);
*
return
outFilePath
;
* @param data
}
* @return *
* @throws Exception
/**
*/
* 将文件转成base64 字符串
public
static
String
encodeBase64File
(
byte
[]
data
)
throws
BizException
{
*
try
{
* @param data
return
new
String
(
Base64
.
encodeBase64
(
data
));
* @return *
}
catch
(
Exception
e
)
{
* @throws Exception
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
*/
return
null
;
public
static
String
encodeBase64File
(
byte
[]
data
)
throws
BizException
{
}
try
{
}
return
new
String
(
Base64
.
encodeBase64
(
data
));
}
catch
(
Exception
e
)
{
/**
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
* 将base64字符解码保存文件
return
null
;
*
}
* @param base64Code
}
* @param targetPath
* @throws Exception
/**
*/
* 将base64字符解码保存文件
public
static
boolean
decoderBase64File
(
String
base64Code
,
String
targetPath
)
throws
BizException
{
*
if
(
base64Code
==
null
)
* @param base64Code
return
false
;
* @param targetPath
OutputStream
out
=
null
;
* @throws Exception
try
{
*/
// Base64解码
public
static
boolean
decoderBase64File
(
String
base64Code
,
String
targetPath
)
throws
BizException
{
byte
[]
b
=
Base64
.
decodeBase64
(
targetPath
);
if
(
base64Code
==
null
)
for
(
int
i
=
0
;
i
<
b
.
length
;
++
i
)
{
return
false
;
if
(
b
[
i
]
<
0
)
{
// 调整异常数据
OutputStream
out
=
null
;
b
[
i
]
+=
256
;
try
{
}
// Base64解码
}
byte
[]
b
=
Base64
.
decodeBase64
(
targetPath
);
// 生成jpeg图片
for
(
int
i
=
0
;
i
<
b
.
length
;
++
i
)
{
out
=
new
FileOutputStream
(
targetPath
);
if
(
b
[
i
]
<
0
)
{
// 调整异常数据
out
.
write
(
b
);
b
[
i
]
+=
256
;
out
.
flush
();
}
}
catch
(
Exception
e
)
{
}
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
// 生成jpeg图片
return
false
;
out
=
new
FileOutputStream
(
targetPath
);
}
finally
{
out
.
write
(
b
);
try
{
out
.
flush
();
if
(
out
!=
null
)
}
catch
(
Exception
e
)
{
out
.
close
();
LOGGER
.
error
(
"【文件工具】将base64字符解码保存文件失败:"
+
e
.
getMessage
(),
e
);
}
catch
(
Exception
e
)
{
return
false
;
}
}
finally
{
}
try
{
return
true
;
if
(
out
!=
null
)
}
out
.
close
();
}
catch
(
Exception
e
)
{
/**
}
* 本地文件到byte数组
}
*
return
true
;
* @param path
}
* @return
*/
/**
public
static
byte
[]
file2byte
(
String
path
)
{
* 本地文件到byte数组
byte
[]
data
=
null
;
*
FileInputStream
fis
=
null
;
* @param path
ByteArrayOutputStream
os
=
null
;
* @return
try
{
*/
fis
=
new
FileInputStream
(
new
File
(
path
));
public
static
byte
[]
file2byte
(
String
path
)
{
os
=
new
ByteArrayOutputStream
();
byte
[]
data
=
null
;
byte
[]
buf
=
new
byte
[
1024
];
FileInputStream
fis
=
null
;
int
numBytesRead
=
0
;
ByteArrayOutputStream
os
=
null
;
while
((
numBytesRead
=
fis
.
read
(
buf
))
!=
-
1
)
{
try
{
os
.
write
(
buf
,
0
,
numBytesRead
);
fis
=
new
FileInputStream
(
new
File
(
path
));
}
os
=
new
ByteArrayOutputStream
();
data
=
os
.
toByteArray
();
byte
[]
buf
=
new
byte
[
1024
];
}
catch
(
Exception
e
)
{
int
numBytesRead
=
0
;
LOGGER
.
error
(
"【文件工具】文件到byte数组:"
+
e
.
getMessage
(),
e
);
while
((
numBytesRead
=
fis
.
read
(
buf
))
!=
-
1
)
{
}
finally
{
os
.
write
(
buf
,
0
,
numBytesRead
);
try
{
}
if
(
fis
!=
null
)
data
=
os
.
toByteArray
();
fis
.
close
();
}
catch
(
Exception
e
)
{
if
(
os
!=
null
)
LOGGER
.
error
(
"【文件工具】文件到byte数组:"
+
e
.
getMessage
(),
e
);
os
.
close
();
}
finally
{
}
catch
(
IOException
e
)
{
try
{
LOGGER
.
error
(
"【文件工具】文件流关闭失败:"
+
e
.
getMessage
(),
e
);
if
(
fis
!=
null
)
}
fis
.
close
();
}
if
(
os
!=
null
)
return
data
;
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
();
* @param inStream 输入流
byte
[]
buffer
=
new
byte
[
1024
];
* @return
int
len
=
0
;
* @throws Exception
try
{
*/
while
((
len
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
public
static
byte
[]
readInputStream
(
InputStream
inStream
)
throws
Exception
{
outStream
.
write
(
buffer
,
0
,
len
);
ByteArrayOutputStream
outStream
=
new
ByteArrayOutputStream
();
}
byte
[]
buffer
=
new
byte
[
1024
];
}
catch
(
Exception
e
)
{
int
len
=
0
;
LOGGER
.
error
(
"【文件工具】文件流到byte数组:"
+
e
.
getMessage
(),
e
);
try
{
}
finally
{
while
((
len
=
inStream
.
read
(
buffer
))
!=
-
1
)
{
inStream
.
close
();
outStream
.
write
(
buffer
,
0
,
len
);
}
}
return
outStream
.
toByteArray
();
}
catch
(
Exception
e
)
{
}
LOGGER
.
error
(
"【文件工具】文件流到byte数组:"
+
e
.
getMessage
(),
e
);
}
finally
{
}
inStream
.
close
();
}
return
outStream
.
toByteArray
();
}
}
pcloud-common/src/main/java/com/pcloud/common/utils/aliyun/OssUtils.java
View file @
44b1f4bd
...
@@ -151,6 +151,23 @@ public class OssUtils {
...
@@ -151,6 +151,23 @@ public class OssUtils {
}
}
/**
/**
* 父子文件上传,子文件全路径和父文件相同,文件后缀类型自定义
*
* @param filePath
* 子文件本地路径
* @param parentFileUrl
* 仅限OSS的全路径,其他本地文件路径均不支持
* @return
* @throws FileException
*/
public
static
UploadResultInfo
uploadLocalFile4Child
(
String
filePath
,
String
parentFileUrl
)
throws
FileException
{
String
fileType
=
FileUtils
.
getFileType
(
filePath
);
String
outObjectKey
=
splitObjectKey
(
parentFileUrl
)
+
"."
+
fileType
;
uploadPointFile2OSS
(
filePath
,
AliyunConstant
.
FILE_BUCKET
,
outObjectKey
);
return
getResultInfo
(
outObjectKey
,
new
File
(
filePath
).
length
(),
null
,
AliyunConstant
.
FILE_BUCKET
);
}
/**
* byte[]数组文件流上传 ,isFlag=true,表示上传成功
* byte[]数组文件流上传 ,isFlag=true,表示上传成功
*
*
* @param buff
* @param buff
...
@@ -747,36 +764,6 @@ public class OssUtils {
...
@@ -747,36 +764,6 @@ public class OssUtils {
return
resultFileUrl
;
return
resultFileUrl
;
}
}
public
static
String
submitClipJobFixBug
(
String
fileName
,
String
fileUrl
,
String
seek
,
String
duration
)
throws
FileException
{
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业(fixBug).<START>.[fileUrl]="
+
fileUrl
+
",[seek]="
+
seek
+
",[duration]="
+
duration
);
if
(
StringUtil
.
isEmpty
(
duration
))
{
return
null
;
}
String
fileType
=
FileUtils
.
getFileType
(
fileUrl
);
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
if
(!(
FileUtils
.
VIDEO
.
equals
(
gatherName
)
||
FileUtils
.
AUDIO
.
equals
(
gatherName
)))
{
return
null
;
}
String
resultFileUrl
=
""
;
String
outFileType
=
FileUtils
.
VIDEO
.
equals
(
gatherName
)
?
"mp4"
:
"mp3"
;
if
(
StringTools
.
contains
(
fileUrl
,
AliyunConstant
.
OSS_CDN_URLS
))
{
String
inObjectKey
=
splitObjectKey
(
fileUrl
);
String
inBucketName
=
fileUrlExist001
(
fileUrl
)
?
AliyunConstant
.
INPUT_BUCKET
:
AliyunConstant
.
getBucketName
(
fileUrl
);
OSSFileDO
ossFileDO
=
new
OSSFileDO
(
AliyunConstant
.
getOssRegion
(
inBucketName
),
inBucketName
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKeyFixBug
(
AliyunEnum
.
CUT
.
value
,
fileName
,
outFileType
);
resultFileUrl
=
clipJob
(
ossFileDO
,
outObjectKey
,
seek
,
duration
);
}
else
{
String
inObjectKey
=
getInObjectKey
(
AliyunEnum
.
CUT
.
value
,
fileName
,
fileType
);
OSSFileDO
ossFileDO
=
uploadPointFile2OSS
(
fileUrl
,
AliyunConstant
.
FILE_BUCKET
,
inObjectKey
);
String
outObjectKey
=
getOutObjectKey
(
AliyunEnum
.
CUT
.
value
,
fileName
,
outFileType
);
resultFileUrl
=
clipJob
(
ossFileDO
,
outObjectKey
,
seek
,
duration
);
}
LOGGER
.
info
(
"【aliOSS】提交音视频剪切作业.<END>.[resultFileUrl]="
+
resultFileUrl
);
return
resultFileUrl
;
}
/**
/**
* 提交视频剪切作业
* 提交视频剪切作业
*
*
...
@@ -1279,14 +1266,6 @@ public class OssUtils {
...
@@ -1279,14 +1266,6 @@ public class OssUtils {
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
}
}
private
static
String
getOutObjectKeyFixBug
(
String
uploadType
,
String
fileName
,
String
fileType
)
{
fileName
=
StringUtil
.
isEmpty
(
fileName
)
?
UUIDUitl
.
taskName
()
:
FileUtils
.
formatName
(
fileName
);
String
gatherName
=
FileUtils
.
getGatherName
(
fileType
);
gatherName
=
StringUtil
.
isEmpty
(
gatherName
)
?
"other"
:
gatherName
.
toLowerCase
();
return
new
StringBuilder
(
"oss/"
).
append
(
uploadType
).
append
(
"/"
).
append
(
gatherName
).
append
(
"/"
).
append
(
fileType
)
.
append
(
"/"
).
append
(
fileName
).
append
(
"."
).
append
(
fileType
).
toString
();
}
/**
/**
* 获取文件下载加密密钥,默认有效期48小时 <br>
* 获取文件下载加密密钥,默认有效期48小时 <br>
* 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
* 密钥需要结合域名https://download.5rs.me使用,可以在浏览器直接下载 <br>
...
...
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