From 495292b588949bcb2cb15eaa9657619aca7f5579 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 30 十月 2024 16:18:41 +0800 Subject: [PATCH] 修改降雨文件的jar --- src/main/java/com/se/simu/helper/WebHelper.java | 65 +++++++++++++++++++++++++++++--- 1 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/se/simu/helper/WebHelper.java b/src/main/java/com/se/simu/helper/WebHelper.java index 08db9f8..e386684 100644 --- a/src/main/java/com/se/simu/helper/WebHelper.java +++ b/src/main/java/com/se/simu/helper/WebHelper.java @@ -1,6 +1,6 @@ package com.se.simu.helper; -import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.web.context.request.RequestContextHolder; @@ -9,8 +9,7 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.FileInputStream; -import java.io.PrintWriter; +import java.io.*; import java.net.InetAddress; import java.net.URLEncoder; import java.net.UnknownHostException; @@ -32,10 +31,23 @@ private final static String UNKNOWN = "unknown"; + public static boolean isWin() { + String osName = System.getProperty("os.name"); + + return osName.startsWith("Windows"); + } + /** - * 鏍煎紡鍖栧綋鍓嶇郴缁熸棩鏈� 4 + * 鑾峰彇CPU鏍稿績鏁� */ - public static final SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss"); + public static int getCpuCores() { + return Runtime.getRuntime().availableProcessors(); + } + + /** + * 鏍煎紡鍖栨棩鏈� + */ + public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss"); /** * 瀛楃涓�,鏄惁涓虹┖null鍜岀┖鏍� @@ -53,8 +65,6 @@ /** * 鑾峰彇涓绘満IP - * - * @return */ public static String getHostIp() { try { @@ -189,6 +199,40 @@ } /** + * 杈撳嚭瀛楄妭鏁扮粍 + */ + public static void writeBytes(byte[] bytes, HttpServletResponse res) throws IOException { + res.setContentType("application/octet-stream"); + + if (null == bytes) { + res.setStatus(HttpStatus.NOT_FOUND.value()); + return; + } + + OutputStream os = res.getOutputStream(); + os.write(bytes, 0, bytes.length); + os.close(); + } + + /** + * 杈撳嚭Png鏂囦欢 + */ + public static void writePng(String filePath, HttpServletResponse res) throws IOException { + File file = new File(filePath); + if (!file.exists() || file.isDirectory()) { + res.setStatus(HttpStatus.NOT_FOUND.value()); + return; + } + + String fileName = URLEncoder.encode(filePath, "UTF-8").replace("+", "%20"); + res.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName); + res.setCharacterEncoding("UTF-8"); + res.setContentType("image/png"); + + writeFile(filePath, res); + } + + /** * 鑾峰彇闅忔満鏁存暟 */ public static int getRandomInt(int min, int max) { @@ -227,6 +271,13 @@ String mime = getMime(ext); res.setContentType(mime); + writeFile(file, res); + } + + /** + * 鍐欐枃浠� + */ + private static void writeFile(String file, HttpServletResponse res) throws IOException { // 閫氳繃response瀵硅薄锛岃幏鍙栧埌杈撳嚭娴� ServletOutputStream outputStream = res.getOutputStream(); // 瀹氫箟杈撳叆娴侊紝閫氳繃杈撳叆娴佽鍙栨枃浠跺唴瀹� -- Gitblit v1.9.3