package com.ruzhoukj.sms.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.platform.model.resp.Result; import com.ruzhoukj.sms.entity.CertificateEntity; import com.ruzhoukj.sms.entity.kzReminderEntity; import com.ruzhoukj.sms.pojo.dto.CertificateDTO; import com.ruzhoukj.sms.service.CertificateService; import com.ruzhoukj.sms.service.KzReminderService; import lombok.extern.slf4j.Slf4j; import org.springframework.util.ObjectUtils; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; @Slf4j @RequestMapping("/certificate") @RestController public class CertificateController { @Resource private CertificateService certificateService; @Resource private KzReminderService kzReminderService; @PostMapping("/reminder") public Result reminder(@RequestBody CertificateDTO certificateDTO) { kzReminderService.save(kzReminderEntity.builder() .time(certificateDTO.getTime()) .phone(certificateDTO.getPhone()) .email(certificateDTO.getEmail()) .userName(certificateDTO.getUserName()) .content(certificateDTO.getContent()) .build()); log.info("提醒人信息---------{}", certificateDTO); return Result.data("定时通知设置成功"); } @GetMapping("/replace") public Result replace(@RequestParam("url") String url, @RequestParam("name") String name) { log.info("url-------------{}", url); log.info("name-------------{}", name); InputStream inputStream; try { inputStream = downloadAsStream(url); } catch (IOException e) { return Result.data(false); } CertificateEntity certificate = certificateService.getOne(new LambdaQueryWrapper().eq(CertificateEntity::getName, name)); if (ObjectUtils.isEmpty(certificate)) { return Result.data(false); } return Result.data(uploadFile(name, certificate.getPath(), inputStream)); } /** * 从指定的URL下载文件并返回输入流 * * @param fileUrl 文件的完整URL地址 * @return InputStream 文件内容的输入流,需要由调用方关闭 * @throws IOException 当网络连接失败或HTTP响应码非200时抛出 */ public static InputStream downloadAsStream(String fileUrl) throws IOException { URL url = new URL(fileUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置连接参数 connection.setConnectTimeout(10000); // 10秒连接超时 connection.setReadTimeout(60000); // 60秒读取超时 connection.setRequestMethod("GET"); // 设置User-Agent,防止被服务器拒绝[2,3,5](@ref) connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"); // 检查HTTP响应状态[6,7](@ref) int responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_OK) { connection.disconnect(); throw new IOException("HTTP请求失败,响应码: " + responseCode + " - " + connection.getResponseMessage()); } // 返回输入流,注意:调用方需要负责关闭这个流[8](@ref) return connection.getInputStream(); } public static Boolean uploadFile( @RequestParam("name") String fileName, @RequestParam("path") String filePath, InputStream inputStream) { if (fileName == null || fileName.isEmpty() || filePath == null || filePath.isEmpty()) { return true; } File directory = new File(filePath); if (!directory.exists()) { directory.mkdirs(); } File outputFile = new File(directory, fileName); // 使用Spring的StreamUtils进行流拷贝 try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { StreamUtils.copy(inputStream, outputStream); return true; } catch (IOException e) { return false; } finally { // 确保关闭输入流,如果它不由Spring管理的话 try { if (inputStream != null) { inputStream.close(); } } catch (IOException ignored) { } } } }