13693261870
2024-09-12 b5326f8d497a6f6e97a487cb9c565fdae1dc4790
src/main/java/com/se/simu/helper/WebHelper.java
@@ -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,16 @@
    private final static String UNKNOWN = "unknown";
    public static boolean isWin() {
        String osName = System.getProperty("os.name");
        return osName.startsWith("Windows");
    }
    /**
     * 格式化当前系统日期 4
     * 格式化日期
     */
    public static final SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss");
    public final static SimpleDateFormat YMDHMS = new SimpleDateFormat("yyyyMMddHHmmss");
    /**
     * 字符串,是否为空null和空格
@@ -53,8 +58,6 @@
    /**
     * 获取主机IP
     *
     * @return
     */
    public static String getHostIp() {
        try {
@@ -189,6 +192,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 +264,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();
        // 定义输入流,通过输入流读取文件内容