:art:完善程序功能

This commit is contained in:
hello-hao
2021-10-29 18:25:53 +08:00
parent 8c0bef5127
commit 8390f355a8
14 changed files with 173 additions and 449 deletions
@@ -17,6 +17,8 @@ public interface ImgMapper {
Integer deleimg(@Param("id") Integer id);
Integer deleimgForImgUid(@Param("imguid") String imguid);
Images selectByPrimaryKey(@Param("id") Integer id);
Integer counts(@Param("userid") Integer userid);
@@ -1,20 +0,0 @@
package cn.hellohao.quartz;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
@EnableScheduling
@Component
public class FirstJob{
public void task(){
//System.out.println("任务1执行....");
TempImgTask tempImgTask = new TempImgTask();
try {
tempImgTask.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@@ -1,88 +0,0 @@
package cn.hellohao.quartz;
import cn.hellohao.quartz.FirstJob;
import cn.hellohao.quartz.SecondJob;
import cn.hellohao.quartz.SpringUtil;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
@Configuration
public class QuartzConfigration {
//直接读取properties文件的值
// @Value("${Expression}")
private String Expression = "0 0 1 1 * ?";
// 配置触发器1
@Bean(name = "firstTrigger")
public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(firstJobDetail);
// 设置任务启动延迟
trigger.setStartDelay(0);
// 每5秒执行一次
trigger.setRepeatInterval(3600000);//一小时
return trigger;
}
// 配置定时任务1
@Bean(name = "firstJobDetail")
public MethodInvokingJobDetailFactoryBean firstJobDetail() {
FirstJob firstJob = (FirstJob) SpringUtil.getBean(FirstJob.class);
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
// 是否并发执行
jobDetail.setConcurrent(false);
// 为需要执行的实体类对应的对象
jobDetail.setTargetObject(firstJob);
// 需要执行的方法
jobDetail.setTargetMethod("task");
return jobDetail;
}
// 配置触发器2
@Bean(name = "secondTrigger")
public CronTriggerFactoryBean secondTrigger(JobDetail secondJobDetail) {
CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
trigger.setJobDetail(secondJobDetail);
// cron表达式
trigger.setCronExpression(Expression);
//trigger.setCronExpression("0 0/1 * * * ?");
return trigger;
}
// 配置定时任务2
@Bean(name = "secondJobDetail")
public MethodInvokingJobDetailFactoryBean secondJobDetail() {
SecondJob secondJob = (SecondJob) SpringUtil.getBean(SecondJob.class);
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
// 是否并发执行
jobDetail.setConcurrent(false);
// 为需要执行的实体类对应的对象
jobDetail.setTargetObject(secondJob);
// 需要执行的方法
jobDetail.setTargetMethod("task");
return jobDetail;
}
// 配置Scheduler
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger, Trigger secondTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 延时启动,应用启动1秒后
bean.setStartupDelay(1);
// 注册触发器
bean.setTriggers(firstTrigger, secondTrigger);
return bean;
}
}
@@ -0,0 +1,49 @@
package cn.hellohao.quartz;
import cn.hellohao.quartz.job.FirstJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;
@Configuration
public class QuartzConfiguration {
// 配置定时任务1
@Bean(name = "firstJobDetail")
public MethodInvokingJobDetailFactoryBean firstJobDetail(FirstJob firstJob) {
MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
// 是否并发执行
jobDetail.setConcurrent(true);
// 为需要执行的实体类对应的对象
jobDetail.setTargetObject(firstJob);
// 需要执行的方法
jobDetail.setTargetMethod("task");
return jobDetail;
}
// 配置触发器1
@Bean(name = "firstTrigger")
public SimpleTriggerFactoryBean firstTrigger(JobDetail firstJobDetail) {
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(firstJobDetail);
// 设置任务启动延迟
trigger.setStartDelay(0);
// 单位毫秒 60000是一分钟
trigger.setRepeatInterval(60000);
return trigger;
}
@Bean(name = "scheduler")
public SchedulerFactoryBean schedulerFactory(Trigger firstTrigger, Trigger secondTrigger) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
// 延时启动,应用启动5秒后
bean.setStartupDelay(5);
// 注册触发器
bean.setTriggers(firstTrigger, secondTrigger);
return bean;
}
}
@@ -1,134 +0,0 @@
package cn.hellohao.quartz;
import cn.hellohao.pojo.Images;
import cn.hellohao.pojo.Imgreview;
import cn.hellohao.pojo.Keys;
import cn.hellohao.service.impl.ImgServiceImpl;
import cn.hellohao.service.impl.ImgreviewServiceImpl;
import cn.hellohao.service.impl.KeysServiceImpl;
import cn.hellohao.service.impl.NOSImageupload;
import cn.hellohao.utils.LocUpdateImg;
import cn.hellohao.utils.SpringContextHolder;
import com.alibaba.fastjson.JSON;
import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/*
单任务执行,并且通过控制器的接口实现时间间隔的动态修改
任务类
* */
//@Configuration
@Component
public class SchedulerTask {
@Autowired
private ImgServiceImpl imgService;
@Autowired
private ImgreviewServiceImpl imgreviewService;
@Autowired
private KeysServiceImpl keysService;
private static AipContentCensor client;
private static SchedulerTask schedulerTask;
@PostConstruct
public void init() {
schedulerTask = this; }
private static void Initializes(Imgreview imgreview) {
client = new AipContentCensor(imgreview.getAppId(), imgreview.getApiKey(), imgreview.getSecretKey());
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
}
public void start(){
//查询鉴黄key
Imgreview imgreview = schedulerTask.imgreviewService.selectByPrimaryKey(1);
if(imgreview.getUsing()==1){
//初始化鉴黄
SchedulerTask.Initializes(imgreview);
//计算出昨天的日期
Date d = new Date();
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
String oldtime = df.format(new Date(d.getTime() - 1 * 24 * 60 * 60 * 1000));
//服务器获取出这个时间段的值然后遍历
List<Images> imglist= schedulerTask.imgService.gettimeimg(oldtime);
for (Images images : imglist) {
String imgurl = images.getImgurl();
System.err.println("正在鉴定的图片:" + imgurl);
JSONObject res = client.antiPorn(imgurl);
res = client.imageCensorUserDefined(imgurl, EImgType.URL, null);
System.err.println("返回的鉴黄json:"+res.toString());
com.alibaba.fastjson.JSONArray jsonArray = JSON.parseArray("[" + res.toString() + "]");
for (Object o : jsonArray) {
com.alibaba.fastjson.JSONObject jsonObject = (com.alibaba.fastjson.JSONObject) o;
com.alibaba.fastjson.JSONArray data = jsonObject.getJSONArray("data");
Integer conclusionType = jsonObject.getInteger("conclusionType");
if (conclusionType == 2) {
for (Object datum : data) {
com.alibaba.fastjson.JSONObject imgdata = (com.alibaba.fastjson.JSONObject) datum;
if (imgdata.getInteger("type") == 1) {
//标记图片
Integer ret = schedulerTask.imgService.deleimgname(images.getImgname()); //删除数据库图片信息
//Imgreview imgreview1 =imgreviewService.selectByPrimaryKey(1);
Imgreview imgreview1 = new Imgreview();
imgreview1.setId(1);
Integer count = imgreview.getCount();
System.out.println("违法图片总数:" + count);
imgreview1.setCount(count + 1);
schedulerTask.imgreviewService.updateByPrimaryKeySelective(imgreview1);
Keys key =null;
if(images.getSource()==1){
key =schedulerTask.keysService.selectKeys(images.getSource());
schedulerTask.imgService.delect(key, images.getImgname());
}else if (images.getSource()==2){
key = schedulerTask.keysService.selectKeys(images.getSource());
schedulerTask.imgService.delectOSS(key, images.getImgname());
}else if(images.getSource()==3){
key = schedulerTask.keysService.selectKeys(images.getSource());
schedulerTask.imgService.delectUSS(key, images.getImgname());
}else if(images.getSource()==4){
key = schedulerTask.keysService.selectKeys(images.getSource());
schedulerTask.imgService.delectKODO(key, images.getImgname());
}else if(images.getSource()==5){
LocUpdateImg.deleteLOCImg(images.getImgname());
}else if(images.getSource()==6){
key = schedulerTask.keysService.selectKeys(images.getSource());
schedulerTask.imgService.delectCOS(key, images.getImgname());
}else if(images.getSource()==7){
key = schedulerTask.keysService.selectKeys(images.getSource());
schedulerTask.imgService.delectFTP(key, images.getImgname());
}else if(images.getSource()==8){
key = schedulerTask.keysService.selectKeys(images.getSource());
schedulerTask.imgService.delectUFile(key, images.getImgname());
}else{
System.err.println("未获取到对象存储参数,上传失败。");
}
if (ret == 1) {
System.err.println("存在色情图片,删除成功。");
} else {
System.err.println("存在色情图片,删除失败");
}
}
}
}
}
}
}
}
}
@@ -1,16 +0,0 @@
package cn.hellohao.quartz;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
@EnableScheduling
@Component
public class SecondJob{
public void task() {
//System.out.println("任务2执行....");
SchedulerTask schedulerTask = new SchedulerTask();
schedulerTask.start();
}
}
@@ -1,40 +0,0 @@
package cn.hellohao.quartz;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
@@ -1,150 +0,0 @@
package cn.hellohao.quartz;
import cn.hellohao.pojo.FixedResources;
import cn.hellohao.pojo.Keys;
import cn.hellohao.service.impl.ImgServiceImpl;
import cn.hellohao.service.impl.KeysServiceImpl;
import cn.hellohao.utils.DeleImg;
import cn.hellohao.utils.LocUpdateImg;
import cn.hellohao.utils.Print;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
单任务执行,并且通过控制器的接口实现时间间隔的动态修改
任务类
* */
//@Configuration
@Component
public class TempImgTask {
@Autowired
private ImgServiceImpl imgService;
@Autowired
private KeysServiceImpl keysService;
private static TempImgTask tempImgTask;
@PostConstruct
public void init() {
tempImgTask = this;
tempImgTask.imgService = this.imgService;
tempImgTask.keysService = this.keysService;
}
public void start() throws InterruptedException {
File f = new File(FixedResources.LOCPATH);
if (!f.exists()) {
f.mkdirs();
}
List<String> list = new ArrayList<>();
File file = new File(File.separator + "HellohaoData"+File.separator+"img.ini");
//判断文件是否存在
if (file.exists()) {
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));//构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
result.append(System.lineSeparator() + s);
String str = s;
String[] strArr = str.split("\\|");
System.out.println(strArr.length); //这里输出3
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date sd1=new Date();//大时间 当前时间
Date sd2=df.parse(strArr[1]);//小时间
if(sd1.after(sd2)){//等于false时说明图片时间大于或者等于当前时间
Integer ret = tempImgTask.imgService.deleimgname(strArr[0]);//删除库里图片数据
Keys key =null;
if(Integer.parseInt(strArr[2])==1){
key = tempImgTask.keysService.selectKeys(Integer.parseInt(strArr[2]));
tempImgTask.imgService.delect(key,strArr[0]);
}else if (Integer.parseInt(strArr[2])==2){
key = tempImgTask.keysService.selectKeys(Integer.parseInt(strArr[2]));
tempImgTask.imgService.delectOSS(key, strArr[0]);
}else if(Integer.parseInt(strArr[2])==3){
key = tempImgTask.keysService.selectKeys(Integer.parseInt(strArr[2]));
tempImgTask.imgService.delectUSS(key, strArr[0]);
}else if(Integer.parseInt(strArr[2])==4){
key = tempImgTask.keysService.selectKeys(Integer.parseInt(strArr[2]));
tempImgTask.imgService.delectKODO(key, strArr[0]);
}else if(Integer.parseInt(strArr[2])==5){
LocUpdateImg.deleteLOCImg(strArr[0]);
}else if(Integer.parseInt(strArr[2])==6){
key = tempImgTask.keysService.selectKeys(Integer.parseInt(strArr[2]));
tempImgTask.imgService.delectCOS(key,strArr[0]);
}else if(Integer.parseInt(strArr[2])==7){
key = tempImgTask.keysService.selectKeys(Integer.parseInt(strArr[2]));
tempImgTask.imgService.delectFTP(key, strArr[0]);
}else{
System.err.println("未获取到对象存储参数,上传失败。");
}
}else{
//没有过期的临时图片
list.add(s);
}
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
//return result.toString();
textcr(list);
} else {
//Print.warning("file not exists, 不进行暂存扫描处理");
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
clearInfoForFile(File.separator + "HellohaoData"+File.separator+"img.ini");
}
/**
* 清空文件内容
* @param fileName
*/
public static void clearInfoForFile(String fileName) {
//Print.Normal("清空原ini文件");
File file =new File(fileName);
try {
if(!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter =new FileWriter(file);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//遍历没有过期的list文本,依次插入
public static void textcr(List<String> list){
clearInfoForFile(File.separator + "HellohaoData"+File.separator+"img.ini");
//Print.Normal("开始重新插入没有过期的文本");
for (String s : list) {
DeleImg.charu(s);
}
}
}
@@ -0,0 +1,95 @@
package cn.hellohao.quartz.job;
import cn.hellohao.pojo.Images;
import cn.hellohao.pojo.Keys;
import cn.hellohao.service.ImgTempService;
import cn.hellohao.service.KeysService;
import cn.hellohao.service.impl.*;
import cn.hellohao.utils.LocUpdateImg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Component
@EnableScheduling
public class FirstJob {
@Autowired
private ImgServiceImpl imgService;
@Autowired
private NOSImageupload nosImageupload;
@Autowired
private OSSImageupload ossImageupload;
@Autowired
private COSImageupload cosImageupload;
@Autowired
private KODOImageupload kodoImageupload;
@Autowired
private USSImageupload ussImageupload;
@Autowired
private UFileImageupload uFileImageupload;
@Autowired
private FTPImageupload ftpImageupload;
@Autowired
ImgTempService imgTempService;
@Autowired
KeysService keysService;
private static FirstJob firstJob;
@PostConstruct
public void init() {
firstJob = this;
}
//调用的任务主体
public void task() {
// System.out.println("定时任务.//开始");
try{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currdate = format.format(new Date());
List<Images> imagesList = imgTempService.selectDelImgUidList(currdate);
if(imagesList.size()==0){
// System.out.println("定时任务.//没有期限图片");
return;
}
for (Images images : imagesList) {
imgTempService.delImgAndExp(images.getImguid());
imgService.deleimgForImgUid(images.getImguid());
Keys keys = keysService.selectKeys(images.getSource());
if(keys.getStorageType()==1){
firstJob.nosImageupload.delNOS(keys.getId(),images.getImgname());
}else if (keys.getStorageType()==2){
firstJob.ossImageupload.delOSS(keys.getId(), images.getImgname());
}else if(keys.getStorageType()==3){
firstJob.ussImageupload.delUSS(keys.getId(), images.getImgname());
}else if(keys.getStorageType()==4){
firstJob.kodoImageupload.delKODO(keys.getId(), images.getImgname());
}else if(keys.getStorageType()==5){
LocUpdateImg.deleteLOCImg(images.getImgname());
}else if(keys.getStorageType()==6){
firstJob.cosImageupload.delCOS(keys.getId(),images.getImgname());
}else if(keys.getStorageType()==7){
firstJob.ftpImageupload.delFTP(keys.getId(), images.getImgname());
}else if(keys.getStorageType()==8){
firstJob.uFileImageupload.delUFile(keys.getId(), images.getImgname());
}else{
System.err.println("未获取到对象存储参数,上传失败。");
}
}
}catch (Exception e){
System.err.println("任务查询期限图像发生错误");
e.printStackTrace();
}
}
}
@@ -0,0 +1,18 @@
package cn.hellohao.quartz.job;
import java.text.SimpleDateFormat;
import java.util.Date;
//@Component
//@EnableScheduling
public class ThirdJob {
public void task() {
System.out.println("任务3执行....当前时间为" + dateFormat().format(new Date()));
}
private SimpleDateFormat dateFormat() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
return simpleDateFormat;
}
}
@@ -15,6 +15,8 @@ public interface ImgService {
Integer deleimg(Integer id);
Integer deleimgForImgUid(String imguid);
Integer countimg(Integer userid);
Images selectByPrimaryKey(Integer id);
@@ -57,6 +57,10 @@ public class ImgServiceImpl implements ImgService {
return imgMapper.deleimg(id);
}
@Override
public Integer deleimgForImgUid(String imguid) {
return imgMapper.deleimgForImgUid(imguid);
}
public Images selectByPrimaryKey(Integer id) {
return imgMapper.selectByPrimaryKey(id);
@@ -76,7 +76,6 @@ public class KODOImageupload {
returnImage.setCode("500");
}
return returnImage;
}
public static Integer Initialize(Keys k) {
+3
View File
@@ -63,6 +63,9 @@
DELETE FROM imgdata WHERE id=#{id}
</delete>
<delete id="deleimgForImgUid" parameterType="string">
DELETE FROM imgdata WHERE imguid = #{imguid}
</delete>
<select id="selectByPrimaryKey" parameterType="integer" resultType="cn.hellohao.pojo.Images">
select * from imgdata where id = #{id}