管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-03-02 c1c2596883cd00b46fcfc783ab8c2af62e138f01
src/main/java/com/lf/server/helper/WebHelper.java
@@ -5,6 +5,8 @@
import com.lf.server.entity.all.ResponseMsg;
import com.lf.server.entity.all.SettingData;
import com.lf.server.entity.all.StaticData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@@ -19,9 +21,7 @@
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;
import java.util.*;
/**
 * Web帮助类
@@ -31,6 +31,8 @@
    private final static String UNKNOWN = "unknown";
    private final static String COMMA = ",";
    private final static Log log = LogFactory.getLog(WebHelper.class);
    /**
     * 获取GUID
@@ -277,6 +279,17 @@
    }
    /**
     * 写响应信息
     */
    public static void writeInfo(HttpStatus status, String info, HttpServletResponse res) {
        try {
            WebHelper.write2Page(res, WebHelper.getErrJson(status, info));
        } catch (Exception e) {
            //
        }
    }
    /**
     * 获取随机整数
     */
    public static int getRandomInt(int min, int max) {
@@ -285,17 +298,29 @@
    /**
     * 下载文件
     */
    public static void download(String file, String fileName, HttpServletResponse res) throws Exception {
        download(file, fileName, res);
    }
    /**
     * 下载文件
     *
     * @param file 文件
     * @param res  响应
     * @param file     文件
     * @param fileName 文件名
     * @param res      响应
     * @throws Exception 异常
     */
    public static void download(String file, HttpServletResponse res) throws Exception {
        String fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8");
    public static void download(String file, String fileName, boolean inline, HttpServletResponse res) throws Exception {
        if (StringHelper.isEmpty(fileName)) {
            fileName = StringHelper.YMDHMS2_FORMAT.format(new Date());
        }
        fileName = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
        String dispose = inline ? "inline" : "attachment";
        // 设置响应头中文件的下载方式为附件方式,以及设置文件名
        res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        // 设置响应头的编码格式为UTF-8
        res.setHeader("Content-Disposition", dispose + "; filename*=UTF-8''" + fileName);
        // 设置响应头的编码格式为 UTF-8
        res.setCharacterEncoding("UTF-8");
        // 通过response对象设置响应数据格式(如:"text/plain; charset=utf-8")
@@ -317,7 +342,56 @@
        }
        // 关闭资源
        outputStream.close();
        fileInputStream.close();
        outputStream.close();
    }
    /**
     * 执行命令
     *
     * @param cmd 命令
     */
    public static void exec(String cmd) {
        try {
            Runtime.getRuntime().exec(cmd);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    /**
     * 获取请求的参数值
     *
     * @param req 请求
     * @param key 参数名
     * @return 参数值
     */
    public static String getReqParamVal(HttpServletRequest req, String key) {
        Map<String, String[]> maps = req.getParameterMap();
        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(key)) {
                return null == entry.getValue() || 0 == entry.getValue().length ? null : entry.getValue()[0];
            }
        }
        return null;
    }
    /**
     * 获取请求的参数值
     *
     * @param req 请求
     * @param key 参数名
     * @return 参数值
     */
    public static String[] getReqParamVals(HttpServletRequest req, String key) {
        Map<String, String[]> maps = req.getParameterMap();
        for (Map.Entry<String, String[]> entry : maps.entrySet()) {
            if (entry.getKey().equalsIgnoreCase(key)) {
                return entry.getValue();
            }
        }
        return null;
    }
}