|
@@ -1,11 +1,10 @@
|
|
|
package com.ssj.sys.controller;
|
|
|
|
|
|
-import java.io.BufferedInputStream;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStream;
|
|
|
-import java.io.OutputStream;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLDecoder;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
@@ -17,6 +16,13 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
+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.DeleteObjectsRequest;
|
|
|
+import com.aliyun.oss.model.DeleteObjectsResult;
|
|
|
import com.ssj.bean.sys.fx.domain.Merchant;
|
|
|
import com.ssj.bean.weixin.libmy.domain.*;
|
|
|
import com.ssj.service.sys.fx.service.MerchantService;
|
|
@@ -1491,4 +1497,175 @@ public class SysLibController extends BaseController {
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 请求删除2021年前的文件,这个2021年写死。
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/admin/delFile", method = RequestMethod.POST)
|
|
|
+ @ResponseBody
|
|
|
+ public Response delFile() {
|
|
|
+ Response response = new Response();
|
|
|
+ try{
|
|
|
+ //进来写入到redis
|
|
|
+ String delState = tokenManager.getString("del_oss_file");
|
|
|
+ if(delState == null){
|
|
|
+ tokenManager.set("del_oss_file","1");
|
|
|
+ //另起线程去处理删除
|
|
|
+ ThreadUtil.execAsync(() -> {
|
|
|
+ /**
|
|
|
+ * 查询2021年前的作业,数据量大
|
|
|
+ * 先查询最早一个月的数据,然后每次加一个月份查询数据做处理,知道2021年1月。
|
|
|
+ */
|
|
|
+ // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
|
|
+ String endpoint = "https://oss-cn-shenzhen-internal.aliyuncs.com";
|
|
|
+ // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
|
|
|
+ String accessKeyId = tokenManager.getString("sts_accessKeyId");
|
|
|
+ String accessKeySecret = tokenManager.getString("sts_accessKeySecret");
|
|
|
+ // 填写Bucket名称,例如examplebucket。
|
|
|
+ String bucketName = tokenManager.getString("bucketName");
|
|
|
+ String fileSavePath = PropertiesUtil.getValue("file_save_path");
|
|
|
+ // 创建OSSClient实例。
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
+ try {
|
|
|
+ String startMonth = zuoybCourseService.findFirstMonth();
|
|
|
+ LocalDateTime localDate=LocalDateTime.parse(startMonth, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+ LocalDate localDate2 = localDate.toLocalDate();
|
|
|
+ //截止年月日,取固定值20210101 删除2021年前学生作业图片
|
|
|
+ LocalDate localDate1 = LocalDate.parse("20210101", DateTimeFormatter.BASIC_ISO_DATE);
|
|
|
+ System.out.println("日期比较"+localDate1.compareTo(localDate2));
|
|
|
+ while (localDate1.compareTo(localDate2)>0){
|
|
|
+ String format = localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM"));
|
|
|
+ //查询该月份的作业
|
|
|
+ List<String> fileKeyList = zuoybCourseService.findFileKeyList(format);
|
|
|
+ if(fileKeyList==null || fileKeyList.size()==0){
|
|
|
+ localDate2 = localDate2.plusMonths(1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ int fileSize = fileKeyList.size();
|
|
|
+ // 删除文件。批量删除最多一次1000个。这里一次删除500
|
|
|
+ int pageSize = 500;
|
|
|
+ int pageCount = fileSize/pageSize;
|
|
|
+ int leftNum = fileSize%pageSize;
|
|
|
+ for(int i = 0;i<pageCount;i++){
|
|
|
+ List<String> list2 = new ArrayList<>();
|
|
|
+ list2.addAll(fileKeyList.subList(i*pageSize,(i+1)*pageSize));
|
|
|
+ // 填写需要删除的多个文件完整路径。文件完整路径中不能包含Bucket名称。
|
|
|
+ DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(list2).withEncodingType("url"));
|
|
|
+ //这里没有查看删除返回的结果集了。List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
|
|
|
+ //删除本地文件
|
|
|
+ for(String str :list2){
|
|
|
+ File file = new File(fileSavePath+"/"+str);
|
|
|
+ if(file.exists()){
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ list2.clear();
|
|
|
+ }
|
|
|
+ if(leftNum>0){
|
|
|
+ List<String> list3 = new ArrayList<>();
|
|
|
+ list3.addAll(fileKeyList.subList(pageCount*pageSize,fileSize));
|
|
|
+ // 填写需要删除的多个文件完整路径。文件完整路径中不能包含Bucket名称。
|
|
|
+ DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(list3).withEncodingType("url"));
|
|
|
+ //删除本地文件
|
|
|
+ for(String str :list3){
|
|
|
+ File file = new File(fileSavePath+"/"+str);
|
|
|
+ if(file.exists()){
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ list3.clear();
|
|
|
+ }
|
|
|
+ fileKeyList.clear();
|
|
|
+
|
|
|
+ localDate2 = localDate2.plusMonths(1);
|
|
|
+ }
|
|
|
+ }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 (Exception e){
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ response.failure("请求失败");
|
|
|
+ } finally {
|
|
|
+ if (ossClient != null) {
|
|
|
+ ossClient.shutdown();
|
|
|
+ System.out.println("----删除成功----");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ response.success();
|
|
|
+ }catch (Exception e){
|
|
|
+ logger.error(e.getMessage());
|
|
|
+ response.failure("请求失败");
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ LocalDateTime localDate=LocalDateTime.parse("2018-06-13 16:05:26", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+
|
|
|
+// System.out.println(localDate);
|
|
|
+ LocalDate localDate2 = localDate.toLocalDate();
|
|
|
+ LocalDate localDate1 = LocalDate.parse("20210101", DateTimeFormatter.BASIC_ISO_DATE);
|
|
|
+
|
|
|
+ while (localDate1.compareTo(localDate2)>0){
|
|
|
+// System.out.println(localDate1.compareTo(localDate2));
|
|
|
+ String format = localDate2.format(DateTimeFormatter.ofPattern("yyyy-MM"));
|
|
|
+ System.out.println(format);
|
|
|
+ localDate2 = localDate2.plusMonths(1);
|
|
|
+ }
|
|
|
+// int j =980;
|
|
|
+// System.out.println(980/50);
|
|
|
+// System.out.println(980%50);
|
|
|
+// for(int i=1;i<=19;i++){
|
|
|
+// System.out.println(19*50);
|
|
|
+// }
|
|
|
+// List<String> list = new ArrayList<>();
|
|
|
+// list.add("1");
|
|
|
+// list.add("2");
|
|
|
+// list.add("3");
|
|
|
+// list.add("4");
|
|
|
+// list.add("5");
|
|
|
+// list.add("6");
|
|
|
+// list.add("7");
|
|
|
+// list.add("8");
|
|
|
+// list.add("9");
|
|
|
+// list.add("10");
|
|
|
+// list.add("11");
|
|
|
+// list.add("12");
|
|
|
+// list.add("13");
|
|
|
+// list.add("14");
|
|
|
+// int pageSize = 3;
|
|
|
+// int pageCount = list.size()/pageSize;
|
|
|
+// int leftNum = list.size()%pageSize;
|
|
|
+// for(int i = 0;i<pageCount;i++){
|
|
|
+// List<String> list2 = new ArrayList<>();
|
|
|
+// list2.addAll(list.subList(i*pageSize,(i+1)*pageSize));
|
|
|
+// for (String str:list2){
|
|
|
+// System.out.println(str);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// if(leftNum>0){
|
|
|
+// List<String> list3 = new ArrayList<>();
|
|
|
+// list3.addAll(list.subList(pageCount*pageSize,list.size()));
|
|
|
+// for (String str:list3){
|
|
|
+// System.out.println(str);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
}
|