Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sfw
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
杨郁彬
sfw
Commits
0a1a0e7e
Commit
0a1a0e7e
authored
Apr 26, 2024
by
杨郁彬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增删除明文文件目录的接口及定时任务删除超过1年的明文文件目录
parent
493a25b0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
105 additions
and
0 deletions
+105
-0
CisController.java
...n/java/com/cesgroup/bdc/cis/controller/CisController.java
+105
-0
No files found.
src/main/java/com/cesgroup/bdc/cis/controller/CisController.java
View file @
0a1a0e7e
package
com
.
cesgroup
.
bdc
.
cis
.
controller
;
package
com
.
cesgroup
.
bdc
.
cis
.
controller
;
import
cn.hutool.core.io.FileUtil
;
import
com.cesgroup.bdc.cis.service.CisService
;
import
com.cesgroup.bdc.cis.service.CisService
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
...
@@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
...
@@ -13,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import
org.springframework.web.servlet.HandlerMapping
;
import
org.springframework.web.servlet.HandlerMapping
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.File
;
import
java.io.File
;
import
java.time.YearMonth
;
import
java.util.*
;
import
java.util.*
;
/**
/**
...
@@ -25,6 +30,7 @@ import java.util.*;
...
@@ -25,6 +30,7 @@ import java.util.*;
@Controller
@Controller
@RequestMapping
(
"/cis"
)
@RequestMapping
(
"/cis"
)
public
class
CisController
{
public
class
CisController
{
private
Log
logger
=
LogFactory
.
getLog
(
getClass
());
@Autowired
@Autowired
private
CisService
cisService
;
private
CisService
cisService
;
...
@@ -146,4 +152,103 @@ public class CisController {
...
@@ -146,4 +152,103 @@ public class CisController {
}
}
}
}
}
}
/**
* 删除明文文件
*
* @param dir 明文文件路径 多个用-分隔
* @param request
* @return
*/
@RequestMapping
(
"/removePlainFiles/{dir}/**"
)
@ResponseBody
public
Map
<
String
,
String
>
removePlainFiles
(
@PathVariable
String
dir
,
HttpServletRequest
request
)
{
Map
<
String
,
String
>
mm
=
new
HashMap
<>();
String
path
=
request
.
getAttribute
(
HandlerMapping
.
PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
).
toString
();
path
=
path
.
replace
(
"/cis/removePlainFiles/"
,
""
);
path
=
path
.
replace
(
"\\"
,
"/"
);
String
[]
filePaths
=
path
.
split
(
"-"
);
List
<
String
>
failFiles
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
filePaths
.
length
;
i
++)
{
StringBuffer
filePath
=
new
StringBuffer
(
""
);
filePath
.
append
(
"fileBasePath"
).
append
(
filePaths
[
i
]);
this
.
recursiveRemove
(
filePath
.
toString
(),
failFiles
);
}
if
(
failFiles
.
size
()
>
0
)
{
mm
.
put
(
"status"
,
"fail"
);
ObjectMapper
objectMapper
=
new
ObjectMapper
();
String
jsonString
=
null
;
try
{
jsonString
=
objectMapper
.
writeValueAsString
(
failFiles
);
}
catch
(
JsonProcessingException
e
)
{
e
.
printStackTrace
();
}
mm
.
put
(
"failFiles"
,
jsonString
);
}
else
{
mm
.
put
(
"status"
,
"success"
);
}
return
mm
;
}
/**
* 删除文件目录
*
* @param filePath
* @param failFiles
*/
private
void
recursiveRemove
(
String
filePath
,
List
<
String
>
failFiles
)
{
boolean
success
=
FileUtil
.
del
(
filePath
);
if
(!
success
)
{
failFiles
.
add
(
filePath
);
}
}
/**
* 清理不是最近一年的明文文件目录
*/
@Scheduled
(
cron
=
"00 00 22 1 * ?"
)
//每月1号22:00:00执行
public
void
removeUnLastYearPlainFileDir
()
{
logger
.
info
(
"开始清理不是最近一年的明文文件目录"
);
List
<
String
>
lastYearDirs
=
new
ArrayList
<>();
YearMonth
currentYearMonth
=
YearMonth
.
now
();
YearMonth
previousYear
=
currentYearMonth
.
minusYears
(
1
);
for
(
int
i
=
1
;
i
<
13
;
i
++)
{
YearMonth
yearMonth
=
previousYear
.
plusMonths
(
i
);
int
month
=
yearMonth
.
getMonth
().
getValue
();
StringBuffer
sb
=
new
StringBuffer
();
sb
.
append
(
yearMonth
.
getYear
());
if
(
month
>=
1
&&
month
<=
9
)
{
sb
.
append
(
"0"
).
append
(
month
);
}
else
{
sb
.
append
(
month
);
}
lastYearDirs
.
add
(
sb
.
toString
());
}
List
<
String
>
needRemoveDirs
=
new
ArrayList
<>();
File
rootPath
=
new
File
(
fileBasePath
);
File
[]
listFiles
=
rootPath
.
listFiles
();
for
(
int
i
=
0
;
i
<
listFiles
.
length
;
i
++){
if
(
listFiles
[
i
].
isDirectory
()){
String
dirName
=
listFiles
[
i
].
getName
();
if
(
dirName
.
matches
(
"\\d{6}"
)
&&
!
lastYearDirs
.
contains
(
dirName
)){
StringBuffer
filePath
=
new
StringBuffer
();
filePath
.
append
(
fileBasePath
).
append
(
dirName
);
needRemoveDirs
.
add
(
filePath
.
toString
());
}
}
}
List
<
String
>
failFiles
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
needRemoveDirs
.
size
();
i
++)
{
this
.
recursiveRemove
(
needRemoveDirs
.
get
(
i
),
failFiles
);
}
if
(
failFiles
.
size
()
>
0
)
{
for
(
int
i
=
0
;
i
<
failFiles
.
size
();
i
++)
{
logger
.
error
(
"目录["
+
failFiles
.
get
(
i
)+
"]"
+
"删除失败"
);
}
}
logger
.
info
(
"结束清理不是最近一年的明文文件目录"
);
}
}
}
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