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
71057ec7
Commit
71057ec7
authored
May 08, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Excel导出功能
parent
88aafb9b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
510 additions
and
0 deletions
+510
-0
RandomHelper.java
...c/main/java/com/yanzuoguang/util/helper/RandomHelper.java
+115
-0
HttpUploadHelper.java
...n/java/com/yanzuoguang/cloud/helper/HttpUploadHelper.java
+110
-0
WebFileHelper.java
...main/java/com/yanzuoguang/cloud/helper/WebFileHelper.java
+226
-0
WebFileVo.java
...oud/src/main/java/com/yanzuoguang/cloud/vo/WebFileVo.java
+59
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/RandomHelper.java
0 → 100755
View file @
71057ec7
package
com
.
yanzuoguang
.
util
.
helper
;
import
com.yanzuoguang.util.contants.SystemContants
;
import
java.util.UUID
;
/**
* 随机码生成工具
*/
public
final
class
RandomHelper
{
public
static
final
String
NUMBER
=
"0123456789"
;
public
static
final
String
LOWER_LETTER
=
"abcdefghijklmnopqrstuvwxyz"
;
public
static
final
String
UPPER_LETTER
=
LOWER_LETTER
.
toUpperCase
();
private
static
final
String
RANDOM_CHARSET
=
NUMBER
+
LOWER_LETTER
+
UPPER_LETTER
;
private
static
final
int
RANDOM_CHARSET_LEN
=
RANDOM_CHARSET
.
length
();
/**
* 得到32位随机码
*
* @return
*/
public
static
String
generateUUID32
()
{
return
replace
(
UUID
.
randomUUID
().
toString
(),
SystemContants
.
TRANSVERSE
,
SystemContants
.
EMPTY
);
}
/**
* 得到64位随机码
*
* @return
*/
public
static
String
generateUUID64
()
{
return
generateUUID32
()
+
generateUUID32
();
}
/**
* 随机码(6位)
*
* @return
*/
public
static
String
generateRandomCodeSix
()
{
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
6
;
i
++)
{
sb
.
append
(
generateRandomCodeOne
(
RANDOM_CHARSET
,
RANDOM_CHARSET_LEN
));
}
return
sb
.
toString
();
}
/**
* 随机码(指定长度)
*
* @param len 随机码长度
* @return
*/
public
static
String
generateRandomCodeSix
(
int
len
)
{
if
(
len
<
1
)
{
throw
new
RuntimeException
(
"length params is error, must be greater than 0, this params is ["
+
len
+
"]"
);
}
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
sb
.
append
(
generateRandomCodeOne
(
RANDOM_CHARSET
,
RANDOM_CHARSET_LEN
));
}
return
sb
.
toString
();
}
/**
* 随机生成指定字符集、指定长度的的随机码
*
* @param charset 基础字符集
* @param len 生成随机码长度
* @return
*/
public
static
String
generateRandomCode
(
String
charset
,
int
len
)
{
if
(
null
==
charset
||
""
.
equals
(
charset
))
{
throw
new
RuntimeException
(
"random code, essential data is empty."
);
}
if
(
len
<
1
)
{
throw
new
RuntimeException
(
"length params is error, must be greater than 0, this params is ["
+
len
+
"]"
);
}
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
sb
.
append
(
generateRandomCodeOne
(
charset
,
charset
.
length
()));
}
return
sb
.
toString
();
}
/**
* 指定字符集中,随机产生一个字符
*
* @param charset
* @param len
* @return
*/
private
static
String
generateRandomCodeOne
(
String
charset
,
int
len
)
{
int
index
=
(
int
)
(
Math
.
random
()
*
len
);
return
String
.
valueOf
(
charset
.
charAt
(
index
));
}
private
static
String
replace
(
String
str
,
String
olds
,
String
news
)
{
if
(
null
==
str
)
{
return
""
;
}
while
(
str
.
indexOf
(
olds
)
>
-
1
)
{
str
=
str
.
replace
(
olds
,
news
);
}
return
str
;
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/helper/HttpUploadHelper.java
0 → 100755
View file @
71057ec7
package
com
.
yanzuoguang
.
cloud
.
helper
;
import
com.alibaba.fastjson.JSON
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.ClientProtocolException
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.mime.MultipartEntityBuilder
;
import
org.apache.http.entity.mime.content.FileBody
;
import
org.apache.http.entity.mime.content.StringBody
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.io.File
;
import
java.io.IOException
;
/**
* HTTP客户端上传文件
* @author 颜佐光
*/
public
final
class
HttpUploadHelper
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
HttpUploadHelper
.
class
);
public
static
final
String
tagName
=
"file"
;
public
static
final
int
CONNECT_TIME_OUT
=
200000
;
public
static
final
int
SOCKET_TIME_OUT
=
200000
;
/**
* 上传文件
*
* @param url 上传的文件
* @param filePath 上传的路径
* @return
*/
public
static
String
upload
(
final
String
url
,
final
String
filePath
)
{
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
try
{
HttpPost
httpPost
=
initHttpPost
(
url
);
httpPost
.
setEntity
(
initRequestEntity
(
filePath
));
logger
.
debug
(
"executing request: {}"
,
httpPost
.
getRequestLine
());
CloseableHttpResponse
closeableHttpResponse
=
httpclient
.
execute
(
httpPost
);
HttpEntity
responseEntity
=
closeableHttpResponse
.
getEntity
();
if
(
responseEntity
!=
null
)
{
String
result
=
EntityUtils
.
toString
(
closeableHttpResponse
.
getEntity
());
logger
.
debug
(
"response result: {}"
,
result
);
return
result
;
}
EntityUtils
.
consume
(
responseEntity
);
closeableHttpResponse
.
close
();
return
JSON
.
toJSONString
(
initResponseError
());
}
catch
(
ClientProtocolException
e
)
{
logger
.
error
(
"exception ClientProtocolException."
,
e
);
throw
new
RuntimeException
(
e
);
}
catch
(
IOException
e
)
{
logger
.
error
(
"exception IOException."
,
e
);
throw
new
RuntimeException
(
e
);
}
finally
{
try
{
httpclient
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"close IO exception IOException."
,
e
);
throw
new
RuntimeException
(
e
);
}
}
}
/**
* 初始化HttpPost
*
* @param url
* @return
*/
private
static
HttpPost
initHttpPost
(
String
url
)
{
HttpPost
httpPost
=
new
HttpPost
(
url
);
RequestConfig
requestConfig
=
RequestConfig
.
custom
().
setConnectTimeout
(
CONNECT_TIME_OUT
).
setSocketTimeout
(
SOCKET_TIME_OUT
).
build
();
httpPost
.
setConfig
(
requestConfig
);
return
httpPost
;
}
/**
* 初始化请求HttpEntity
*
* @param filePath
* @return
*/
private
static
HttpEntity
initRequestEntity
(
String
filePath
)
{
FileBody
fileBody
=
new
FileBody
(
new
File
(
filePath
));
StringBody
stringBody
=
new
StringBody
(
"This is comment"
,
ContentType
.
TEXT_PLAIN
);
return
MultipartEntityBuilder
.
create
().
addPart
(
tagName
,
fileBody
).
addPart
(
"comment"
,
stringBody
).
build
();
}
/**
* 初始化返回错误信息
*
* @return
*/
private
static
ResponseResult
initResponseError
()
{
return
ResponseResult
.
error
(
"01"
,
"上传文件失败"
);
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/helper/WebFileHelper.java
0 → 100755
View file @
71057ec7
package
com
.
yanzuoguang
.
cloud
.
helper
;
import
com.yanzuoguang.cloud.vo.WebFileVo
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.core.io.FileSystemResource
;
import
org.springframework.core.io.InputStreamResource
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLEncoder
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.UUID
;
/**
* 上传文件,下载文件操作类
*/
public
final
class
WebFileHelper
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WebFileHelper
.
class
);
private
static
final
String
CACHE_CONTROL_VALUE
=
"no-cache, no-store, must-revalidate"
;
private
static
final
String
CONTENT_DISPOSITION_VALUE
=
"attachment; filename=\"%s\""
;
private
static
final
String
PRAGMA_VALUE
=
"no-cache"
;
private
static
final
String
EXPIRES_VALUE
=
"0"
;
private
static
final
String
SEPARATE_SPOT
=
"."
;
private
static
final
String
SEPARATE_HR
=
"-"
;
private
static
final
String
UTF_8
=
"UTF-8"
;
private
static
WebFileVo
webFileVo
;
private
WebFileHelper
()
{
super
();
}
/**
* 检查并创建文件夹
*
* @param path
*/
public
static
boolean
createDirectory
(
String
path
)
{
File
file
=
new
File
(
path
);
if
(!
file
.
isDirectory
())
{
return
file
.
mkdirs
();
}
return
false
;
}
/**
* 下载文件,下载文件名为服务器保存文件名
*
* @param path 文件路径
* @return
*/
public
static
ResponseEntity
download
(
String
path
)
{
try
{
FileSystemResource
file
=
new
FileSystemResource
(
path
);
HttpHeaders
httpHeaders
=
buildHttpHeaders
(
file
.
getFilename
());
return
buildResponseEntity
(
httpHeaders
,
file
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
/**
* 下载文件,指定下载文件后文件名
*
* @param path 下载路径
* @param name 重命名
* @return
*/
public
static
ResponseEntity
download
(
String
path
,
String
name
)
{
try
{
FileSystemResource
file
=
new
FileSystemResource
(
path
);
HttpHeaders
httpHeaders
=
buildHttpHeaders
(
name
);
return
buildResponseEntity
(
httpHeaders
,
file
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
/**
* 设置下载headers
*
* @param name
* @return
*/
private
static
HttpHeaders
buildHttpHeaders
(
String
name
)
throws
UnsupportedEncodingException
{
HttpHeaders
headers
=
new
HttpHeaders
();
headers
.
add
(
HttpHeaders
.
CACHE_CONTROL
,
CACHE_CONTROL_VALUE
);
String
encodeName
=
URLEncoder
.
encode
(
name
,
UTF_8
);
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"download taskDefinition name, and before encode taskDefinition name [{}], and after taskDefinition name[{}]"
,
name
,
encodeName
);
}
headers
.
add
(
HttpHeaders
.
CONTENT_DISPOSITION
,
String
.
format
(
CONTENT_DISPOSITION_VALUE
,
encodeName
));
headers
.
add
(
HttpHeaders
.
PRAGMA
,
PRAGMA_VALUE
);
headers
.
add
(
HttpHeaders
.
EXPIRES
,
EXPIRES_VALUE
);
return
headers
;
}
/**
* 下载设置返回值
*
* @param httpHeaders
* @param file
* @return
* @throws IOException
*/
private
static
ResponseEntity
buildResponseEntity
(
HttpHeaders
httpHeaders
,
FileSystemResource
file
)
throws
IOException
{
return
ResponseEntity
.
ok
()
.
headers
(
httpHeaders
)
.
contentLength
(
file
.
contentLength
())
.
contentType
(
MediaType
.
parseMediaType
(
MediaType
.
APPLICATION_OCTET_STREAM_VALUE
))
.
body
(
new
InputStreamResource
(
file
.
getInputStream
()));
}
/**
* 单文件上传
*
* @param multipartFile
* @param savePath
* @return
*/
public
static
WebFileVo
upload
(
MultipartFile
multipartFile
,
String
savePath
)
{
String
fileName
=
multipartFile
.
getOriginalFilename
();
if
(!
savePath
.
endsWith
(
"/"
)
||
!
savePath
.
endsWith
(
"\\"
))
{
savePath
+=
"/"
;
}
webFileVo
=
new
WebFileVo
();
webFileVo
.
setOldName
(
fileName
);
try
{
String
suffixName
=
suffix
(
fileName
);
String
uuid
=
generateUUID
();
String
newPath
=
rename
(
uuid
,
suffixName
,
savePath
);
webFileVo
.
setNewName
(
uuid
+
suffixName
);
File
newFile
=
new
File
(
newPath
);
createDirectory
(
savePath
);
multipartFile
.
transferTo
(
newFile
);
webFileVo
.
setPath
(
newPath
);
return
webFileVo
;
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
/**
* 多文件上传
*
* @param tagName 页面控件名称
* @return
*/
public
static
List
<
WebFileVo
>
upload
(
HttpServletRequest
request
,
String
tagName
,
String
savePath
)
{
List
<
WebFileVo
>
list
=
new
ArrayList
<>();
List
<
MultipartFile
>
files
=
((
MultipartHttpServletRequest
)
request
).
getFiles
(
tagName
);
if
(
null
!=
files
&&
!
files
.
isEmpty
())
{
for
(
MultipartFile
multipartFile
:
files
)
{
WebFileVo
tempBean
=
upload
(
multipartFile
,
savePath
);
list
.
add
(
tempBean
);
}
}
return
list
;
}
/**
* 上传文件重命名
*
* @param suffixName
* @param savePath
* @return
*/
private
static
String
rename
(
String
uuid
,
String
suffixName
,
String
savePath
)
{
return
savePath
+
uuid
+
suffixName
;
}
/**
* 获取UUID
*
* @return
*/
private
static
String
generateUUID
()
{
String
str
=
String
.
valueOf
(
UUID
.
randomUUID
());
while
(
str
.
indexOf
(
SEPARATE_HR
)
>=
0
)
{
str
=
str
.
replace
(
SEPARATE_HR
,
""
);
}
return
str
;
}
/**
* 默认获取后缀
*
* @param fileName
* @return
*/
private
static
String
suffix
(
String
fileName
)
{
return
fileName
.
substring
(
fileName
.
lastIndexOf
(
SEPARATE_SPOT
));
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/vo/WebFileVo.java
0 → 100755
View file @
71057ec7
package
com
.
yanzuoguang
.
cloud
.
vo
;
import
com.yanzuoguang.util.vo.BaseVo
;
/**
* 上传文件返回实体
* @author 阿姨中诺广
*/
public
class
WebFileVo
extends
BaseVo
{
/**
* 上传前文件名称
*/
private
String
oldName
;
/**
* 上传后文件名称
*/
private
String
newName
;
/**
* 文件保存路径
*/
private
String
path
;
/**
* nginx请求地址
*/
private
String
nginxUrl
;
public
String
getOldName
()
{
return
oldName
;
}
public
void
setOldName
(
String
oldName
)
{
this
.
oldName
=
oldName
;
}
public
String
getNewName
()
{
return
newName
;
}
public
void
setNewName
(
String
newName
)
{
this
.
newName
=
newName
;
}
public
String
getPath
()
{
return
path
;
}
public
void
setPath
(
String
path
)
{
this
.
path
=
path
;
}
public
String
getNginxUrl
()
{
return
nginxUrl
;
}
public
void
setNginxUrl
(
String
nginxUrl
)
{
this
.
nginxUrl
=
nginxUrl
;
}
}
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