Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
8
8timerapiv200
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
8timerv2
8timerapiv200
Commits
4d7dbfd6
Commit
4d7dbfd6
authored
Dec 07, 2021
by
翁国栋
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
禅道id--
395、在合同证照当中添加一个导出合同证照的功能
parent
d21ab452
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
865 additions
and
722 deletions
+865
-722
src/main/java/cn/timer/api/controller/htzz/HtzzController.java
+315
-237
src/main/java/cn/timer/api/utils/aliyun/OSSUtil.java
+550
-485
No files found.
src/main/java/cn/timer/api/controller/htzz/HtzzController.java
View file @
4d7dbfd6
package
cn
.
timer
.
api
.
controller
.
htzz
;
import
java.
util.ArrayList
;
import
java.
util.Date
;
import
java.util.
List
;
import
java.
io.*
;
import
java.
nio.charset.StandardCharsets
;
import
java.util.
*
;
import
cn.timer.api.utils.aliyun.OSSUtil
;
import
com.google.common.base.Strings
;
import
com.google.common.collect.Maps
;
import
io.swagger.models.auth.In
;
import
org.bouncycastle.util.encoders.UrlBase64Encoder
;
import
org.eclipse.jetty.util.UrlEncoded
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -26,7 +32,6 @@ import com.google.common.collect.Lists;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.cron.CronUtil
;
import
cn.hutool.cron.task.Task
;
import
cn.timer.api.bean.admin.AdminAssoTxjlb
;
import
cn.timer.api.bean.htzz.HtzzAdminZzda
;
import
cn.timer.api.bean.htzz.HtzzAssoHtgx
;
import
cn.timer.api.bean.htzz.HtzzAssoZztx
;
...
...
@@ -37,248 +42,321 @@ import cn.timer.api.config.annotation.UserBean;
import
cn.timer.api.dao.htzz.HtzzAdminZzdaMapper
;
import
cn.timer.api.dto.htzz.HtzzQueryDto
;
import
cn.timer.api.dto.htzz.NotifyPersonDto
;
import
cn.timer.api.dto.htzz.QueryNoReadingDto
;
import
cn.timer.api.utils.Result
;
import
cn.timer.api.utils.ResultUtil
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
@Api
(
tags
=
"7.0合同证照"
)
@RestController
@Transactional
@RequestMapping
(
value
=
"/htzz"
,
produces
=
{
"application/json"
})
@RequestMapping
(
value
=
"/htzz"
,
produces
=
{
"application/json"
})
public
class
HtzzController
{
@Autowired
private
HtzzAdminZzdaMapper
htzzAdminZzdaMapper
;
/**
* 新增证件
*
* @param userBean
* @param htzzAdminZzda 证照档案实体
* @return 新增证件结果
*/
@PostMapping
(
"/addzj"
)
@ApiOperation
(
value
=
"新增合同证件"
,
httpMethod
=
"POST"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
addzj
(
@CurrentUser
UserBean
userBean
,
@RequestBody
HtzzAdminZzda
zzda
)
{
Integer
orgCode
=
userBean
.
getOrgCode
();
QyzxRemainingQuantity
quantity
=
QyzxRemainingQuantity
.
builder
().
build
().
selectOne
(
new
LambdaQueryWrapper
<
QyzxRemainingQuantity
>().
eq
(
QyzxRemainingQuantity:
:
getOrgCode
,
orgCode
)
.
eq
(
QyzxRemainingQuantity:
:
getPmid
,
1
).
gt
(
QyzxRemainingQuantity:
:
getRemainder
,
0
));
Date
date
=
null
;
Integer
remainder
=
null
;
if
(
quantity
!=
null
)
{
remainder
=
quantity
.
getRemainder
();
date
=
quantity
.
getExpireDate
();
}
if
(
quantity
==
null
||
remainder
==
null
||
remainder
<=
0
)
{
// 判断充了钱没
return
ResultUtil
.
error
(
"请充值后使用短信功能!"
);
}
if
(
date
!=
null
&&
date
.
getTime
()
<=
new
Date
().
getTime
())
{
Logoutput
(
"企业 "
+
orgCode
+
" :短信套餐已过期"
);
return
ResultUtil
.
error
(
"短信套餐已到期!"
);
}
if
(
zzda
.
getId
()
==
null
)
{
zzda
.
setTxkgType
(
1
);
//默认开启提醒
}
zzda
.
setLrrid
(
userBean
.
getEmpNum
());
zzda
.
setLrsjTime
(
new
Date
());
zzda
.
setOrgCode
(
orgCode
);
zzda
.
setSendingStatus
(
0
);
//0:未发;1:已发
zzda
.
setTemplateId
(
zzda
.
getTemplateId
());
//消息模板id
@Autowired
private
HtzzAdminZzdaMapper
htzzAdminZzdaMapper
;
@Autowired
private
OSSUtil
oss
;
@Value
(
"${config-8timer.Aliyun.project_package}"
)
private
String
project_package
;
@Value
(
"${zip.path}"
)
private
String
zipPath
;
/**
* 新增证件
*
* @param userBean
* @param htzzAdminZzda 证照档案实体
* @return 新增证件结果
*/
@PostMapping
(
"/addzj"
)
@ApiOperation
(
value
=
"新增合同证件"
,
httpMethod
=
"POST"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
addzj
(
@CurrentUser
UserBean
userBean
,
@RequestBody
HtzzAdminZzda
zzda
)
{
Integer
orgCode
=
userBean
.
getOrgCode
();
QyzxRemainingQuantity
quantity
=
QyzxRemainingQuantity
.
builder
().
build
().
selectOne
(
new
LambdaQueryWrapper
<
QyzxRemainingQuantity
>().
eq
(
QyzxRemainingQuantity:
:
getOrgCode
,
orgCode
)
.
eq
(
QyzxRemainingQuantity:
:
getPmid
,
1
).
gt
(
QyzxRemainingQuantity:
:
getRemainder
,
0
));
Date
date
=
null
;
Integer
remainder
=
null
;
if
(
quantity
!=
null
)
{
remainder
=
quantity
.
getRemainder
();
date
=
quantity
.
getExpireDate
();
}
if
(
quantity
==
null
||
remainder
==
null
||
remainder
<=
0
)
{
// 判断充了钱没
return
ResultUtil
.
error
(
"请充值后使用短信功能!"
);
}
if
(
date
!=
null
&&
date
.
getTime
()
<=
new
Date
().
getTime
())
{
Logoutput
(
"企业 "
+
orgCode
+
" :短信套餐已过期"
);
// return ResultUtil.error("短信套餐已到期!");
}
if
(
zzda
.
getId
()
==
null
)
{
zzda
.
setTxkgType
(
1
);
//默认开启提醒
}
zzda
.
setLrrid
(
userBean
.
getEmpNum
());
zzda
.
setLrsjTime
(
new
Date
());
zzda
.
setOrgCode
(
orgCode
);
zzda
.
setSendingStatus
(
0
);
//0:未发;1:已发
zzda
.
setTemplateId
(
zzda
.
getTemplateId
());
//消息模板id
// zzda.insert();
zzda
.
insertOrUpdate
();
/**
* 新增通知关系表,每人一个
*/
List
<
NotifyPersonDto
>
ids
=
zzda
.
getIds
();
//通知人 用户id+手机号码
List
<
HtzzAssoHtgx
>
htgxs
=
Lists
.
newArrayList
();
for
(
NotifyPersonDto
pre
:
ids
)
{
HtzzAssoHtgx
.
builder
().
build
().
delete
(
new
QueryWrapper
<
HtzzAssoHtgx
>().
lambda
().
eq
(
HtzzAssoHtgx:
:
getOrgCode
,
orgCode
).
eq
(
HtzzAssoHtgx:
:
getTzrid
,
pre
.
getEmpNum
())
.
eq
(
HtzzAssoHtgx:
:
getPhone
,
pre
.
getPhone
()));
HtzzAssoHtgx
htgx
=
HtzzAssoHtgx
.
builder
().
build
();
htgx
.
setHtid
(
zzda
.
getId
());
htgx
.
setTzrid
(
pre
.
getEmpNum
());
htgx
.
setName
(
pre
.
getEmpName
());
htgx
.
setPhone
(
pre
.
getPhone
());
htgx
.
setOrgCode
(
orgCode
);
htgx
.
insert
();
htgxs
.
add
(
htgx
);
}
HtzzAssoZztx
zztx
=
zzda
.
getZztx
();
zztx
.
setZzdaId
(
zzda
.
getId
());
zztx
.
setOrgCode
(
orgCode
);
zztx
.
setLrrid
(
userBean
.
getEmpNum
());
zztx
.
setLrsjTime
(
new
Date
());
zztx
.
insertOrUpdate
();
return
ResultUtil
.
datas
(
zzda
,
htgxs
,
"新增证件成功!"
);
}
@GetMapping
(
"/modifytx/{id}"
)
@ApiOperation
(
value
=
"修改提醒数据"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
modifytx
(
@CurrentUser
UserBean
userBean
,
@PathVariable
Integer
id
)
{
List
<
NotifyPersonDto
>
tzids
=
new
ArrayList
<
NotifyPersonDto
>();
List
<
HtzzAssoHtgx
>
httxs
=
HtzzAssoHtgx
.
builder
().
build
().
selectList
(
new
QueryWrapper
<
HtzzAssoHtgx
>().
lambda
().
eq
(
HtzzAssoHtgx:
:
getHtid
,
id
));
for
(
HtzzAssoHtgx
gx
:
httxs
)
{
NotifyPersonDto
predto
=
NotifyPersonDto
.
builder
().
build
();
predto
.
setEmpName
(
gx
.
getName
());
predto
.
setEmpNum
(
gx
.
getTzrid
());
predto
.
setPhone
(
gx
.
getPhone
());
tzids
.
add
(
predto
);
}
HtzzAdminZzda
zzda
=
HtzzAdminZzda
.
builder
().
id
(
id
).
build
().
selectById
();
HtzzAssoZztx
zztx
=
HtzzAssoZztx
.
builder
().
build
().
selectOne
(
new
QueryWrapper
<
HtzzAssoZztx
>().
lambda
().
eq
(
HtzzAssoZztx:
:
getZzdaId
,
id
));
zzda
.
setZztx
(
zztx
);
zzda
.
setIds
(
tzids
);
return
ResultUtil
.
data
(
zzda
,
"新增成功"
);
}
/**
* 按条件搜索(关键字、到期时间)
*
* @param userBean
* @return
*/
@GetMapping
(
"/queryht"
)
@ApiOperation
(
value
=
"按条件搜索(关键字、到期时间)"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
queryht
(
@CurrentUser
UserBean
userBean
,
HtzzQueryDto
htzzQueryDto
)
{
String
e
=
htzzQueryDto
.
getEndTime
();
String
q
=
htzzQueryDto
.
getQuery
();
Page
<
HtzzAdminZzda
>
page
=
new
Page
<
HtzzAdminZzda
>(
htzzQueryDto
.
getCurrentPage
()
==
null
?
1
:
htzzQueryDto
.
getCurrentPage
(),
htzzQueryDto
.
getTotalPage
()
==
null
?
10
:
htzzQueryDto
.
getTotalPage
());
QueryWrapper
<
HtzzAdminZzda
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
eq
(
"org_code"
,
userBean
.
getOrgCode
())
.
select
(
"id"
,
"zjmc"
,
"czry"
,
"fzjg"
,
"fzrq"
,
"yxdqr"
,
"txkg_type"
).
eq
(
"is_delete"
,
0
).
eq
(
"document_type"
,
htzzQueryDto
.
getDocumentType
())
.
gt
(!
StrUtil
.
hasBlank
(
e
),
"yxdqr"
,
!
StrUtil
.
hasBlank
(
e
)
?
e
:
"9999-01-01 00:00:00"
)
.
and
(!
StrUtil
.
hasBlank
(
q
),
wq
->
wq
.
like
(
"zjmc"
,
q
).
or
().
like
(
"czry"
,
q
)).
orderByDesc
(
"lrsj_time"
);
IPage
<
HtzzAdminZzda
>
zPage
=
HtzzAdminZzda
.
builder
().
build
().
selectPage
(
page
,
queryWrapper
);
List
<
HtzzAdminZzda
>
zccrkgls
=
zPage
.
getRecords
();
zPage
.
getCurrent
();
zPage
.
getPages
();
zPage
.
getTotal
();
zPage
.
getSize
();
return
ResultUtil
.
data
(
zPage
,
zccrkgls
,
"搜索成功"
);
}
/**
* 根据id查合同
*
* @param id
* @return
*/
@PostMapping
(
value
=
"/queryHt/{id}"
)
@ApiOperation
(
value
=
"根据id查合同"
,
httpMethod
=
"POST"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
queryHt
(
@CurrentUser
UserBean
userBean
,
@PathVariable
Integer
id
)
{
List
<
Object
>
list
=
new
ArrayList
<
Object
>();
list
.
add
(
HtzzAdminZzda
.
builder
().
build
().
selectOne
(
new
QueryWrapper
<
HtzzAdminZzda
>()
.
eq
(
"org_code"
,
userBean
.
getOrgCode
()).
eq
(
"is_delete"
,
0
).
eq
(
"id"
,
id
)));
QueryWrapper
<
HtzzAssoHtgx
>
q
=
new
QueryWrapper
<
HtzzAssoHtgx
>();
q
.
select
(
"id"
,
"name"
).
eq
(
"htid"
,
id
);
List
<
HtzzAssoHtgx
>
htgx
=
HtzzAssoHtgx
.
builder
().
build
().
selectList
(
q
);
for
(
HtzzAssoHtgx
gx
:
htgx
)
{
String
name
=
gx
.
getName
();
list
.
add
(
name
);
}
return
ResultUtil
.
data
(
list
);
}
/**
* 根据id删除合同
*
* @param id
* @return
*/
@DeleteMapping
(
value
=
"/delHt"
)
@ApiOperation
(
value
=
"根据id删除合同"
,
httpMethod
=
"DELETE"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
delHt
(
@CurrentUser
UserBean
userBean
,
@RequestParam
Integer
htid
)
{
// 删除提醒关系
HtzzAssoHtgx
.
builder
().
build
().
delete
(
new
QueryWrapper
<
HtzzAssoHtgx
>().
eq
(
"org_code"
,
userBean
.
getOrgCode
()).
eq
(
"htid"
,
htid
));
//删除证照提醒表配置
HtzzAssoZztx
.
builder
().
build
().
delete
(
new
QueryWrapper
<
HtzzAssoZztx
>().
lambda
().
eq
(
HtzzAssoZztx:
:
getZzdaId
,
htid
));
// 设置证照删除状态
return
ResultUtil
.
data
(
HtzzAdminZzda
.
builder
().
id
(
htid
).
isDelete
(
1
).
build
().
updateById
());
}
/**
* 修改提醒状态
*
* @param id
* @return
*/
@PostMapping
(
value
=
"/changeTx"
)
@ApiOperation
(
value
=
"修改提醒状态"
,
httpMethod
=
"POST"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
changeTx
(
@CurrentUser
UserBean
userBean
,
@RequestParam
Integer
htid
)
{
QueryWrapper
<
HtzzAdminZzda
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
select
(
"txkg_type"
).
eq
(
"id"
,
htid
);
HtzzAdminZzda
zzda
=
HtzzAdminZzda
.
builder
().
build
().
selectOne
(
queryWrapper
);
UpdateWrapper
<
HtzzAdminZzda
>
updateWrapper
=
new
UpdateWrapper
<>();
updateWrapper
.
eq
(
"id"
,
htid
);
HtzzAdminZzda
w
=
HtzzAdminZzda
.
builder
().
orgCode
(
userBean
.
getOrgCode
()).
id
(
htid
)
.
txkgType
(
zzda
.
getTxkgType
()
==
0
?
1
:
0
).
build
();
htzzAdminZzdaMapper
.
update
(
w
,
updateWrapper
);
return
ResultUtil
.
success
(
"修改成功"
);
}
/**
* 查詢全部員工id、姓名
*
* @param id
* @return
*/
@GetMapping
(
value
=
"/queryYg"
)
@ApiOperation
(
value
=
"查詢全部員工id、姓名"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
queryYg
(
@CurrentUser
UserBean
userBean
)
{
return
ResultUtil
.
data
(
YgglMainEmp
.
builder
().
build
().
selectList
(
new
QueryWrapper
<
YgglMainEmp
>().
select
(
"emp_num"
,
"name"
,
"phone"
).
eq
(
"org_code"
,
userBean
.
getOrgCode
())));
}
@GetMapping
(
value
=
"/test"
)
@ApiOperation
(
value
=
"test"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Void
>
test
(
String
schedulingPattern
)
{
CronUtil
.
schedule
(
"*/2 * * 24 * *"
,
new
Task
()
{
@Override
public
void
execute
()
{
Logoutput
(
"start:"
+
new
Date
());
}
});
CronUtil
.
setMatchSecond
(
true
);
CronUtil
.
start
();
return
ResultUtil
.
success
(
"操作成功"
);
}
@Value
(
"${config-8timer.environmental-science}"
)
public
String
environmental_science
;
public
void
Logoutput
(
String
science
)
{
if
(!(
"pro"
).
equals
(
environmental_science
))
{
System
.
out
.
println
(
science
);
}
else
{
System
.
out
.
println
(
""
);
}
}
zzda
.
insertOrUpdate
();
/**
* 新增通知关系表,每人一个
*/
List
<
NotifyPersonDto
>
ids
=
zzda
.
getIds
();
//通知人 用户id+手机号码
List
<
HtzzAssoHtgx
>
htgxs
=
Lists
.
newArrayList
();
for
(
NotifyPersonDto
pre
:
ids
)
{
HtzzAssoHtgx
.
builder
().
build
().
delete
(
new
QueryWrapper
<
HtzzAssoHtgx
>().
lambda
().
eq
(
HtzzAssoHtgx:
:
getOrgCode
,
orgCode
).
eq
(
HtzzAssoHtgx:
:
getTzrid
,
pre
.
getEmpNum
())
.
eq
(
HtzzAssoHtgx:
:
getPhone
,
pre
.
getPhone
()));
HtzzAssoHtgx
htgx
=
HtzzAssoHtgx
.
builder
().
build
();
htgx
.
setHtid
(
zzda
.
getId
());
htgx
.
setTzrid
(
pre
.
getEmpNum
());
htgx
.
setName
(
pre
.
getEmpName
());
htgx
.
setPhone
(
pre
.
getPhone
());
htgx
.
setOrgCode
(
orgCode
);
htgx
.
insert
();
htgxs
.
add
(
htgx
);
}
HtzzAssoZztx
zztx
=
zzda
.
getZztx
();
zztx
.
setZzdaId
(
zzda
.
getId
());
zztx
.
setOrgCode
(
orgCode
);
zztx
.
setLrrid
(
userBean
.
getEmpNum
());
zztx
.
setLrsjTime
(
new
Date
());
zztx
.
insertOrUpdate
();
return
ResultUtil
.
datas
(
zzda
,
htgxs
,
"新增证件成功!"
);
}
@GetMapping
(
"/modifytx/{id}"
)
@ApiOperation
(
value
=
"修改提醒数据"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
modifytx
(
@CurrentUser
UserBean
userBean
,
@PathVariable
Integer
id
)
{
List
<
NotifyPersonDto
>
tzids
=
new
ArrayList
<
NotifyPersonDto
>();
List
<
HtzzAssoHtgx
>
httxs
=
HtzzAssoHtgx
.
builder
().
build
().
selectList
(
new
QueryWrapper
<
HtzzAssoHtgx
>().
lambda
().
eq
(
HtzzAssoHtgx:
:
getHtid
,
id
));
for
(
HtzzAssoHtgx
gx
:
httxs
)
{
NotifyPersonDto
predto
=
NotifyPersonDto
.
builder
().
build
();
predto
.
setEmpName
(
gx
.
getName
());
predto
.
setEmpNum
(
gx
.
getTzrid
());
predto
.
setPhone
(
gx
.
getPhone
());
tzids
.
add
(
predto
);
}
HtzzAdminZzda
zzda
=
HtzzAdminZzda
.
builder
().
id
(
id
).
build
().
selectById
();
HtzzAssoZztx
zztx
=
HtzzAssoZztx
.
builder
().
build
().
selectOne
(
new
QueryWrapper
<
HtzzAssoZztx
>().
lambda
().
eq
(
HtzzAssoZztx:
:
getZzdaId
,
id
));
zzda
.
setZztx
(
zztx
);
zzda
.
setIds
(
tzids
);
return
ResultUtil
.
data
(
zzda
,
"新增成功"
);
}
/**
* 按条件搜索(关键字、到期时间)
*
* @param userBean
* @return
*/
@GetMapping
(
"/queryht"
)
@ApiOperation
(
value
=
"按条件搜索(关键字、到期时间)"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
queryht
(
@CurrentUser
UserBean
userBean
,
HtzzQueryDto
htzzQueryDto
)
{
String
e
=
htzzQueryDto
.
getEndTime
();
String
q
=
htzzQueryDto
.
getQuery
();
Page
<
HtzzAdminZzda
>
page
=
new
Page
<
HtzzAdminZzda
>(
htzzQueryDto
.
getCurrentPage
()
==
null
?
1
:
htzzQueryDto
.
getCurrentPage
(),
htzzQueryDto
.
getTotalPage
()
==
null
?
10
:
htzzQueryDto
.
getTotalPage
());
QueryWrapper
<
HtzzAdminZzda
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
eq
(
"org_code"
,
userBean
.
getOrgCode
())
.
select
(
"id"
,
"zjmc"
,
"czry"
,
"fzjg"
,
"fzrq"
,
"yxdqr"
,
"txkg_type"
).
eq
(
"is_delete"
,
0
).
eq
(
"document_type"
,
htzzQueryDto
.
getDocumentType
())
.
gt
(!
StrUtil
.
hasBlank
(
e
),
"yxdqr"
,
!
StrUtil
.
hasBlank
(
e
)
?
e
:
"9999-01-01 00:00:00"
)
.
and
(!
StrUtil
.
hasBlank
(
q
),
wq
->
wq
.
like
(
"zjmc"
,
q
).
or
().
like
(
"czry"
,
q
)).
orderByDesc
(
"lrsj_time"
);
IPage
<
HtzzAdminZzda
>
zPage
=
HtzzAdminZzda
.
builder
().
build
().
selectPage
(
page
,
queryWrapper
);
List
<
HtzzAdminZzda
>
zccrkgls
=
zPage
.
getRecords
();
zPage
.
getCurrent
();
zPage
.
getPages
();
zPage
.
getTotal
();
zPage
.
getSize
();
return
ResultUtil
.
data
(
zPage
,
zccrkgls
,
"搜索成功"
);
}
/**
* 根据id查合同
*
* @param id
* @return
*/
@PostMapping
(
value
=
"/queryHt/{id}"
)
@ApiOperation
(
value
=
"根据id查合同"
,
httpMethod
=
"POST"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
queryHt
(
@CurrentUser
UserBean
userBean
,
@PathVariable
Integer
id
)
{
List
<
Object
>
list
=
new
ArrayList
<
Object
>();
list
.
add
(
HtzzAdminZzda
.
builder
().
build
().
selectOne
(
new
QueryWrapper
<
HtzzAdminZzda
>()
.
eq
(
"org_code"
,
userBean
.
getOrgCode
()).
eq
(
"is_delete"
,
0
).
eq
(
"id"
,
id
)));
QueryWrapper
<
HtzzAssoHtgx
>
q
=
new
QueryWrapper
<
HtzzAssoHtgx
>();
q
.
select
(
"id"
,
"name"
).
eq
(
"htid"
,
id
);
List
<
HtzzAssoHtgx
>
htgx
=
HtzzAssoHtgx
.
builder
().
build
().
selectList
(
q
);
for
(
HtzzAssoHtgx
gx
:
htgx
)
{
String
name
=
gx
.
getName
();
list
.
add
(
name
);
}
return
ResultUtil
.
data
(
list
);
}
/**
* 根据id删除合同
*
* @param id
* @return
*/
@DeleteMapping
(
value
=
"/delHt"
)
@ApiOperation
(
value
=
"根据id删除合同"
,
httpMethod
=
"DELETE"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
delHt
(
@CurrentUser
UserBean
userBean
,
@RequestParam
Integer
htid
)
{
// 删除提醒关系
HtzzAssoHtgx
.
builder
().
build
().
delete
(
new
QueryWrapper
<
HtzzAssoHtgx
>().
eq
(
"org_code"
,
userBean
.
getOrgCode
()).
eq
(
"htid"
,
htid
));
//删除证照提醒表配置
HtzzAssoZztx
.
builder
().
build
().
delete
(
new
QueryWrapper
<
HtzzAssoZztx
>().
lambda
().
eq
(
HtzzAssoZztx:
:
getZzdaId
,
htid
));
// 设置证照删除状态
return
ResultUtil
.
data
(
HtzzAdminZzda
.
builder
().
id
(
htid
).
isDelete
(
1
).
build
().
updateById
());
}
/**
* 修改提醒状态
*
* @param id
* @return
*/
@PostMapping
(
value
=
"/changeTx"
)
@ApiOperation
(
value
=
"修改提醒状态"
,
httpMethod
=
"POST"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
changeTx
(
@CurrentUser
UserBean
userBean
,
@RequestParam
Integer
htid
)
{
QueryWrapper
<
HtzzAdminZzda
>
queryWrapper
=
new
QueryWrapper
<>();
queryWrapper
.
select
(
"txkg_type"
).
eq
(
"id"
,
htid
);
HtzzAdminZzda
zzda
=
HtzzAdminZzda
.
builder
().
build
().
selectOne
(
queryWrapper
);
UpdateWrapper
<
HtzzAdminZzda
>
updateWrapper
=
new
UpdateWrapper
<>();
updateWrapper
.
eq
(
"id"
,
htid
);
HtzzAdminZzda
w
=
HtzzAdminZzda
.
builder
().
orgCode
(
userBean
.
getOrgCode
()).
id
(
htid
)
.
txkgType
(
zzda
.
getTxkgType
()
==
0
?
1
:
0
).
build
();
htzzAdminZzdaMapper
.
update
(
w
,
updateWrapper
);
return
ResultUtil
.
success
(
"修改成功"
);
}
/**
* 查詢全部員工id、姓名
*
* @param id
* @return
*/
@GetMapping
(
value
=
"/queryYg"
)
@ApiOperation
(
value
=
"查詢全部員工id、姓名"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Object
>
queryYg
(
@CurrentUser
UserBean
userBean
)
{
return
ResultUtil
.
data
(
YgglMainEmp
.
builder
().
build
().
selectList
(
new
QueryWrapper
<
YgglMainEmp
>().
select
(
"emp_num"
,
"name"
,
"phone"
).
eq
(
"org_code"
,
userBean
.
getOrgCode
())));
}
@GetMapping
(
value
=
"/test"
)
@ApiOperation
(
value
=
"test"
,
httpMethod
=
"GET"
,
notes
=
"接口发布说明"
)
public
Result
<
Void
>
test
(
String
schedulingPattern
)
{
CronUtil
.
schedule
(
"*/2 * * 24 * *"
,
new
Task
()
{
@Override
public
void
execute
()
{
Logoutput
(
"start:"
+
new
Date
());
}
});
CronUtil
.
setMatchSecond
(
true
);
CronUtil
.
start
();
return
ResultUtil
.
success
(
"操作成功"
);
}
@Value
(
"${config-8timer.environmental-science}"
)
public
String
environmental_science
;
public
void
Logoutput
(
String
science
)
{
if
(!(
"pro"
).
equals
(
environmental_science
))
{
System
.
out
.
println
(
science
);
}
else
{
System
.
out
.
println
(
""
);
}
}
/**
* @Author wgd
* @Description 导出勾选的合同
* @Date 2021/12/2 17:05
**/
@PostMapping
(
value
=
"/exportContract"
)
public
Result
<
Object
>
exportContract
(
@CurrentUser
UserBean
userBean
,
@RequestBody
List
<
Integer
>
ids
,
HttpServletRequest
request
,
HttpServletResponse
resp
)
{
if
(
ids
.
size
()
<=
0
)
{
return
ResultUtil
.
error
(
"操作失败"
);
}
Map
<
String
,
String
>
url
=
Maps
.
newHashMap
();
ids
.
forEach
(
i
->
{
HtzzAdminZzda
htzzAdminZzda
=
HtzzAdminZzda
.
builder
().
id
(
i
).
build
().
selectById
();
/*格式化获取文字*/
String
urla
=
UrlEncoded
.
decodeString
(
htzzAdminZzda
.
getZjfj
());
int
index
=
urla
.
indexOf
(
project_package
);
url
.
put
(
htzzAdminZzda
.
getId
()
+
"_"
+
htzzAdminZzda
.
getZjmc
(),
urla
.
substring
(
index
));
});
String
zipUrl
=
null
;
try
{
zipUrl
=
oss
.
createZip
(
url
);
return
ResultUtil
.
data
(
zipUrl
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
ResultUtil
.
error
(
"操作失败"
);
}
@GetMapping
(
value
=
"/exportContract"
)
public
void
exportContract
(
@CurrentUser
UserBean
userBean
,
@RequestParam
(
"url"
)
String
url
,
HttpServletRequest
request
,
HttpServletResponse
resp
)
{
FileInputStream
fis
=
null
;
BufferedInputStream
bis
=
null
;
OutputStream
os
;
File
filePath
=
null
;
try
{
resp
.
setContentType
(
"application/octet-stream"
);
resp
.
setHeader
(
"content-disposition"
,
"attachment; filename="
+
new
String
(
url
.
getBytes
(
"UTF8"
),
"ISO-8859-1"
));
resp
.
setCharacterEncoding
(
"UTF-8"
);
filePath
=
new
File
(
zipPath
+
url
);
byte
[]
buffer
=
new
byte
[
1024
];
fis
=
new
FileInputStream
(
filePath
);
bis
=
new
BufferedInputStream
(
fis
);
os
=
resp
.
getOutputStream
();
int
i
=
bis
.
read
(
buffer
);
while
(
i
!=
-
1
)
{
os
.
write
(
buffer
,
0
,
i
);
i
=
bis
.
read
(
buffer
);
}
os
.
flush
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
bis
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
fis
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
filePath
.
delete
();
}
}
}
src/main/java/cn/timer/api/utils/aliyun/OSSUtil.java
View file @
4d7dbfd6
package
cn
.
timer
.
api
.
utils
.
aliyun
;
import
java.io.BufferedReader
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.*
;
import
java.net.URL
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.text.DateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
import
com.google.common.collect.Lists
;
import
com.google.common.collect.Maps
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.bouncycastle.jce.exception.ExtIOException
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.multipart.MultipartFile
;
...
...
@@ -38,315 +40,319 @@ import cn.hutool.core.util.StrUtil;
import
cn.timer.api.utils.Result
;
import
cn.timer.api.utils.ResultUtil
;
import
javax.servlet.http.HttpServletResponse
;
/**
* OSS接口操作示例 1.创建空间 2.上传文件 3.下载文件
*
* @author dsc
*
* @author dsc
*/
@Component
public
class
OSSUtil
{
@Value
(
"${config-8timer.Aliyun.endpoint}"
)
private
String
endpoint
;
@Value
(
"${config-8timer.Aliyun.ACCESSKEY_ID}"
)
private
String
accessKeyId
;
@Value
(
"${config-8timer.Aliyun.SECRET}"
)
private
String
accessKeySecret
;
@Value
(
"${config-8timer.Aliyun.bucketName}"
)
private
String
bucketName
;
@Value
(
"${config-8timer.Aliyun.bucketName_pri}"
)
private
String
bucketName_pri
;
@Value
(
"${config-8timer.Aliyun.project_package}"
)
private
String
project_package
;
@Value
(
"${config-8timer.Aliyun.expirationTime}"
)
private
String
expirationTime
;
@Value
(
"${config-8timer.Aliyun.expirationTime_pri}"
)
private
String
expirationTime_pri
;
/**
* 1.创建储存空间 sout控制台輸出 储存空间名
*
* @param bucketName OSS空间名
* @return 成功信息
*/
public
Result
<
Void
>
createZone
(
String
bucketName
)
{
// Endpoint以杭州为例,其它Region请按实际情况填写。
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
// https://ram.console.aliyun.com 创建RAM账号。
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建存储空间。
ossClient
.
createBucket
(
bucketName
);
// 关闭OSSClient。
ossClient
.
shutdown
();
// 控制台输出空间名
Logoutput
(
"bucketName:"
+
bucketName
);
// 返回成功
return
ResultUtil
.
success
(
"创建OSS空间成功"
);
}
/**
* 2.上传文件到8time-v2
*
* @param path 上传路径(用'/'分包)
* @param ins 输入流
* @return
*/
public
String
uploadFile
(
File
path
,
InputStream
ins
)
{
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
Logoutput
(
endpoint
);
Logoutput
(
accessKeyId
);
Logoutput
(
accessKeySecret
);
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建PutObjectRequest对象。
// 项目名+用户公司id+模块名+文件夹名+文件名
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
bucketName
,
path
.
getPath
(),
ins
);
// [file1,file2,file3],["mk1/img","mk1"]
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS,
// StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传文件。
ossClient
.
putObject
(
putObjectRequest
);
// 设置URL过期时间为100年。 1s 1h 1d 1y 100y
@Value
(
"${config-8timer.Aliyun.endpoint}"
)
private
String
endpoint
;
@Value
(
"${config-8timer.Aliyun.ACCESSKEY_ID}"
)
private
String
accessKeyId
;
@Value
(
"${config-8timer.Aliyun.SECRET}"
)
private
String
accessKeySecret
;
@Value
(
"${config-8timer.Aliyun.bucketName}"
)
private
String
bucketName
;
@Value
(
"${config-8timer.Aliyun.bucketName_pri}"
)
private
String
bucketName_pri
;
@Value
(
"${config-8timer.Aliyun.project_package}"
)
private
String
project_package
;
@Value
(
"${config-8timer.Aliyun.expirationTime}"
)
private
String
expirationTime
;
@Value
(
"${config-8timer.Aliyun.expirationTime_pri}"
)
private
String
expirationTime_pri
;
@Value
(
"${zip.path}"
)
private
String
zipPath
;
/**
* 1.创建储存空间 sout控制台輸出 储存空间名
*
* @param bucketName OSS空间名
* @return 成功信息
*/
public
Result
<
Void
>
createZone
(
String
bucketName
)
{
// Endpoint以杭州为例,其它Region请按实际情况填写。
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
// https://ram.console.aliyun.com 创建RAM账号。
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建存储空间。
ossClient
.
createBucket
(
bucketName
);
// 关闭OSSClient。
ossClient
.
shutdown
();
// 控制台输出空间名
Logoutput
(
"bucketName:"
+
bucketName
);
// 返回成功
return
ResultUtil
.
success
(
"创建OSS空间成功"
);
}
/**
* 2.上传文件到8time-v2
*
* @param path 上传路径(用'/'分包)
* @param ins 输入流
* @return
*/
public
String
uploadFile
(
File
path
,
InputStream
ins
)
{
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
Logoutput
(
endpoint
);
Logoutput
(
accessKeyId
);
Logoutput
(
accessKeySecret
);
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建PutObjectRequest对象。
// 项目名+用户公司id+模块名+文件夹名+文件名
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
bucketName
,
path
.
getPath
(),
ins
);
// [file1,file2,file3],["mk1/img","mk1"]
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS,
// StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传文件。
ossClient
.
putObject
(
putObjectRequest
);
// 设置URL过期时间为100年。 1s 1h 1d 1y 100y
// Date expiration = new Date(new Date().getTime() + 1000L * 3600 * 24 * 365 * 100);
Date
now
=
new
Date
();
Date
expiration
=
DateUtil
.
offset
(
now
,
DateField
.
YEAR
,
100
);
// 一百年之后过期
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
.
getPath
(),
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
// 返回上传成功的文件名
return
url
.
toString
().
substring
(
0
,
url
.
toString
().
lastIndexOf
(
"?"
));
}
/**
* 3.上传文件到8time-v2-private
*
* @param path 上传路径(用'/'分包)
* @param ins 输入流
* @return 文件路径
*/
public
String
uploadPrivateFile
(
String
path
,
InputStream
ins
)
{
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建PutObjectRequest对象。
// 项目名+用户公司id+模块名+文件夹名+文件名
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
bucketName_pri
,
path
,
ins
);
// [file1,file2,file3],["mk1/img","mk1"]
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS,
// StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传文件。
ossClient
.
putObject
(
putObjectRequest
);
// 设置URL过期时间为10分钟
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
1000
*
600
);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName_pri
,
path
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
// 返回上传成功的文件名
return
path
;
}
/**
* 4.从OSS下载文件
*
* @param path
本地储存文件路径
* @param orgCode
公司id
* @param dir
包名
* @param fileName
文件名
* @param moudle
模块名
* @return 完整文件名
*/
public
Result
<
String
>
downloadFile
(
String
orgCode
,
String
moudle
,
String
dir
,
String
fileName
,
String
path
)
{
// <yourObjectName>从OSS下载文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
ossClient
.
getObject
(
new
GetObjectRequest
(
bucketName
,
project_package
+
orgCode
+
"/"
+
moudle
+
"/"
+
dir
+
"/"
+
fileName
),
new
File
(
path
+
fileName
));
// 关闭OSSClient。
ossClient
.
shutdown
();
// 完整路径的文件名
Logoutput
(
"objectName:"
+
project_package
+
orgCode
+
"/"
+
moudle
+
"/"
+
dir
+
"/"
+
fileName
);
Logoutput
(
"path:"
+
path
+
fileName
);
// 返回成功的本地路径
return
ResultUtil
.
success
(
path
+
fileName
);
}
public
Result
<
BufferedReader
>
downloadFile
(
String
objectName
)
throws
IOException
{
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
OSSObject
ossObject
=
ossClient
.
getObject
(
bucketName
,
objectName
);
// 读取文件内容。
System
.
out
.
println
(
"Object content:"
);
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
ossObject
.
getObjectContent
()));
while
(
true
)
{
String
line
=
reader
.
readLine
();
if
(
line
==
null
)
break
;
System
.
out
.
println
(
"\n"
+
line
);
}
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
reader
.
close
();
// 关闭OSSClient。
ossClient
.
shutdown
();
return
ResultUtil
.
data
(
reader
,
"下载成功"
);
}
/**
* 文件输入流批量上传
*
* @param request 获取参数
* @param files
批量文件
* @return 返回上传成功或失败的信息
*/
public
String
uploadFile
(
MultipartHttpServletRequest
request
,
List
<
MultipartFile
>
files
)
{
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
String
newFileName
=
null
;
URL
url
=
null
;
try
{
// 上传文件
CommonsMultipartResolver
multipartResolver
=
new
CommonsMultipartResolver
(
request
.
getSession
().
getServletContext
());
// 判断 request 是否有文件上传,即多部分请求
if
(
multipartResolver
.
isMultipart
(
request
))
{
// 转换成多部分request
MultipartHttpServletRequest
multiRequest
=
(
MultipartHttpServletRequest
)
request
;
// 取得request中的所有文件名
Iterator
<
String
>
iter
=
multiRequest
.
getFileNames
();
// 此处上传单个文件,如需多个文件改为while循环
while
(
iter
.
hasNext
())
{
// 取得上传文件
files
=
multiRequest
.
getFiles
(
iter
.
next
());
if
(
files
!=
null
)
{
// 取得当前上传文件的文件名称
String
myFileName
=
null
;
for
(
MultipartFile
file
:
files
)
{
myFileName
=
file
.
getOriginalFilename
();
// 如果名称不为“”,说明该文件存在,否则说明该文件不存在
if
(
StrUtil
.
isNotEmpty
(
myFileName
.
trim
()))
{
newFileName
=
System
.
currentTimeMillis
()
+
myFileName
.
substring
(
myFileName
.
lastIndexOf
(
"."
));
// 上传客户端文件到oss,如直接上传服务器文件到oss,可使用new FileInputStream( new File("服务器文件路径"))
ossClient
.
putObject
(
bucketName
,
newFileName
,
file
.
getInputStream
());
// 设置URL过期时间为100年。 1s 1h 1d 1y 100y
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
Long
.
parseLong
(
expirationTime
));
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
newFileName
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
}
}
}
}
}
}
catch
(
IOException
e
)
{
e
.
getStackTrace
();
}
finally
{
// 关闭ossClient
if
(
null
!=
ossClient
)
ossClient
.
shutdown
();
}
return
url
.
toString
();
/*
* if (file == null || file.getSize() <= 0) { return
* ResultUtil.error("上传的文件为空,请重新选择!"); } else { try {
* OSSUtil.uploadFile(userBean.getOrgCode(), className,
* file.getOriginalFilename(), file.getInputStream()); } catch (IOException e) {
* e.printStackTrace(); } return ResultUtil.success("上传成功"); }
*/
}
/**
* 获取8time-v2文件的url
*
* @return url地址
*/
public
String
getUrl
(
String
path
)
{
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 设置URL过期时间为100年。 1s 1h 1d 1y 10y
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
Long
.
parseLong
(
expirationTime
));
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
return
url
.
toString
();
}
/**
* 获取8time-v2-private文件的url
*
* @return url地址
*/
public
String
getUrlP
(
String
path
)
{
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 设置URL过期时间为10分钟。
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
Long
.
parseLong
(
expirationTime_pri
));
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
return
url
.
toString
();
}
/**
* 删除单个
*
* @param objectName 文件名
*/
public
void
delSingleFile
(
String
objectName
)
{
Date
now
=
new
Date
();
Date
expiration
=
DateUtil
.
offset
(
now
,
DateField
.
YEAR
,
100
);
// 一百年之后过期
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
.
getPath
(),
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
// 返回上传成功的文件名
return
url
.
toString
().
substring
(
0
,
url
.
toString
().
lastIndexOf
(
"?"
));
}
/**
* 3.上传文件到8time-v2-private
*
* @param path 上传路径(用'/'分包)
* @param ins 输入流
* @return 文件路径
*/
public
String
uploadPrivateFile
(
String
path
,
InputStream
ins
)
{
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建PutObjectRequest对象。
// 项目名+用户公司id+模块名+文件夹名+文件名
PutObjectRequest
putObjectRequest
=
new
PutObjectRequest
(
bucketName_pri
,
path
,
ins
);
// [file1,file2,file3],["mk1/img","mk1"]
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS,
// StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传文件。
ossClient
.
putObject
(
putObjectRequest
);
// 设置URL过期时间为10分钟
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
1000
*
600
);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName_pri
,
path
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
// 返回上传成功的文件名
return
path
;
}
/**
* 4.从OSS下载文件
*
* @param path
本地储存文件路径
* @param orgCode
公司id
* @param dir
包名
* @param fileName
文件名
* @param moudle
模块名
* @return 完整文件名
*/
public
Result
<
String
>
downloadFile
(
String
orgCode
,
String
moudle
,
String
dir
,
String
fileName
,
String
path
)
{
// <yourObjectName>从OSS下载文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// String objectName = "8time-v2/test/123.jpg";
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖,不存在则新建。
ossClient
.
getObject
(
new
GetObjectRequest
(
bucketName
,
project_package
+
orgCode
+
"/"
+
moudle
+
"/"
+
dir
+
"/"
+
fileName
),
new
File
(
path
+
fileName
));
// 关闭OSSClient。
ossClient
.
shutdown
();
// 完整路径的文件名
Logoutput
(
"objectName:"
+
project_package
+
orgCode
+
"/"
+
moudle
+
"/"
+
dir
+
"/"
+
fileName
);
Logoutput
(
"path:"
+
path
+
fileName
);
// 返回成功的本地路径
return
ResultUtil
.
success
(
path
+
fileName
);
}
public
Result
<
BufferedReader
>
downloadFile
(
String
objectName
)
throws
IOException
{
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
OSSObject
ossObject
=
ossClient
.
getObject
(
bucketName
,
objectName
);
// 读取文件内容。
System
.
out
.
println
(
"Object content:"
);
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
ossObject
.
getObjectContent
()));
while
(
true
)
{
String
line
=
reader
.
readLine
();
if
(
line
==
null
)
break
;
System
.
out
.
println
(
"\n"
+
line
);
}
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
reader
.
close
();
// 关闭OSSClient。
ossClient
.
shutdown
();
return
ResultUtil
.
data
(
reader
,
"下载成功"
);
}
/**
* 文件输入流批量上传
*
* @param request 获取参数
* @param files
批量文件
* @return 返回上传成功或失败的信息
*/
public
String
uploadFile
(
MultipartHttpServletRequest
request
,
List
<
MultipartFile
>
files
)
{
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
String
newFileName
=
null
;
URL
url
=
null
;
try
{
// 上传文件
CommonsMultipartResolver
multipartResolver
=
new
CommonsMultipartResolver
(
request
.
getSession
().
getServletContext
());
// 判断 request 是否有文件上传,即多部分请求
if
(
multipartResolver
.
isMultipart
(
request
))
{
// 转换成多部分request
MultipartHttpServletRequest
multiRequest
=
(
MultipartHttpServletRequest
)
request
;
// 取得request中的所有文件名
Iterator
<
String
>
iter
=
multiRequest
.
getFileNames
();
// 此处上传单个文件,如需多个文件改为while循环
while
(
iter
.
hasNext
())
{
// 取得上传文件
files
=
multiRequest
.
getFiles
(
iter
.
next
());
if
(
files
!=
null
)
{
// 取得当前上传文件的文件名称
String
myFileName
=
null
;
for
(
MultipartFile
file
:
files
)
{
myFileName
=
file
.
getOriginalFilename
();
// 如果名称不为“”,说明该文件存在,否则说明该文件不存在
if
(
StrUtil
.
isNotEmpty
(
myFileName
.
trim
()))
{
newFileName
=
System
.
currentTimeMillis
()
+
myFileName
.
substring
(
myFileName
.
lastIndexOf
(
"."
));
// 上传客户端文件到oss,如直接上传服务器文件到oss,可使用new FileInputStream( new File("服务器文件路径"))
ossClient
.
putObject
(
bucketName
,
newFileName
,
file
.
getInputStream
());
// 设置URL过期时间为100年。 1s 1h 1d 1y 100y
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
Long
.
parseLong
(
expirationTime
));
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
newFileName
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
}
}
}
}
}
}
catch
(
IOException
e
)
{
e
.
getStackTrace
();
}
finally
{
// 关闭ossClient
if
(
null
!=
ossClient
)
ossClient
.
shutdown
();
}
return
url
.
toString
();
/*
* if (file == null || file.getSize() <= 0) { return
* ResultUtil.error("上传的文件为空,请重新选择!"); } else { try {
* OSSUtil.uploadFile(userBean.getOrgCode(), className,
* file.getOriginalFilename(), file.getInputStream()); } catch (IOException e) {
* e.printStackTrace(); } return ResultUtil.success("上传成功"); }
*/
}
/**
* 获取8time-v2文件的url
*
* @return url地址
*/
public
String
getUrl
(
String
path
)
{
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 设置URL过期时间为100年。 1s 1h 1d 1y 10y
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
Long
.
parseLong
(
expirationTime
));
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
return
url
.
toString
();
}
/**
* 获取8time-v2-private文件的url
*
* @return url地址
*/
public
String
getUrlP
(
String
path
)
{
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 设置URL过期时间为10分钟。
Date
expiration
=
new
Date
(
new
Date
().
getTime
()
+
Long
.
parseLong
(
expirationTime_pri
));
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL
url
=
ossClient
.
generatePresignedUrl
(
bucketName
,
path
,
expiration
);
Logoutput
(
String
.
valueOf
(
url
));
// 关闭OSSClient。
ossClient
.
shutdown
();
return
url
.
toString
();
}
/**
* 删除单个
*
* @param objectName 文件名
*/
public
void
delSingleFile
(
String
objectName
)
{
// // Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
...
...
@@ -355,24 +361,24 @@ public class OSSUtil {
// String bucketName = "<yourBucketName>";
// String objectName = "<yourObjectName>";
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 删除文件。如需删除文件夹,请将ObjectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
ossClient
.
deleteObject
(
bucketName
,
objectName
);
// 删除文件。如需删除文件夹,请将ObjectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
ossClient
.
deleteObject
(
bucketName
,
objectName
);
// 关闭OSSClient。
ossClient
.
shutdown
();
}
// 关闭OSSClient。
ossClient
.
shutdown
();
}
/**
* 删除多个文件
*
* @param keys 需要删除的文件。
* @param quiet 返回模式。true表示简单模式,false表示详细模式。默认为详细模式。
* @return 删除结果。详细模式下为删除成功的文件列表,简单模式下为删除失败的文件列表。
*/
public
List
<
String
>
delFiles
(
List
<
String
>
keys
,
boolean
quiet
)
{
/**
* 删除多个文件
*
* @param keys 需要删除的文件。
* @param quiet 返回模式。true表示简单模式,false表示详细模式。默认为详细模式。
* @return 删除结果。详细模式下为删除成功的文件列表,简单模式下为删除失败的文件列表。
*/
public
List
<
String
>
delFiles
(
List
<
String
>
keys
,
boolean
quiet
)
{
// // Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
...
...
@@ -380,31 +386,31 @@ public class OSSUtil {
// String accessKeySecret = "<yourAccessKeySecret>";
// String bucketName = "<yourBucketName>";
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 删除文件。key等同于ObjectName,表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// 删除文件。key等同于ObjectName,表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
// List<String> keys = new ArrayList<String>();
// keys.add("key0");
// keys.add("key1");
// keys.add("key2");
DeleteObjectsResult
deleteObjectsResult
=
ossClient
.
deleteObjects
(
new
DeleteObjectsRequest
(
bucketName
).
withKeys
(
keys
).
withQuiet
(
quiet
));
List
<
String
>
deletedObjects
=
deleteObjectsResult
.
getDeletedObjects
();
DeleteObjectsResult
deleteObjectsResult
=
ossClient
.
deleteObjects
(
new
DeleteObjectsRequest
(
bucketName
).
withKeys
(
keys
).
withQuiet
(
quiet
));
List
<
String
>
deletedObjects
=
deleteObjectsResult
.
getDeletedObjects
();
// 关闭OSSClient。
ossClient
.
shutdown
();
// 关闭OSSClient。
ossClient
.
shutdown
();
return
deletedObjects
;
}
return
deletedObjects
;
}
/**
* 删除指定前缀(prefix)的文件
*
* @param prefix
*/
public
void
delPrefixWith
(
String
prefix
)
{
/**
* 删除指定前缀(prefix)的文件
*
* @param prefix
*/
public
void
delPrefixWith
(
String
prefix
)
{
// // Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
...
...
@@ -416,79 +422,79 @@ public class OSSUtil {
// // 指定前缀。
// final String prefix = "<yourkeyPrefix>";
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 列举所有包含指定前缀的文件并删除。
String
nextMarker
=
null
;
ObjectListing
objectListing
=
null
;
do
{
ListObjectsRequest
listObjectsRequest
=
new
ListObjectsRequest
(
bucketName
).
withPrefix
(
prefix
)
.
withMarker
(
nextMarker
);
objectListing
=
ossClient
.
listObjects
(
listObjectsRequest
);
if
(
objectListing
.
getObjectSummaries
().
size
()
>
0
)
{
List
<
String
>
keys
=
new
ArrayList
<
String
>();
for
(
OSSObjectSummary
s
:
objectListing
.
getObjectSummaries
())
{
Logoutput
(
"key name: "
+
s
.
getKey
());
keys
.
add
(
s
.
getKey
());
}
DeleteObjectsRequest
deleteObjectsRequest
=
new
DeleteObjectsRequest
(
bucketName
).
withKeys
(
keys
);
ossClient
.
deleteObjects
(
deleteObjectsRequest
);
}
nextMarker
=
objectListing
.
getNextMarker
();
}
while
(
objectListing
.
isTruncated
());
// 关闭OSSClient。
ossClient
.
shutdown
();
}
/*
* (跨域资源) // Endpoint以杭州为例,其它Region请按实际情况填写。 String endpoint =
* "http://oss-cn-hangzhou.aliyuncs.com"; //
* 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
* https://ram.console.aliyun.com 创建RAM账号 String accessKeyId =
* "<yourAccessKeyId>"; String accessKeySecret = "<yourAccessKeySecret>"; String
* bucketName = "<yourBucketName>";
*
* // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint,
* accessKeyId, accessKeySecret);
*
* SetBucketCORSRequest request = new SetBucketCORSRequest(bucketName);
*
* // 跨域资源共享规则的容器,每个存储空间最多允许10条规则。 ArrayList<CORSRule> putCorsRules = new
* ArrayList<CORSRule>();
*
* CORSRule corRule = new CORSRule();
*
* ArrayList<String> allowedOrigin = new ArrayList<String>(); // 指定允许跨域请求的来源。
* allowedOrigin.add( "http://www.b.com");
*
* ArrayList<String> allowedMethod = new ArrayList<String>(); //
* 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD)。 allowedMethod.add("GET");
*
* ArrayList<String> allowedHeader = new ArrayList<String>(); //
* 是否允许预取指令(OPTIONS)中Access-Control-Request-Headers头中指定的Header。
* allowedHeader.add("x-oss-test");
*
* ArrayList<String> exposedHeader = new ArrayList<String>(); //
* 指定允许用户从应用程序中访问的响应头。 exposedHeader.add("x-oss-test1"); //
* AllowedOrigins和AllowedMethods最多支持一个星号(*)通配符。星号(*)表示允许所有的域来源或者操作。
* corRule.setAllowedMethods(allowedMethod);
* corRule.setAllowedOrigins(allowedOrigin); //
* AllowedHeaders和ExposeHeaders不支持通配符。 corRule.setAllowedHeaders(allowedHeader);
* corRule.setExposeHeaders(exposedHeader); //
* 指定浏览器对特定资源的预取(OPTIONS)请求返回结果的缓存时间,单位为秒。 corRule.setMaxAgeSeconds(10);
*
* // 最多允许10条规则。 putCorsRules.add(corRule); // 已存在的规则将被覆盖。
* request.setCorsRules(putCorsRules); ossClient.setBucketCORS(request);
*
* // 关闭OSSClient。 ossClient.shutdown();
*/
public
void
download
(
String
url
,
String
savePath
)
{
// Endpoint以杭州为例,其它Region请按实际情况填写。
// 创建OSSClient实例。
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// 列举所有包含指定前缀的文件并删除。
String
nextMarker
=
null
;
ObjectListing
objectListing
=
null
;
do
{
ListObjectsRequest
listObjectsRequest
=
new
ListObjectsRequest
(
bucketName
).
withPrefix
(
prefix
)
.
withMarker
(
nextMarker
);
objectListing
=
ossClient
.
listObjects
(
listObjectsRequest
);
if
(
objectListing
.
getObjectSummaries
().
size
()
>
0
)
{
List
<
String
>
keys
=
new
ArrayList
<
String
>();
for
(
OSSObjectSummary
s
:
objectListing
.
getObjectSummaries
())
{
Logoutput
(
"key name: "
+
s
.
getKey
());
keys
.
add
(
s
.
getKey
());
}
DeleteObjectsRequest
deleteObjectsRequest
=
new
DeleteObjectsRequest
(
bucketName
).
withKeys
(
keys
);
ossClient
.
deleteObjects
(
deleteObjectsRequest
);
}
nextMarker
=
objectListing
.
getNextMarker
();
}
while
(
objectListing
.
isTruncated
());
// 关闭OSSClient。
ossClient
.
shutdown
();
}
/*
* (跨域资源) // Endpoint以杭州为例,其它Region请按实际情况填写。 String endpoint =
* "http://oss-cn-hangzhou.aliyuncs.com"; //
* 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
* https://ram.console.aliyun.com 创建RAM账号 String accessKeyId =
* "<yourAccessKeyId>"; String accessKeySecret = "<yourAccessKeySecret>"; String
* bucketName = "<yourBucketName>";
*
* // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint,
* accessKeyId, accessKeySecret);
*
* SetBucketCORSRequest request = new SetBucketCORSRequest(bucketName);
*
* // 跨域资源共享规则的容器,每个存储空间最多允许10条规则。 ArrayList<CORSRule> putCorsRules = new
* ArrayList<CORSRule>();
*
* CORSRule corRule = new CORSRule();
*
* ArrayList<String> allowedOrigin = new ArrayList<String>(); // 指定允许跨域请求的来源。
* allowedOrigin.add( "http://www.b.com");
*
* ArrayList<String> allowedMethod = new ArrayList<String>(); //
* 指定允许的跨域请求方法(GET/PUT/DELETE/POST/HEAD)。 allowedMethod.add("GET");
*
* ArrayList<String> allowedHeader = new ArrayList<String>(); //
* 是否允许预取指令(OPTIONS)中Access-Control-Request-Headers头中指定的Header。
* allowedHeader.add("x-oss-test");
*
* ArrayList<String> exposedHeader = new ArrayList<String>(); //
* 指定允许用户从应用程序中访问的响应头。 exposedHeader.add("x-oss-test1"); //
* AllowedOrigins和AllowedMethods最多支持一个星号(*)通配符。星号(*)表示允许所有的域来源或者操作。
* corRule.setAllowedMethods(allowedMethod);
* corRule.setAllowedOrigins(allowedOrigin); //
* AllowedHeaders和ExposeHeaders不支持通配符。 corRule.setAllowedHeaders(allowedHeader);
* corRule.setExposeHeaders(exposedHeader); //
* 指定浏览器对特定资源的预取(OPTIONS)请求返回结果的缓存时间,单位为秒。 corRule.setMaxAgeSeconds(10);
*
* // 最多允许10条规则。 putCorsRules.add(corRule); // 已存在的规则将被覆盖。
* request.setCorsRules(putCorsRules); ossClient.setBucketCORS(request);
*
* // 关闭OSSClient。 ossClient.shutdown();
*/
public
void
download
(
String
url
,
String
savePath
)
{
// Endpoint以杭州为例,其它Region请按实际情况填写。
// String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
// // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录
// // https://ram.console.aliyun.com 创建RAM账号。
...
...
@@ -497,79 +503,138 @@ public class OSSUtil {
// String bucketName = "8time-v2";
// String objectName = "8timer2.0/117/test/测试.pdf";
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
// String url = "http://8time-v2.oss-cn-shenzhen.aliyuncs.com/8timer2.0/310/qyxx/filepath/158872957193802f4a85a-9dd9-483a-9049-86fac5c73bd5.pdf?Expires=4747952308&OSSAccessKeyId=LTAI4FuaShJWQ1dggsFWG5CC&Signature=z%2FNIX0RrxnLuBLAS9olPpxxsCrI%3D";
try
{
// 带进度条的下载。
ObjectMetadata
a
=
ossClient
.
getObject
(
new
GetObjectRequest
(
new
URL
(
url
),
null
),
new
File
(
savePath
));
System
.
err
.
println
(
a
);
try
{
// 带进度条的下载。
ObjectMetadata
a
=
ossClient
.
getObject
(
new
GetObjectRequest
(
new
URL
(
url
),
null
),
new
File
(
savePath
));
System
.
err
.
println
(
a
);
// new File("C:\\Users\\Lenovo\\Desktop\\8小时\\test2.pdf"));
// // 带进度条的下载。
// ossClient.getObject(new GetObjectRequest(bucketName, objectName).<GetObjectRequest>withProgressListener(
// new GetObjectProgressListener()), new File("C:\\Users\\Lenovo\\Desktop\\8小时\\test.pdf"));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
// 关闭OSSClient。
ossClient
.
shutdown
();
}
static
class
GetObjectProgressListener
implements
ProgressListener
{
private
long
bytesRead
=
0
;
private
long
totalBytes
=
-
1
;
private
boolean
succeed
=
false
;
@Override
public
void
progressChanged
(
ProgressEvent
progressEvent
)
{
long
bytes
=
progressEvent
.
getBytes
();
ProgressEventType
eventType
=
progressEvent
.
getEventType
();
switch
(
eventType
)
{
case
TRANSFER_STARTED_EVENT:
System
.
out
.
println
(
"Start to download......"
);
break
;
case
RESPONSE_CONTENT_LENGTH_EVENT:
this
.
totalBytes
=
bytes
;
System
.
out
.
println
(
this
.
totalBytes
+
" bytes in total will be downloaded to a local file"
);
break
;
case
RESPONSE_BYTE_TRANSFER_EVENT:
this
.
bytesRead
+=
bytes
;
if
(
this
.
totalBytes
!=
-
1
)
{
int
percent
=
(
int
)
(
this
.
bytesRead
*
100.0
/
this
.
totalBytes
);
System
.
out
.
println
(
bytes
+
" bytes have been read at this time, download progress: "
+
percent
+
"%("
+
this
.
bytesRead
+
"/"
+
this
.
totalBytes
+
")"
);
}
else
{
System
.
out
.
println
(
bytes
+
" bytes have been read at this time, download ratio: unknown"
+
"("
+
this
.
bytesRead
+
"/...)"
);
}
break
;
case
TRANSFER_COMPLETED_EVENT:
this
.
succeed
=
true
;
System
.
out
.
println
(
"Succeed to download, "
+
this
.
bytesRead
+
" bytes have been transferred in total"
);
break
;
case
TRANSFER_FAILED_EVENT:
System
.
out
.
println
(
"Failed to download, "
+
this
.
bytesRead
+
" bytes have been transferred"
);
break
;
default
:
break
;
}
}
public
boolean
isSucceed
()
{
return
succeed
;
}
}
@Value
(
"${config-8timer.environmental-science}"
)
public
String
environmental_science
;
public
void
Logoutput
(
String
science
)
{
if
(!(
"pro"
).
equals
(
environmental_science
))
{
System
.
out
.
println
(
science
);
}
else
{
System
.
out
.
println
(
""
);
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
// 关闭OSSClient。
ossClient
.
shutdown
();
}
static
class
GetObjectProgressListener
implements
ProgressListener
{
private
long
bytesRead
=
0
;
private
long
totalBytes
=
-
1
;
private
boolean
succeed
=
false
;
@Override
public
void
progressChanged
(
ProgressEvent
progressEvent
)
{
long
bytes
=
progressEvent
.
getBytes
();
ProgressEventType
eventType
=
progressEvent
.
getEventType
();
switch
(
eventType
)
{
case
TRANSFER_STARTED_EVENT:
System
.
out
.
println
(
"Start to download......"
);
break
;
case
RESPONSE_CONTENT_LENGTH_EVENT:
this
.
totalBytes
=
bytes
;
System
.
out
.
println
(
this
.
totalBytes
+
" bytes in total will be downloaded to a local file"
);
break
;
case
RESPONSE_BYTE_TRANSFER_EVENT:
this
.
bytesRead
+=
bytes
;
if
(
this
.
totalBytes
!=
-
1
)
{
int
percent
=
(
int
)
(
this
.
bytesRead
*
100.0
/
this
.
totalBytes
);
System
.
out
.
println
(
bytes
+
" bytes have been read at this time, download progress: "
+
percent
+
"%("
+
this
.
bytesRead
+
"/"
+
this
.
totalBytes
+
")"
);
}
else
{
System
.
out
.
println
(
bytes
+
" bytes have been read at this time, download ratio: unknown"
+
"("
+
this
.
bytesRead
+
"/...)"
);
}
break
;
case
TRANSFER_COMPLETED_EVENT:
this
.
succeed
=
true
;
System
.
out
.
println
(
"Succeed to download, "
+
this
.
bytesRead
+
" bytes have been transferred in total"
);
break
;
case
TRANSFER_FAILED_EVENT:
System
.
out
.
println
(
"Failed to download, "
+
this
.
bytesRead
+
" bytes have been transferred"
);
break
;
default
:
break
;
}
}
public
boolean
isSucceed
()
{
return
succeed
;
}
}
@Value
(
"${config-8timer.environmental-science}"
)
public
String
environmental_science
;
public
void
Logoutput
(
String
science
)
{
if
(!(
"pro"
).
equals
(
environmental_science
))
{
System
.
out
.
println
(
science
);
}
else
{
System
.
out
.
println
(
""
);
}
}
/**
* @Author wgd
* @Description 创建zip
* @Date 2021/12/2 18:06
**/
public
String
createZip
(
Map
<
String
,
String
>
urlMap
)
throws
Exception
{
DateFormat
dateFormat
=
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
);
String
fileName
=
dateFormat
.
format
(
new
Date
())
+
".zip"
;
FileOutputStream
fos
=
null
;
ZipOutputStream
zos
=
null
;
BufferedInputStream
inputStream
;
OSSObject
ossObject
;
String
Suffix
;
try
{
fos
=
new
FileOutputStream
(
zipPath
+
fileName
);
zos
=
new
ZipOutputStream
(
fos
);
OSS
ossClient
=
new
OSSClientBuilder
().
build
(
endpoint
,
accessKeyId
,
accessKeySecret
);
for
(
String
m
:
urlMap
.
keySet
())
{
Suffix
=
urlMap
.
get
(
m
).
substring
(
urlMap
.
get
(
m
).
lastIndexOf
(
"."
));
ossObject
=
ossClient
.
getObject
(
bucketName
,
urlMap
.
get
(
m
));
inputStream
=
new
BufferedInputStream
(
ossObject
.
getObjectContent
());
ZipEntry
entry
=
new
ZipEntry
(
m
+
Suffix
);
try
{
zos
.
putNextEntry
(
entry
);
int
nNumber
;
byte
[]
buffer
=
new
byte
[
4096
];
while
((
nNumber
=
inputStream
.
read
(
buffer
))
>
0
)
{
zos
.
write
(
buffer
,
0
,
nNumber
);
}
}
catch
(
IOException
e
)
{
throw
new
Exception
(
"写入失败"
);
//TODO 应作为自定义异常
}
finally
{
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
throw
new
Exception
(
"关闭失败"
);
//TODO 应作为自定义异常
}
}
}
ossClient
.
shutdown
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
finally
{
// 关闭创建的流对象
try
{
if
(
zos
!=
null
)
{
zos
.
closeEntry
();
zos
.
close
();
}
if
(
fos
!=
null
)
{
fos
.
close
();
}
}
catch
(
IOException
e
)
{
throw
new
Exception
(
"关闭失败"
);
//TODO 应作为自定义异常
}
}
return
fileName
;
}
}
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