From 968e290a94dd2f01db0f1443ff900ace79629c92 Mon Sep 17 00:00:00 2001 From: guochaojie Date: Tue, 28 May 2024 21:15:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cc/yunxi/controller/QRCodeController.java | 85 +++++++++++++++++++ .../src/main/resources/static/7Hmf2EbbCc.txt | 1 + 2 files changed, 86 insertions(+) create mode 100644 nxhs-service/src/main/java/cc/yunxi/controller/QRCodeController.java create mode 100644 nxhs-service/src/main/resources/static/7Hmf2EbbCc.txt diff --git a/nxhs-service/src/main/java/cc/yunxi/controller/QRCodeController.java b/nxhs-service/src/main/java/cc/yunxi/controller/QRCodeController.java new file mode 100644 index 0000000..d8c9962 --- /dev/null +++ b/nxhs-service/src/main/java/cc/yunxi/controller/QRCodeController.java @@ -0,0 +1,85 @@ +package cc.yunxi.controller; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.http.ResponseEntity; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +@ApiModel("机柜扫码登录") +@Slf4j +@RestController("/qrcode") +public class QRCodeController { + + private static final Map FILE_CONTEXT = new ConcurrentHashMap<>(); + +// @ApiOperation("二维码校验文件") +// @GetMapping("/qrcode/{filename}") +// public void getQRCode(HttpServletResponse response, @PathVariable String filename) throws IOException { +// Resource resource = new ClassPathResource("static/" + filename); +// +// if (resource.exists() || resource.isReadable()) { +// // 设置响应头 +// String contentType = null; +// try (InputStream inputStream = resource.getInputStream()) { +// contentType = "application/octet-stream"; +// } catch (IOException e) { +// // 如果无法获取输入流,则回退到默认内容类型 +// contentType = "application/octet-stream"; +// } +// +// response.setContentType(contentType); +// response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\""); +// +// try (InputStream inputStream = resource.getInputStream(); +// OutputStream outputStream = response.getOutputStream()) { +// +// byte[] buffer = new byte[4096]; +// int bytesRead = -1; +// while ((bytesRead = inputStream.read(buffer)) != -1) { +// outputStream.write(buffer, 0, bytesRead); +// } +// outputStream.flush(); +// } +// } else { +// response.setStatus(HttpServletResponse.SC_NOT_FOUND); +// } +// } + + @ApiOperation("二维码校验文件") + @GetMapping("/qrcode/{filename:.+}") + public ResponseEntity getFile(@PathVariable String filename) { + if (StringUtils.hasText(filename)) { + String context = FILE_CONTEXT.get(filename); + if (StringUtils.hasText(context)) return ResponseEntity.ok(context); + } + Resource resource = new ClassPathResource("static/" + filename); + if (resource.exists() || resource.isReadable()) { + + try { + Path path = resource.getFile().toPath(); + String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8); + FILE_CONTEXT.put(filename, content); + return ResponseEntity.ok(content); + } catch (IOException e) { + e.printStackTrace(); + log.error("读取校验文件失败: {}", e.getMessage()); + } + } + return ResponseEntity.notFound().build(); + } + + +} diff --git a/nxhs-service/src/main/resources/static/7Hmf2EbbCc.txt b/nxhs-service/src/main/resources/static/7Hmf2EbbCc.txt new file mode 100644 index 0000000..a8afb47 --- /dev/null +++ b/nxhs-service/src/main/resources/static/7Hmf2EbbCc.txt @@ -0,0 +1 @@ +175babf4957dd6090b5b36b78cd51c78 \ No newline at end of file