思路
这里就不说怎么弄pgp的公钥私钥如何生成了
--- 5个路径 思路清晰
--- 公钥key publicKey
--- 私钥key privateKey
--- 加密好的文件 filePath
--- 备份文件 upFilePath
--- 解密后的文件 untieFilePath
--- 还会有密码 passwrod
标题好吧直接上代码吧不罗嗦了
// 看怎么去执行了 这里做一个定时任务job
// 去自己扫描文件夹是否有要解密的文件
// @Scheduled 定时任务的注解 cron 表达式 这里是30秒走一次 不知道可以自己搜一下了解一下
@Scheduled(cron = "30 * * * * ?")
public void fileDecryptTask(){
try{
log.info("-------------------开始执行定时任务:PGP文件解密------------------------");
Collection<File> files = FileUtils.listFiles(new
File(filePath), new String[]{"pgp"}, true);
for (File f : files) {
decryptPgpFile(f.getAbsolutePath(), untieFilePath, upFilePath,
privateKey,passwrod);
}
log.info("-------------------结束执行定时任务:PGP文件解密------------------------");
}catch (Exception e) {
log.error("定时解析加密文件出现错误", e);
}
}
public static String decryptPgpFile(String filePath, String untieFilePath, String upFilePath,
String privateKeyPath, String password) {
try {
File file = new File(filePath);
String fileName = file.getName();
String resultPathNew = untieFilePath;
String name = fileName.substring(0, fileName.indexOf(".")) + ".csv";
log.info("生成文件NAME---------------:{}",name);
String resultPath = untieFilePath+ name;
Security.addProvider(new BouncyCastleProvider());
KeyBasedLargeFileProcessor.decryptFile(file.getAbsolutePath(),
privateKeyPath,
password.toCharArray(),
resultPathNew,
name);
FileUtil.copyFile(file.getAbsolutePath(), upFilePath+ fileName);
log.info("已复制解析文件到已读文件夹中 [{}]", upFilePath+ fileName);
FileUtil.delete(file.getAbsolutePath());
log.info("已删除已读文件");
return resultPath;
} catch (Exception e) {
log.error("解密文件出现异常", e);
}
return null;
}
FileUtil 里的方法
/**
* 删除文件或文件夹
*
* @param filePath
*/
public static void delete(String filePath) {
try {
File file = new File(filePath);
if (file.exists()){
if(file.isDirectory()){
FileUtils.deleteDirectory(file);
}else{
file.delete();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 复制
public static void copyFile(String srcFile,String destFile){
try {
if(FileUtil.exist(srcFile)){
FileUtils.copyFile(new File(srcFile),new File( destFile ));
}
} catch (IOException e) {
e.printStackTrace();
}
}
文件下载
KeyBasedLargeFileProcessor 类
点击连接
提取码:tyy7
jar 包
点击连接
提取码:tyy7