Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cloud-platform
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
youjj
cloud-platform
Commits
958531b8
Commit
958531b8
authored
Aug 01, 2019
by
hezhen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
123
parent
01fbac34
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
236 additions
and
3 deletions
+236
-3
pom.xml
xx-universal/xx-universal-api/pom.xml
+12
-0
FileTypeEnum.java
...c/platform/universal/constant/enumerate/FileTypeEnum.java
+24
-0
UnPackeUtil.java
...n/java/com/xxfc/platform/universal/utils/UnPackeUtil.java
+85
-0
CommonConfig.java
...java/com/xxfc/platform/universal/config/CommonConfig.java
+2
-2
UploadController.java
.../xxfc/platform/universal/controller/UploadController.java
+11
-0
FileUploadService.java
...om/xxfc/platform/universal/service/FileUploadService.java
+9
-0
UploadService.java
...va/com/xxfc/platform/universal/service/UploadService.java
+1
-1
FileUploadServiceImpl.java
...latform/universal/service/impl/FileUploadServiceImpl.java
+92
-0
No files found.
xx-universal/xx-universal-api/pom.xml
View file @
958531b8
...
@@ -60,6 +60,18 @@
...
@@ -60,6 +60,18 @@
<groupId>
com.google.code.gson
</groupId>
<groupId>
com.google.code.gson
</groupId>
<artifactId>
gson
</artifactId>
<artifactId>
gson
</artifactId>
</dependency>
</dependency>
<!--zip4j依赖,解压zip压缩-->
<dependency>
<groupId>
net.lingala.zip4j
</groupId>
<artifactId>
zip4j
</artifactId>
<version>
1.3.2
</version>
</dependency>
<!--解压rar压缩-->
<dependency>
<groupId>
com.github.junrar
</groupId>
<artifactId>
junrar
</artifactId>
<version>
0.7
</version>
</dependency>
</dependencies>
</dependencies>
...
...
xx-universal/xx-universal-api/src/main/java/com/xxfc/platform/universal/constant/enumerate/FileTypeEnum.java
0 → 100644
View file @
958531b8
package
com
.
xxfc
.
platform
.
universal
.
constant
.
enumerate
;
import
lombok.AllArgsConstructor
;
import
lombok.NoArgsConstructor
;
@AllArgsConstructor
@NoArgsConstructor
public
enum
FileTypeEnum
{
FILE_TYPE_ZIP
(
"application/zip"
,
".zip"
),
FILE_TYPE_RAR
(
"application/octet-stream"
,
".rar"
);
public
String
type
;
public
String
fileStufix
;
public
static
String
getFileStufix
(
String
type
)
{
for
(
FileTypeEnum
orderTypeEnum
:
FileTypeEnum
.
values
())
{
if
(
orderTypeEnum
.
type
.
equals
(
type
))
{
return
orderTypeEnum
.
fileStufix
;
}
}
return
null
;
}
}
xx-universal/xx-universal-api/src/main/java/com/xxfc/platform/universal/utils/UnPackeUtil.java
0 → 100644
View file @
958531b8
package
com
.
xxfc
.
platform
.
universal
.
utils
;
import
com.github.junrar.Archive
;
import
com.github.junrar.rarfile.FileHeader
;
import
lombok.extern.slf4j.Slf4j
;
import
net.lingala.zip4j.core.ZipFile
;
import
java.io.*
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipInputStream
;
/**
* 解压rar/zip工具类
*/
@Slf4j
public
class
UnPackeUtil
{
/**
* zip文件解压
*
* @param destPath 解压文件路径
* @param zipFile 压缩文件
* @param password 解压密码(如果有)
*/
public
static
void
unPackZip
(
File
zipFile
,
String
password
,
String
destPath
)
{
try
{
ZipFile
zip
=
new
ZipFile
(
zipFile
);
/*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/
zip
.
setFileNameCharset
(
"GBK"
);
log
.
info
(
"begin unpack zip file...."
);
zip
.
extractAll
(
destPath
);
// 如果解压需要密码
if
(
zip
.
isEncrypted
())
{
zip
.
setPassword
(
password
);
}
}
catch
(
Exception
e
)
{
log
.
error
(
"unPack zip file to "
+
destPath
+
" fail ...."
,
e
.
getMessage
(),
e
);
}
}
/**
* rar文件解压(不支持有密码的压缩包)
*
* @param rarFile rar压缩包
* @param destPath 解压保存路径
*/
public
static
void
unPackRar
(
File
rarFile
,
String
destPath
)
{
try
(
Archive
archive
=
new
Archive
(
rarFile
))
{
if
(
null
!=
archive
)
{
FileHeader
fileHeader
=
archive
.
nextFileHeader
();
File
file
=
null
;
while
(
null
!=
fileHeader
)
{
// 防止文件名中文乱码问题的处理
String
fileName
=
fileHeader
.
getFileNameW
().
isEmpty
()
?
fileHeader
.
getFileNameString
()
:
fileHeader
.
getFileNameW
();
if
(
fileHeader
.
isDirectory
())
{
//是文件夹
file
=
new
File
(
destPath
+
File
.
separator
+
fileName
);
file
.
mkdirs
();
}
else
{
//不是文件夹
file
=
new
File
(
destPath
+
File
.
separator
+
fileName
.
trim
());
if
(!
file
.
exists
())
{
if
(!
file
.
getParentFile
().
exists
())
{
// 相对路径可能多级,可能需要创建父目录.
file
.
getParentFile
().
mkdirs
();
}
file
.
createNewFile
();
}
FileOutputStream
os
=
new
FileOutputStream
(
file
);
archive
.
extractFile
(
fileHeader
,
os
);
os
.
close
();
}
fileHeader
=
archive
.
nextFileHeader
();
}
}
}
catch
(
Exception
e
)
{
log
.
error
(
"unpack rar file fail...."
,
e
.
getMessage
(),
e
);
}
}
}
\ No newline at end of file
xx-universal/xx-universal-server/src/main/java/com/xxfc/platform/universal/config/CommonConfig.java
View file @
958531b8
...
@@ -13,9 +13,9 @@ public class CommonConfig {
...
@@ -13,9 +13,9 @@ public class CommonConfig {
public
MultipartConfigElement
multipartConfigElement
()
{
public
MultipartConfigElement
multipartConfigElement
()
{
MultipartConfigFactory
factory
=
new
MultipartConfigFactory
();
MultipartConfigFactory
factory
=
new
MultipartConfigFactory
();
// 单个数据大小
// 单个数据大小
factory
.
setMaxFileSize
(
"10240KB"
);
// KB,MB
factory
.
setMaxFileSize
(
"10240
0
KB"
);
// KB,MB
/// 总上传数据大小
/// 总上传数据大小
factory
.
setMaxRequestSize
(
"
1
02400KB"
);
factory
.
setMaxRequestSize
(
"
5
02400KB"
);
return
factory
.
createMultipartConfig
();
return
factory
.
createMultipartConfig
();
}
}
}
}
\ No newline at end of file
xx-universal/xx-universal-server/src/main/java/com/xxfc/platform/universal/controller/UploadController.java
View file @
958531b8
...
@@ -2,9 +2,11 @@ package com.xxfc.platform.universal.controller;
...
@@ -2,9 +2,11 @@ package com.xxfc.platform.universal.controller;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken
;
import
com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken
;
import
com.github.wxiaoqi.security.common.msg.ObjectRestResponse
;
import
com.github.wxiaoqi.security.common.util.result.JsonResultUtil
;
import
com.github.wxiaoqi.security.common.util.result.JsonResultUtil
;
import
com.xxfc.platform.universal.dto.ImgDTO
;
import
com.xxfc.platform.universal.dto.ImgDTO
;
import
com.xxfc.platform.universal.dto.UploadImgDTO
;
import
com.xxfc.platform.universal.dto.UploadImgDTO
;
import
com.xxfc.platform.universal.service.FileUploadService
;
import
com.xxfc.platform.universal.service.UploadService
;
import
com.xxfc.platform.universal.service.UploadService
;
import
com.xxfc.platform.universal.utils.ImgBase64Util
;
import
com.xxfc.platform.universal.utils.ImgBase64Util
;
import
com.xxfc.platform.universal.utils.PublicMsg
;
import
com.xxfc.platform.universal.utils.PublicMsg
;
...
@@ -38,6 +40,8 @@ public class UploadController{
...
@@ -38,6 +40,8 @@ public class UploadController{
@Autowired
@Autowired
UploadService
uploadService
;
UploadService
uploadService
;
@Autowired
FileUploadService
fileUploadService
;
private
static
Integer
MAX_DRIVING_LICENSE_SIZE
=
10
*
1024
*
1024
;
//10M
private
static
Integer
MAX_DRIVING_LICENSE_SIZE
=
10
*
1024
*
1024
;
//10M
...
@@ -158,5 +162,12 @@ public class UploadController{
...
@@ -158,5 +162,12 @@ public class UploadController{
ueditor
.
setTitle
(
upfile
.
getOriginalFilename
());
ueditor
.
setTitle
(
upfile
.
getOriginalFilename
());
return
ueditor
;
return
ueditor
;
}
}
@RequestMapping
(
value
=
"/app/unauth/zip"
,
method
=
RequestMethod
.
POST
)
public
ObjectRestResponse
zip
(
@RequestParam
(
"file"
)
MultipartFile
upfile
,
@RequestParam
(
value
=
"prefix"
,
defaultValue
=
"renovate"
)
String
prefix
)
throws
Exception
{
return
fileUploadService
.
handlerUpload
(
upfile
,
null
,
prefix
);
}
}
}
xx-universal/xx-universal-server/src/main/java/com/xxfc/platform/universal/service/FileUploadService.java
0 → 100644
View file @
958531b8
package
com
.
xxfc
.
platform
.
universal
.
service
;
import
com.github.wxiaoqi.security.common.msg.ObjectRestResponse
;
import
org.springframework.web.multipart.MultipartFile
;
public
interface
FileUploadService
{
public
ObjectRestResponse
handlerUpload
(
MultipartFile
zipFile
,
String
password
,
String
prefix
)
throws
Exception
;
}
xx-universal/xx-universal-server/src/main/java/com/xxfc/platform/universal/service/UploadService.java
View file @
958531b8
...
@@ -23,7 +23,7 @@ import java.util.UUID;
...
@@ -23,7 +23,7 @@ import java.util.UUID;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
@Service
@Service
public
class
UploadService
{
public
class
UploadService
{
public
static
final
DateTimeFormatter
DEFAULT_DATE_TIME_FORMATTER
=
DateTimeFormat
.
forPattern
(
"yyyy-MM-dd"
);
public
static
final
DateTimeFormatter
DEFAULT_DATE_TIME_FORMATTER
=
DateTimeFormat
.
forPattern
(
"yyyy-MM-dd"
);
@Value
(
"${universal.baseUploadPath}"
)
@Value
(
"${universal.baseUploadPath}"
)
...
...
xx-universal/xx-universal-server/src/main/java/com/xxfc/platform/universal/service/impl/FileUploadServiceImpl.java
0 → 100644
View file @
958531b8
package
com
.
xxfc
.
platform
.
universal
.
service
.
impl
;
import
com.github.wxiaoqi.security.common.msg.ObjectRestResponse
;
import
com.github.wxiaoqi.security.common.util.process.ResultCode
;
import
com.xxfc.platform.universal.constant.enumerate.FileTypeEnum
;
import
com.xxfc.platform.universal.service.FileUploadService
;
import
com.xxfc.platform.universal.utils.UnPackeUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.*
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipFile
;
import
java.util.zip.ZipInputStream
;
@Service
@Slf4j
public
class
FileUploadServiceImpl
implements
FileUploadService
{
@Value
(
"${universal.uploadPath}"
)
private
String
uploadPath
;
@Value
(
"${universal.url}"
)
private
String
xx_url
;
@Override
public
ObjectRestResponse
handlerUpload
(
MultipartFile
zipFile
,
String
password
,
String
prefix
)
throws
Exception
{
if
(
null
==
zipFile
)
{
return
ObjectRestResponse
.
createFailedResult
(
ResultCode
.
FAILED_CODE
,
"请上传压缩文件!"
);
}
boolean
isZipPack
=
true
;
String
fileContentType
=
zipFile
.
getContentType
();
//将压缩包保存在指定路径
String
packFilePath
=
uploadPath
+
File
.
separator
+
zipFile
.
getName
();
if
(
FileTypeEnum
.
FILE_TYPE_ZIP
.
type
.
equals
(
fileContentType
))
{
//zip解压缩处理
packFilePath
+=
FileTypeEnum
.
FILE_TYPE_ZIP
.
fileStufix
;
}
else
if
(
FileTypeEnum
.
FILE_TYPE_RAR
.
type
.
equals
(
fileContentType
))
{
//rar解压缩处理
packFilePath
+=
FileTypeEnum
.
FILE_TYPE_RAR
.
fileStufix
;
isZipPack
=
false
;
}
else
{
return
ObjectRestResponse
.
createFailedResult
(
ResultCode
.
FAILED_CODE
,
"上传的压缩包格式不正确,仅支持rar和zip压缩文件!"
);
}
File
file
=
new
File
(
packFilePath
);
try
{
zipFile
.
transferTo
(
file
);
}
catch
(
IOException
e
)
{
log
.
error
(
"zip file save to "
+
uploadPath
+
" error"
,
e
.
getMessage
(),
e
);
return
ObjectRestResponse
.
createFailedResult
(
ResultCode
.
FAILED_CODE
,
"保存压缩文件到:"
+
uploadPath
+
" 失败!"
);
}
if
(
isZipPack
)
{
//zip压缩包
UnPackeUtil
.
unPackZip
(
file
,
password
,
uploadPath
);
}
else
{
//rar压缩包
UnPackeUtil
.
unPackRar
(
file
,
uploadPath
);
}
String
path
=
readZipFile
(
packFilePath
);
if
(
StringUtils
.
isNotBlank
(
path
)){
path
=
xx_url
+
"/"
+
prefix
+
"/"
+
path
;
return
ObjectRestResponse
.
succ
(
path
);
}
else
{
return
ObjectRestResponse
.
createDefaultFail
();
}
}
public
static
String
readZipFile
(
String
file
)
throws
Exception
{
ZipFile
zf
=
new
ZipFile
(
file
);
InputStream
in
=
new
BufferedInputStream
(
new
FileInputStream
(
file
));
ZipInputStream
zin
=
new
ZipInputStream
(
in
);
ZipEntry
ze
;
String
path
=
null
;
while
((
ze
=
zin
.
getNextEntry
())
!=
null
)
{
if
(
ze
.
isDirectory
())
{
}
else
{
path
=
ze
.
getName
();
}
}
zin
.
closeEntry
();
return
path
;
}
}
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