Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
95257366
Commit
95257366
authored
Nov 11, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件处理
parent
f389179a
Changes
12
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
499 additions
and
29 deletions
+499
-29
pom.xml
pom.xml
+13
-0
FileHelper.java
...src/main/java/com/yanzuoguang/util/helper/FileHelper.java
+43
-0
YzgFileConfig.java
...c/main/java/com/yanzuoguang/cloud/file/YzgFileConfig.java
+112
-0
YzgFileService.java
.../main/java/com/yanzuoguang/cloud/file/YzgFileService.java
+21
-0
readme.md
...-cloud/src/main/java/com/yanzuoguang/cloud/file/readme.md
+1
-0
YzgFileBaseVo.java
...ain/java/com/yanzuoguang/cloud/file/vo/YzgFileBaseVo.java
+84
-0
YzgFileUploadItemResVo.java
...com/yanzuoguang/cloud/file/vo/YzgFileUploadItemResVo.java
+27
-0
YzgFileUploadReqVo.java
...ava/com/yanzuoguang/cloud/file/vo/YzgFileUploadReqVo.java
+43
-0
YzgFileUploadResVo.java
...ava/com/yanzuoguang/cloud/file/vo/YzgFileUploadResVo.java
+30
-0
pom.xml
yzg-util-file/pom.xml
+35
-0
YzgFileServiceImpl.java
...n/java/com/yanzuoguang/cloud/file/YzgFileServiceImpl.java
+90
-0
ImageHelper.java
...image/src/main/java/com/yanzuoguang/util/ImageHelper.java
+0
-29
No files found.
pom.xml
View file @
95257366
...
...
@@ -18,6 +18,7 @@
<module>
yzg-util-image-extend
</module>
<module>
yzg-util-print
</module>
<module>
yzg-util-chinese
</module>
<module>
yzg-util-file
</module>
</modules>
<properties>
...
...
@@ -144,6 +145,12 @@
<version>
${yzg.version}
</version>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-image-extend
</artifactId>
<version>
${yzg.version}
</version>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-print
</artifactId>
...
...
@@ -155,6 +162,12 @@
<artifactId>
yzg-util-chinese
</artifactId>
<version>
${yzg.version}
</version>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-file
</artifactId>
<version>
${yzg.version}
</version>
</dependency>
</dependencies>
</dependencyManagement>
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/FileHelper.java
View file @
95257366
...
...
@@ -13,6 +13,11 @@ import java.nio.file.Paths;
*/
public
class
FileHelper
{
public
static
final
int
FILE_TYPE_NONE
=
0
;
public
static
final
int
FILE_TYPE_IMAGE
=
1
;
public
static
final
int
FILE_TYPE_VIDEO
=
2
;
public
static
final
int
FILE_TYPE_AUDIO
=
3
;
/**
* 获取文件类型
*
...
...
@@ -34,6 +39,44 @@ public class FileHelper {
}
}
/**
* 获取文件媒体类型
*
* @param file
* @return
* @throws IOException
*/
public
static
int
getMediaType
(
String
file
)
{
String
mimeType
=
FileHelper
.
getMimeType
(
file
);
if
(
mimeType
==
null
)
{
throw
new
RuntimeException
(
"不能识别文件类型"
+
file
);
}
int
type
=
getMediaTypeByMime
(
mimeType
);
// System.out.println(mimeType);
if
(
type
==
FILE_TYPE_NONE
)
{
throw
new
RuntimeException
(
"文件类型不为图片、音频、视频"
);
}
return
type
;
}
/**
* 获取视频类型
*
* @param mimeType
* @return
*/
public
static
int
getMediaTypeByMime
(
String
mimeType
)
{
if
(
mimeType
.
startsWith
(
"video/"
))
{
return
FILE_TYPE_VIDEO
;
}
else
if
(
mimeType
.
startsWith
(
"audio/"
))
{
return
FILE_TYPE_AUDIO
;
}
else
if
(
mimeType
.
startsWith
(
"image/"
))
{
return
FILE_TYPE_IMAGE
;
}
else
{
return
FILE_TYPE_NONE
;
}
}
/**
* 创建目录
*
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/file/YzgFileConfig.java
0 → 100644
View file @
95257366
package
com
.
yanzuoguang
.
cloud
.
file
;
import
com.yanzuoguang.cloud.file.vo.YzgFileBaseVo
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
java.io.File
;
/**
* 文件配置对象
*/
@Component
public
class
YzgFileConfig
{
/**
* 服务器存储地址,注意权限
*/
@Value
(
"${yzg.upload.server:}"
)
private
String
serverUrl
;
/**
* 外网地址
*/
@Value
(
"${yzg.upload.display:}"
)
private
String
displayUrl
;
public
String
getServerUrl
()
{
if
(
StringHelper
.
isEmpty
(
this
.
serverUrl
))
{
throw
new
CodeException
(
"请配置yzg.upload.server"
);
}
return
serverUrl
;
}
public
String
getDisplayUrl
()
{
if
(
StringHelper
.
isEmpty
(
this
.
serverUrl
))
{
throw
new
CodeException
(
"请配置yzg.upload.display"
);
}
return
displayUrl
;
}
/**
* 初始化文件对象
*
* @param item 文件对象
* @param serverFile 默认路径
*/
public
void
init
(
YzgFileBaseVo
item
,
String
serverFile
)
{
String
fromUrl
=
getServerPath
(
serverFile
);
// 服务器相对路径
item
.
setServer
(
fromUrl
);
// 对外显示全路径
item
.
setDisplay
(
getUrl
(
this
.
getDisplayUrl
(),
fromUrl
));
// 文件全路径
String
serverFullPath
=
getUrl
(
this
.
getServerUrl
(),
fromUrl
);
// 获取文件大小
File
file
=
new
File
(
serverFullPath
);
item
.
setSize
(
file
.
length
());
// 获取mime
item
.
setMime
(
FileHelper
.
getMimeType
(
serverFullPath
));
item
.
setType
(
FileHelper
.
getMediaTypeByMime
(
item
.
getMime
()));
}
/**
* 获取相对路径
*
* @param fromUrl 来源文件
* @return
*/
public
String
getServerPath
(
String
fromUrl
)
{
String
serverUrl
=
this
.
getServerUrl
();
String
displayUrl
=
this
.
getDisplayUrl
();
// 处理serverFile为简写
if
(
fromUrl
.
startsWith
(
serverUrl
))
{
fromUrl
=
fromUrl
.
substring
(
serverUrl
.
length
());
}
if
(
fromUrl
.
startsWith
(
displayUrl
))
{
fromUrl
=
fromUrl
.
substring
(
displayUrl
.
length
());
}
return
fromUrl
;
}
/**
* 获取服务器完全地址
*
* @param fromUrl
* @return
*/
public
String
getServerFullPath
(
String
fromUrl
)
{
fromUrl
=
getServerPath
(
fromUrl
);
return
getUrl
(
this
.
getServerUrl
(),
fromUrl
);
}
/**
* 获取服务器完全地址
*
* @param fromUrl
* @return
*/
public
String
getDisplayPath
(
String
fromUrl
)
{
fromUrl
=
getServerPath
(
fromUrl
);
return
getUrl
(
this
.
getDisplayUrl
(),
fromUrl
);
}
private
String
getUrl
(
String
folder
,
String
fromUrl
)
{
return
String
.
format
(
"%s/%s"
,
folder
,
fromUrl
);
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/file/YzgFileService.java
0 → 100644
View file @
95257366
package
com
.
yanzuoguang
.
cloud
.
file
;
import
com.yanzuoguang.cloud.file.vo.YzgFileUploadReqVo
;
import
com.yanzuoguang.cloud.file.vo.YzgFileUploadResVo
;
/**
* 文件上传路径
*
* @author 颜佐光
*/
public
interface
YzgFileService
{
/**
* 压缩文件
*
* @param req 文件上传请求参数
* @return
*/
YzgFileUploadResVo
upload
(
YzgFileUploadReqVo
req
);
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/file/readme.md
0 → 100644
View file @
95257366
# 文件处理模块
\ No newline at end of file
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/file/vo/YzgFileBaseVo.java
0 → 100644
View file @
95257366
package
com
.
yanzuoguang
.
cloud
.
file
.
vo
;
import
com.yanzuoguang.util.vo.BaseVo
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
/**
* 文件基本信息
*
* @author 颜佐光
*/
@ApiModel
(
description
=
"文件基本信息"
)
public
class
YzgFileBaseVo
extends
BaseVo
{
/**
* 服务器相对路径,一般来说,该路径需要交给其他后台程序进行处理
*/
@ApiModelProperty
(
notes
=
"服务器相对路径,一般来说,该路径需要交给其他后台程序进行处理"
,
position
=
1000
)
private
String
server
;
/**
* 外网显示路径,用于前台显示
*/
@ApiModelProperty
(
notes
=
"外网显示路径,用于前台显示"
,
position
=
1010
)
private
String
display
;
/**
* 文件大小
*/
@ApiModelProperty
(
notes
=
"文件大小"
,
position
=
1020
)
private
long
size
;
/**
* 文件头: mime
*/
@ApiModelProperty
(
notes
=
"文件头: mime"
,
position
=
1030
)
private
String
mime
;
/**
* 文教类型归档:0-其他文件,1-图片,2-视频
*/
@ApiModelProperty
(
notes
=
"文教类型归档:0-其他文件,1-图片,2-视频"
,
position
=
1040
)
private
int
type
;
public
String
getServer
()
{
return
server
;
}
public
void
setServer
(
String
server
)
{
this
.
server
=
server
;
}
public
String
getDisplay
()
{
return
display
;
}
public
void
setDisplay
(
String
display
)
{
this
.
display
=
display
;
}
public
long
getSize
()
{
return
size
;
}
public
void
setSize
(
long
size
)
{
this
.
size
=
size
;
}
public
String
getMime
()
{
return
mime
;
}
public
void
setMime
(
String
mime
)
{
this
.
mime
=
mime
;
}
public
int
getType
()
{
return
type
;
}
public
void
setType
(
int
type
)
{
this
.
type
=
type
;
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/file/vo/YzgFileUploadItemResVo.java
0 → 100644
View file @
95257366
package
com
.
yanzuoguang
.
cloud
.
file
.
vo
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
/**
* 某个文件上传结果
*
* @author 颜佐光
*/
@ApiModel
(
description
=
"某个文件上传结果"
)
public
class
YzgFileUploadItemResVo
extends
YzgFileBaseVo
{
/**
* 上传的文件前台路径
*/
@ApiModelProperty
(
notes
=
"上传的文件前台路径"
,
position
=
10
)
private
String
localFile
;
public
String
getLocalFile
()
{
return
localFile
;
}
public
void
setLocalFile
(
String
localFile
)
{
this
.
localFile
=
localFile
;
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/file/vo/YzgFileUploadReqVo.java
0 → 100644
View file @
95257366
package
com
.
yanzuoguang
.
cloud
.
file
.
vo
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
org.springframework.web.multipart.MultipartFile
;
/**
* 图片上传接口请求参数,调用本接口后需要后台服务调用路径转换接口
*
* @author 颜佐光
*/
@ApiModel
(
description
=
"图片上传接口请求参数,调用本接口后需要后台服务调用路径转换接口"
)
public
class
YzgFileUploadReqVo
{
/**
* 文件数组
*/
@ApiModelProperty
(
notes
=
"文件数组"
)
private
MultipartFile
[]
file
;
/**
* 临时文件夹名称:默认temp
*/
@ApiModelProperty
(
notes
=
"临时文件夹名称:默认temp"
)
private
String
folder
;
public
MultipartFile
[]
getFile
()
{
return
file
;
}
public
void
setFile
(
MultipartFile
[]
file
)
{
this
.
file
=
file
;
}
public
String
getFolder
()
{
return
folder
;
}
public
void
setFolder
(
String
folder
)
{
this
.
folder
=
folder
;
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/file/vo/YzgFileUploadResVo.java
0 → 100644
View file @
95257366
package
com
.
yanzuoguang
.
cloud
.
file
.
vo
;
import
com.yanzuoguang.util.vo.BaseVo
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 上传文件输出结果
*
* @author 颜佐光
*/
@ApiModel
(
description
=
"上传文件输出结果"
)
public
class
YzgFileUploadResVo
extends
BaseVo
{
/**
* 文件上传结果列表
*/
@ApiModelProperty
(
notes
=
"文件上传结果列表"
,
position
=
10
)
private
List
<
YzgFileUploadItemResVo
>
list
=
new
ArrayList
<>();
public
List
<
YzgFileUploadItemResVo
>
getList
()
{
return
list
;
}
public
void
setList
(
List
<
YzgFileUploadItemResVo
>
list
)
{
this
.
list
=
list
;
}
}
yzg-util-file/pom.xml
0 → 100644
View file @
95257366
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
yzg-util
</artifactId>
<groupId>
com.yanzuoguang
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
yzg-util-file
</artifactId>
<dependencies>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-base
</artifactId>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-cloud
</artifactId>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-image
</artifactId>
</dependency>
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-image-extend
</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
yzg-util-file/src/main/java/com/yanzuoguang/cloud/file/YzgFileServiceImpl.java
0 → 100644
View file @
95257366
package
com
.
yanzuoguang
.
cloud
.
file
;
import
com.yanzuoguang.cloud.file.vo.YzgFileUploadItemResVo
;
import
com.yanzuoguang.cloud.file.vo.YzgFileUploadReqVo
;
import
com.yanzuoguang.cloud.file.vo.YzgFileUploadResVo
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.DateHelper
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.File
;
import
java.util.Date
;
/**
* 文件上传服务实现
*
* @author 颜佐光
*/
@Component
public
class
YzgFileServiceImpl
implements
YzgFileService
{
@Autowired
private
YzgFileConfig
fileConfig
;
/**
* 压缩文件
*
* @param req 文件上传请求参数
* @return
*/
@Override
public
YzgFileUploadResVo
upload
(
YzgFileUploadReqVo
req
)
{
YzgFileUploadResVo
res
=
new
YzgFileUploadResVo
();
if
(
req
.
getFile
()
==
null
||
req
.
getFile
().
length
==
0
)
{
throw
new
CodeException
(
"请上传文件"
);
}
String
folder
=
StringHelper
.
getFirst
(
req
.
getFolder
(),
"temp"
);
if
(
folder
.
contains
(
".."
))
{
throw
new
CodeException
(
"文件夹不能包含两个."
);
}
if
(
folder
.
contains
(
"\\u"
))
{
throw
new
CodeException
(
"文件夹不能包含\\u"
);
}
for
(
MultipartFile
uploadFile
:
req
.
getFile
())
{
// 获取上传源文件名和扩展名
String
originalFilename
=
uploadFile
.
getOriginalFilename
();
String
ext
=
originalFilename
.
substring
(
originalFilename
.
lastIndexOf
(
"."
));
// 保存的文件名
String
saveFileName
=
StringHelper
.
getNewID
()
+
ext
;
// 获取临时文件路径
String
tempFolder
=
getTempFolder
(
folder
);
String
serverFolder
=
String
.
format
(
"%s/%s"
,
fileConfig
.
getServerUrl
(),
tempFolder
);
// 创建服务器路径
FileHelper
.
createDirectory
(
serverFolder
);
// 设置保存文件的路径
String
serverFile
=
String
.
format
(
"%s/%s"
,
serverFolder
,
saveFileName
);
File
file
=
new
File
(
serverFile
);
try
{
uploadFile
.
transferTo
(
file
);
}
catch
(
Exception
ex
)
{
throw
new
CodeException
(
"文件保存失败"
,
ex
);
}
YzgFileUploadItemResVo
item
=
new
YzgFileUploadItemResVo
();
item
.
setLocalFile
(
originalFilename
);
fileConfig
.
init
(
item
,
serverFile
);
res
.
getList
().
add
(
item
);
}
return
res
;
}
/**
* 获取行记的显示的图片
*
* @return
*/
private
String
getTempFolder
(
String
folder
)
{
String
ret
=
String
.
format
(
"%s/%s/%s/"
,
folder
,
DateHelper
.
getDateTimeString
(
"yyyy-MM"
,
new
Date
()),
DateHelper
.
getToday
());
return
ret
;
}
}
yzg-util-image/src/main/java/com/yanzuoguang/util/ImageHelper.java
View file @
95257366
package
com
.
yanzuoguang
.
util
;
import
com.yanzuoguang.util.helper.FileHelper
;
import
net.coobird.thumbnailator.Thumbnails
;
import
javax.imageio.IIOImage
;
...
...
@@ -25,34 +24,6 @@ public class ImageHelper {
public
static
final
float
DEFAULT_SIZE
=
1
f
;
public
static
final
float
DEFAULT_QUALITY
=
1
f
;
public
static
final
int
MEDIA_TYPE_IMAGE
=
0
;
public
static
final
int
MEDIA_TYPE_VIDEO
=
1
;
public
static
final
int
MEDIA_TYPE_AUDIO
=
2
;
/**
* 获取文件媒体类型
*
* @param file
* @return
* @throws IOException
*/
public
static
int
getMediaType
(
String
file
)
{
String
mimeType
=
FileHelper
.
getMimeType
(
file
);
if
(
mimeType
==
null
)
{
throw
new
RuntimeException
(
"不能识别文件类型"
+
file
);
}
// System.out.println(mimeType);
if
(
mimeType
.
startsWith
(
"video/"
))
{
return
MEDIA_TYPE_VIDEO
;
}
else
if
(
mimeType
.
startsWith
(
"audio/"
))
{
return
MEDIA_TYPE_AUDIO
;
}
else
if
(
mimeType
.
startsWith
(
"image/"
))
{
return
MEDIA_TYPE_IMAGE
;
}
else
{
throw
new
RuntimeException
(
"文件类型不为图片、音频、视频"
);
}
}
/**
* 压缩图片
*
...
...
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