package com.ruzhoukj.sms.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.platform.exception.BusinessException; import com.platform.model.resp.Result; import com.ruzhoukj.sms.entity.CertificateEntity; import com.ruzhoukj.sms.model.vo.KzWorkVO; import com.ruzhoukj.sms.pojo.vo.CertificateVO; import com.ruzhoukj.sms.service.CertificateService; import com.ruzhoukj.sms.service.KzWorkService; import lombok.extern.slf4j.Slf4j; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.List; @Slf4j @RequestMapping("/certificate") @RestController public class CertificateController { @Resource private CertificateService certificateService; @Resource private KzWorkService kzWorkService; @GetMapping("/replace") public Result replace(@RequestParam("name") String name) { log.info("name-------------{}", name); CertificateEntity certificate = certificateService.getOne(new LambdaQueryWrapper().eq(CertificateEntity::getName, name)); if (ObjectUtils.isEmpty(certificate)) { throw new BusinessException("证书不存在"); } //指定新的文件名和保存目录 String localDirectory = certificate.getPath(); Boolean crt = upload(localDirectory, certificate.getPrefix() + ".ruzhoukj.com.crt", "account.ruzhoukj.com.crt"); Boolean key = upload(localDirectory, certificate.getPrefix() + ".ruzhoukj.com.key", "account.ruzhoukj.com.key"); if(crt && key){ return Result.data(name + "证书替换成功"); }else { return Result.fail(name + "证书替换失败,请检查日志信息,排查错误原因"); } } private boolean upload(String localDirectory, String newFileName, String resourceFileName) { String localFilePath = localDirectory + "/" + newFileName; try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(resourceFileName)) { if (inputStream == null) { log.error("资源文件未找到: " + resourceFileName); return false; } File targetFile = new File(localFilePath); // 将输入流复制到目标文件,实现上传和重命名 Files.copy(inputStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); log.info("证书文件已成功上传并重命名为: " + targetFile.getAbsolutePath()); } catch (IOException e) { log.error("处理证书文件时出错: " + e.getMessage()); return false; } return true; } @GetMapping("/record") public Result> record(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "time",required = false) String time) { log.info("name-------------{}", name); log.info("time-------------{}", time); return Result.data(kzWorkService.record(name, time)); } }