From 0e449c4aba8b287ee9554c321cc88615667f1210 Mon Sep 17 00:00:00 2001 From: zhangyanli Date: Tue, 26 Mar 2024 15:32:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=99=E5=9F=B9=E6=A1=A3=E6=A1=88=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E8=AE=B0=E5=BD=95=E5=88=A0=E9=99=A4=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/FileUploadLogMapper.java | 73 ++++++++++ .../education/ArchivesPdfFileMapper.java | 30 ++++ .../education/ArchivesDelScheduled.java | 45 ++++++ .../education/ArchivesPdfFileService.java | 30 ++++ .../impl/ArchivesPdfFileServiceImpl.java | 47 +++++++ .../service/system/FileUploadLogService.java | 73 ++++++++++ .../system/impl/FileUploadLogServiceImpl.java | 97 +++++++++++++ src/main/java/com/zcloud/util/Smb.java | 118 ++++++++++++++++ .../datasource/system/FileUploadLogMapper.xml | 133 ++++++++++++++++++ .../dsno2/education/ArchivesPdfFileMapper.xml | 88 ++++++++++++ 10 files changed, 734 insertions(+) create mode 100644 src/main/java/com/zcloud/mapper/datasource/system/FileUploadLogMapper.java create mode 100644 src/main/java/com/zcloud/mapper/dsno2/education/ArchivesPdfFileMapper.java create mode 100644 src/main/java/com/zcloud/scheduled/education/ArchivesDelScheduled.java create mode 100644 src/main/java/com/zcloud/service/education/ArchivesPdfFileService.java create mode 100644 src/main/java/com/zcloud/service/education/impl/ArchivesPdfFileServiceImpl.java create mode 100644 src/main/java/com/zcloud/service/system/FileUploadLogService.java create mode 100644 src/main/java/com/zcloud/service/system/impl/FileUploadLogServiceImpl.java create mode 100644 src/main/java/com/zcloud/util/Smb.java create mode 100644 src/main/resources/mybatis/datasource/system/FileUploadLogMapper.xml create mode 100644 src/main/resources/mybatis/dsno2/education/ArchivesPdfFileMapper.xml diff --git a/src/main/java/com/zcloud/mapper/datasource/system/FileUploadLogMapper.java b/src/main/java/com/zcloud/mapper/datasource/system/FileUploadLogMapper.java new file mode 100644 index 0000000..471fa58 --- /dev/null +++ b/src/main/java/com/zcloud/mapper/datasource/system/FileUploadLogMapper.java @@ -0,0 +1,73 @@ +package com.zcloud.mapper.datasource.system; + +import com.zcloud.entity.Page; +import com.zcloud.entity.PageData; + +import java.util.List; + +/** + * 说明:附件上传日志 + * 作者:luoxiaobao + * 时间:2022-08-24 + * 官网:www.zcloudchina.com + */ +public interface FileUploadLogMapper { + + /** + * 新增 + * + * @param pd + * @throws Exception + */ + void save(PageData pd); + + /** + * 删除 + * + * @param pd + * @throws Exception + */ + void delete(PageData pd); + + /** + * 修改 + * + * @param pd + * @throws Exception + */ + void edit(PageData pd); + + /** + * 列表 + * + * @param page + * @throws Exception + */ + List datalistPage(Page page); + + /** + * 列表(全部) + * + * @param pd + * @throws Exception + */ + List listAll(PageData pd); + + /** + * 通过id获取数据 + * + * @param pd + * @throws Exception + */ + PageData findById(PageData pd); + + /** + * 批量删除 + * + * @param ArrayDATA_IDS + * @throws Exception + */ + void deleteAll(String[] ArrayDATA_IDS); + +} + diff --git a/src/main/java/com/zcloud/mapper/dsno2/education/ArchivesPdfFileMapper.java b/src/main/java/com/zcloud/mapper/dsno2/education/ArchivesPdfFileMapper.java new file mode 100644 index 0000000..e338769 --- /dev/null +++ b/src/main/java/com/zcloud/mapper/dsno2/education/ArchivesPdfFileMapper.java @@ -0,0 +1,30 @@ +package com.zcloud.mapper.dsno2.education; + +import com.zcloud.entity.Page; +import com.zcloud.entity.PageData; + +import java.util.List; + +/** + * 说明:档案文件 + * 作者:luoxiaobao + * 时间:2023-08-25 + * 官网:www.zcloudchina.com + */ +public interface ArchivesPdfFileMapper { + + + /**列表(N天之前) + * @param pd + * @throws Exception + */ + List listAllDaysAgo(PageData pd); + + /**删除 + * @param pd + * @throws Exception + */ + void deleteDaysAgo(PageData pd); + +} + diff --git a/src/main/java/com/zcloud/scheduled/education/ArchivesDelScheduled.java b/src/main/java/com/zcloud/scheduled/education/ArchivesDelScheduled.java new file mode 100644 index 0000000..6b1a8bc --- /dev/null +++ b/src/main/java/com/zcloud/scheduled/education/ArchivesDelScheduled.java @@ -0,0 +1,45 @@ +package com.zcloud.scheduled.education; + +import com.zcloud.entity.PageData; +import com.zcloud.service.education.ArchivesPdfFileService; +import com.zcloud.util.Smb; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * 档案文件删除(三天前的删除) + */ +@Component +public class ArchivesDelScheduled { + @Autowired + private ArchivesPdfFileService archivesPdfFileService; + @Autowired + private Smb smb; + + + @Scheduled(cron = "0 30 0 * * ?") // 每天晚上12:30执行 +// @Scheduled(cron ="0/30 * * * * ?") // 测试 每5分钟执行一次 + public void scheduled() { + try { + System.out.println("--------------------------开始删除档案文件-------------------------"); + try { + PageData pd = new PageData(); + pd.put("DAYNUM", 3); + List deleteList = archivesPdfFileService.listAllDaysAgo(pd); + if(deleteList != null && deleteList.size() > 0){ + for(PageData delete : deleteList){ + smb.deleteFile(delete.getString("FILE_PATH")); + archivesPdfFileService.deleteDaysAgo(delete); + } + } + } catch (Exception e){ + e.printStackTrace(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/zcloud/service/education/ArchivesPdfFileService.java b/src/main/java/com/zcloud/service/education/ArchivesPdfFileService.java new file mode 100644 index 0000000..9138488 --- /dev/null +++ b/src/main/java/com/zcloud/service/education/ArchivesPdfFileService.java @@ -0,0 +1,30 @@ +package com.zcloud.service.education; + +import com.zcloud.entity.Page; +import com.zcloud.entity.PageData; +import org.springframework.scheduling.annotation.Async; + +import java.util.List; + +/** + * 说明:档案文件 + * 作者:luoxiaobao + * 时间:2023-08-25 + * 官网:www.zcloudchina.com + */ +public interface ArchivesPdfFileService { + + /**列表(全部N天以前) + * @param pd + * @throws Exception + */ + List listAllDaysAgo(PageData pd)throws Exception; + + /**删除 + * @param pd + * @throws Exception + */ + void deleteDaysAgo(PageData pd)throws Exception; + +} + diff --git a/src/main/java/com/zcloud/service/education/impl/ArchivesPdfFileServiceImpl.java b/src/main/java/com/zcloud/service/education/impl/ArchivesPdfFileServiceImpl.java new file mode 100644 index 0000000..0bc0b48 --- /dev/null +++ b/src/main/java/com/zcloud/service/education/impl/ArchivesPdfFileServiceImpl.java @@ -0,0 +1,47 @@ +package com.zcloud.service.education.impl; + +import com.zcloud.entity.PageData; +import com.zcloud.mapper.dsno2.education.ArchivesPdfFileMapper; +import com.zcloud.service.education.ArchivesPdfFileService; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.*; + +/** + * 说明:档案文件 + * 作者:luoxiaobao + * 时间:2023-08-25 + * 官网:www.zcloudchina.com + */ +@Service +@Transactional //开启事物 +public class ArchivesPdfFileServiceImpl implements ArchivesPdfFileService { + + @Resource + private ArchivesPdfFileMapper archivespdffileMapper; + + + + /** + * 列表(N天前) + * + * @param pd + * @throws Exception + */ + public List listAllDaysAgo(PageData pd) throws Exception { + return archivespdffileMapper.listAllDaysAgo(pd); + } + + /** + * 删除 + * + * @param pd + * @throws Exception + */ + public void deleteDaysAgo(PageData pd) throws Exception { + archivespdffileMapper.deleteDaysAgo(pd); + } +} + diff --git a/src/main/java/com/zcloud/service/system/FileUploadLogService.java b/src/main/java/com/zcloud/service/system/FileUploadLogService.java new file mode 100644 index 0000000..254747d --- /dev/null +++ b/src/main/java/com/zcloud/service/system/FileUploadLogService.java @@ -0,0 +1,73 @@ +package com.zcloud.service.system; + +import com.zcloud.entity.Page; +import com.zcloud.entity.PageData; + +import java.util.List; + +/** + * 说明:附件上传日志 + * 作者:luoxiaobao + * 时间:2022-08-24 + * 官网:www.zcloudchina.com + */ +public interface FileUploadLogService { + + /** + * 新增 + * + * @param pd + * @throws Exception + */ + public void save(PageData pd) throws Exception; + + /** + * 删除 + * + * @param pd + * @throws Exception + */ + public void delete(PageData pd) throws Exception; + + /** + * 修改 + * + * @param pd + * @throws Exception + */ + public void edit(PageData pd) throws Exception; + + /** + * 列表 + * + * @param page + * @throws Exception + */ + public List list(Page page) throws Exception; + + /** + * 列表(全部) + * + * @param pd + * @throws Exception + */ + public List listAll(PageData pd) throws Exception; + + /** + * 通过id获取数据 + * + * @param pd + * @throws Exception + */ + public PageData findById(PageData pd) throws Exception; + + /** + * 批量删除 + * + * @param ArrayDATA_IDS + * @throws Exception + */ + public void deleteAll(String[] ArrayDATA_IDS) throws Exception; + +} + diff --git a/src/main/java/com/zcloud/service/system/impl/FileUploadLogServiceImpl.java b/src/main/java/com/zcloud/service/system/impl/FileUploadLogServiceImpl.java new file mode 100644 index 0000000..95b1529 --- /dev/null +++ b/src/main/java/com/zcloud/service/system/impl/FileUploadLogServiceImpl.java @@ -0,0 +1,97 @@ +package com.zcloud.service.system.impl; + +import com.zcloud.entity.Page; +import com.zcloud.entity.PageData; +import com.zcloud.mapper.datasource.system.FileUploadLogMapper; +import com.zcloud.service.system.FileUploadLogService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +/** + * 说明:附件上传日志 + * 作者:luoxiaobao + * 时间:2022-08-24 + * 官网:www.zcloudchina.com + */ +@Service +@Transactional //开启事物 +public class FileUploadLogServiceImpl implements FileUploadLogService { + + @Autowired + private FileUploadLogMapper fileuploadlogMapper; + + /** + * 新增 + * + * @param pd + * @throws Exception + */ + public void save(PageData pd) throws Exception { + fileuploadlogMapper.save(pd); + } + + /** + * 删除 + * + * @param pd + * @throws Exception + */ + public void delete(PageData pd) throws Exception { + fileuploadlogMapper.delete(pd); + } + + /** + * 修改 + * + * @param pd + * @throws Exception + */ + public void edit(PageData pd) throws Exception { + fileuploadlogMapper.edit(pd); + } + + /** + * 列表 + * + * @param page + * @throws Exception + */ + public List list(Page page) throws Exception { + return fileuploadlogMapper.datalistPage(page); + } + + /** + * 列表(全部) + * + * @param pd + * @throws Exception + */ + public List listAll(PageData pd) throws Exception { + return fileuploadlogMapper.listAll(pd); + } + + /** + * 通过id获取数据 + * + * @param pd + * @throws Exception + */ + public PageData findById(PageData pd) throws Exception { + return fileuploadlogMapper.findById(pd); + } + + /** + * 批量删除 + * + * @param ArrayDATA_IDS + * @throws Exception + */ + public void deleteAll(String[] ArrayDATA_IDS) throws Exception { + fileuploadlogMapper.deleteAll(ArrayDATA_IDS); + } + +} + diff --git a/src/main/java/com/zcloud/util/Smb.java b/src/main/java/com/zcloud/util/Smb.java new file mode 100644 index 0000000..748add2 --- /dev/null +++ b/src/main/java/com/zcloud/util/Smb.java @@ -0,0 +1,118 @@ +package com.zcloud.util; + +import com.aliyun.oss.ClientException; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; +import com.aliyun.oss.OSSException; +import com.aliyun.oss.model.OSSObject; +import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.JSchException; +import com.jcraft.jsch.SftpATTRS; +import com.jcraft.jsch.SftpException; +import com.zcloud.entity.PageData; +import com.zcloud.service.system.FileUploadLogService; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import java.io.*; +import java.util.Date; +import java.util.Map; + +@Component +public class Smb { + @Autowired + private FileUploadLogService fileUploadLogService; + + private static String endpoint = "https://oss-cn-zhangjiakou.aliyuncs.com"; + // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。 + private static String accessKeyId = "LTAI5tK134ZzXPEwykAdpVn2"; + private static String accessKeySecret = "XCEMY8FG52cXImFMeIiH4tDJ9BIN3N"; + // 填写Bucket名称。 + private static String bucketName = "qyag"; + + /** + * 删除文件 + * + * @throws SftpException + * @throws JSchException + */ + public void deleteFile(String directoryFile) throws Exception { + + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + + try { + // 删除文件或目录。如果要删除目录,目录必须为空。 + ossClient.deleteObject(bucketName, "YTHFile" + directoryFile); + } catch (OSSException oe) { + PageData pd = new PageData(); + pd.put("FILE_PATH", directoryFile); + fileUploadLogService.delete(pd); + System.out.println("Caught an OSSException, which means your request made it to OSS, " + + "but was rejected with an error response for some reason."); + System.out.println("Error Message:" + oe.getErrorMessage()); + System.out.println("Error Code:" + oe.getErrorCode()); + System.out.println("Request ID:" + oe.getRequestId()); + System.out.println("Host ID:" + oe.getHostId()); + } catch (ClientException ce) { + System.out.println("Caught an ClientException, which means the client encountered " + + "a serious internal problem while trying to communicate with OSS, " + + "such as not being able to access the network."); + System.out.println("Error Message:" + ce.getMessage()); + } finally { + if (ossClient != null) { + ossClient.shutdown(); + } + } + + } + + public static void main(String[] args) { + // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。 + String objectName = "exampledir/exampleobject.txt"; + + // 创建OSSClient实例。 + OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); + + try { + // 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。 + OSSObject ossObject = ossClient.getObject(bucketName, objectName); + // 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。 + InputStream content = ossObject.getObjectContent(); + if (content != null) { + BufferedReader reader = new BufferedReader(new InputStreamReader(content)); + while (true) { + String line = reader.readLine(); + if (line == null) break; + System.out.println("\n" + line); + } + // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。 + content.close(); + } + } catch (OSSException oe) { + System.out.println("Caught an OSSException, which means your request made it to OSS, " + + "but was rejected with an error response for some reason."); + System.out.println("Error Message:" + oe.getErrorMessage()); + System.out.println("Error Code:" + oe.getErrorCode()); + System.out.println("Request ID:" + oe.getRequestId()); + System.out.println("Host ID:" + oe.getHostId()); + } catch (ClientException ce) { + System.out.println("Caught an ClientException, which means the client encountered " + + "a serious internal problem while trying to communicate with OSS, " + + "such as not being able to access the network."); + System.out.println("Error Message:" + ce.getMessage()); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (ossClient != null) { + ossClient.shutdown(); + } + } + } +} + + diff --git a/src/main/resources/mybatis/datasource/system/FileUploadLogMapper.xml b/src/main/resources/mybatis/datasource/system/FileUploadLogMapper.xml new file mode 100644 index 0000000..0878847 --- /dev/null +++ b/src/main/resources/mybatis/datasource/system/FileUploadLogMapper.xml @@ -0,0 +1,133 @@ + + + + + + + BUS_FILEUPLOADLOG + + + + + SYS_DICTIONARIES + + + + + f + . + CORPINFO_ID + , + f.CREATTIME, + f.FILE_PATH, + f.FILE_SIZE, + f.FILEUPLOADLOG_ID + + + + + CORPINFO_ID + , + CREATTIME, + FILE_PATH, + FILE_SIZE, + FILEUPLOADLOG_ID + + + + + #{CORPINFO_ID} + , + #{CREATTIME}, + #{FILE_PATH}, + #{FILE_SIZE}, + #{FILEUPLOADLOG_ID} + + + + + insert into + + ( + + ) values ( + + ) + + + + + delete from + + where FILE_PATH = #{FILE_PATH} + + + + + update + + set + CORPINFO_ID = #{CORPINFO_ID}, + CREATTIME = #{CREATTIME}, + FILE_PATH = #{FILE_PATH}, + FILE_SIZE = #{FILE_SIZE}, + FILEUPLOADLOG_ID = FILEUPLOADLOG_ID + where + FILEUPLOADLOG_ID = #{FILEUPLOADLOG_ID} + + + + + + + + + + + + + + update + + set + ISDELETE = '1' + where + FILEUPLOADLOG_ID in + + #{item} + + + + diff --git a/src/main/resources/mybatis/dsno2/education/ArchivesPdfFileMapper.xml b/src/main/resources/mybatis/dsno2/education/ArchivesPdfFileMapper.xml new file mode 100644 index 0000000..1fc711e --- /dev/null +++ b/src/main/resources/mybatis/dsno2/education/ArchivesPdfFileMapper.xml @@ -0,0 +1,88 @@ + + + + + + + BUS_ARCHIVES_PDF_FILE + + + + + SYS_DICTIONARIES + + + + + f.TABLE_NAME, + f.TABLE_ID, + f.FILE_PATH, + f.TYPE, + f.STATUS, + f.ERRORMSG, + f.CREATOR, + f.CREATTIME, + f.ARCHIVES_PDF_FILE_ID, + f.DESCR, + f.METHOD_NAME, + f.CLASS_NAME, + f.PARAM_NAME + + + + + TABLE_NAME, + TABLE_ID, + FILE_PATH, + TYPE, + STATUS, + ERRORMSG, + CREATOR, + CREATTIME, + ARCHIVES_PDF_FILE_ID, + DESCR, + METHOD_NAME, + CLASS_NAME, + PARAM_NAME + + + + + #{TABLE_NAME}, + #{TABLE_ID}, + #{FILE_PATH}, + #{TYPE}, + #{STATUS}, + #{ERRORMSG}, + #{CREATOR}, + #{CREATTIME}, + #{ARCHIVES_PDF_FILE_ID}, + #{DESCR}, + #{METHOD_NAME}, + #{CLASS_NAME}, + #{PARAM_NAME} + + + + + + + + + + update + + set + STATUS = '-2' + where + ARCHIVES_PDF_FILE_ID = #{ARCHIVES_PDF_FILE_ID} + +