Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.cesgroup.bdc.cis.controller;
import com.cesgroup.bdc.cis.service.CisService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;
/**
* @author: YYB
* @description: CIS文件加密控制类
* @date: 2024/2/8.
* @modified by:
*/
@Slf4j
@Controller
@RequestMapping("/cis")
public class CisController {
@Autowired
private CisService cisService;
@Value("${file.upload.path}")
private String fileBasePath;
/**
* 目录(文件)加密调试
* @param dir 目录(文件)路径 如:20240304 或者 20240304/123.ofd
* @param request
* @return
*/
@RequestMapping("/encrypted/{dir}/**")
@ResponseBody
public Map<String, String> encrypted(@PathVariable String dir, HttpServletRequest request) {
Map<String, String> mm = new HashMap<>();
boolean success = false;
String path =
request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
path = path.replace("/cis/encrypted/","");
path = path.replace("\\","/");
String filePath = fileBasePath + path;
List<String> failFiles = new ArrayList<>();
this.recursiveEncrypted(new File(filePath),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 file 文件
* @param failFiles 加密错误文件路径信息
*/
private void recursiveEncrypted(File file,List<String> failFiles){
if (file != null && file.exists()) {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
recursiveEncrypted(child,failFiles); // 递归调用自身处理子目录
}
} else {
// System.out.println("文件名:"+file.getName());
boolean success = cisService.encryptedFile(file);
if(!success){
failFiles.add(file.getAbsolutePath());
}
}
}
}
/**
* 目录(文件)解密调试
* @param dir 目录(文件)路径 如:jmdir/20240304 或者 jmdir/20240304/123.ofd
* @param request
* @return
*/
@RequestMapping("/decrypted/{dir}/**")
@ResponseBody
public Map<String, String> decrypted(@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/decrypted/","");
path = path.replace("\\","/");
String filePath = fileBasePath + path;
List<String> failFiles = new ArrayList<>();
this.recursiveDecrypted(new File(filePath),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 file 已加密文件
* @param failFiles
*/
private void recursiveDecrypted(File file,List<String> failFiles){
if (file != null && file.exists()) {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
recursiveDecrypted(child,failFiles); // 递归调用自身处理子目录
}
} else {
// System.out.println("文件名:"+file.getName());
boolean success = cisService.decryptedFile(file);
if(!success){
failFiles.add(file.getAbsolutePath());
}
}
}
}
}