地质所 沉降监测网建设项目
zmk
2024-07-03 41750848c6cc6506fa107036d04b28c0f28cfba8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.javaweb.common.utils;
 
import com.javaweb.common.constant.Constants;
import com.javaweb.common.core.text.Convert;
import com.javaweb.common.utils.cookie.CookieUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 客户端工具类
 * 
 * @author ruoyi
 */
public class ServletUtils
{
    /**
     * 获取String参数
     */
    public static String getParameter(String name)
    {
        return getRequest().getParameter(name);
    }
 
    /**
     * 获取String参数
     */
    public static String getParameter(String name, String defaultValue)
    {
        return Convert.toStr(getRequest().getParameter(name), defaultValue);
    }
 
    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(String name)
    {
        return Convert.toInt(getRequest().getParameter(name));
    }
 
    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(String name, Integer defaultValue)
    {
        return Convert.toInt(getRequest().getParameter(name), defaultValue);
    }
 
    /**
     * 获取request
     */
    public static HttpServletRequest getRequest()
    {
        return getRequestAttributes().getRequest();
    }
 
    /**
     * 获取response
     */
    public static HttpServletResponse getResponse()
    {
        return getRequestAttributes().getResponse();
    }
 
    /**
     * 获取session
     */
    public static HttpSession getSession()
    {
        return getRequest().getSession();
    }
 
    public static ServletRequestAttributes getRequestAttributes()
    {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }
 
    /**
     * 将字符串渲染到客户端
     * 
     * @param response 渲染对象
     * @param string 待渲染的字符串
     * @return null
     */
    public static String renderString(HttpServletResponse response, String string)
    {
        try
        {
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 是否是Ajax异步请求
     * 
     * @param request
     */
    public static boolean isAjaxRequest(HttpServletRequest request)
    {
        String accept = request.getHeader("accept");
        if (accept != null && accept.indexOf("application/json") != -1)
        {
            return true;
        }
 
        String xRequestedWith = request.getHeader("X-Requested-With");
        if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
        {
            return true;
        }
 
        String uri = request.getRequestURI();
        if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
        {
            return true;
        }
 
        String ajax = request.getParameter("__ajax");
        if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))
        {
            return true;
        }
        return false;
    }
 
    /**
     * 登录成功后设置cookie
     * @param username
     * @param password
     * @param rememberMe
     */
    public static void setLoginCookie(String username, String password, Boolean rememberMe){
        HttpServletResponse response= ServletUtils.getResponse();
        response.setHeader("Access-Control-Allow-Credentials","true");
        CookieUtils.setCookie(response, Constants.ADMIN_COOKIE_USERNAME, username, Constants.SECONDS_7_DAYS);
        if (rememberMe) {//记住密码
            CookieUtils.setCookie(response, Constants.ADMIN_COOKIE_PASSWORD, password, Constants.SECONDS_7_DAYS);
            CookieUtils.setCookie(response, Constants.ADMIN_COOKIE_REMEMBERME, "1", Constants.SECONDS_7_DAYS);
        } else {
            CookieUtils.setCookie(response, Constants.ADMIN_COOKIE_PASSWORD, password,0);//0表示清除Cookie
            CookieUtils.setCookie(response, Constants.ADMIN_COOKIE_REMEMBERME, "0", Constants.SECONDS_7_DAYS);
        }
    }
    
    /**
     * 前台登录
     * @param username
     * @param password
     * @param rememberMe
     */
    public static void setFrontLoginCookie(String username, String password, Boolean rememberMe){
        HttpServletResponse response= ServletUtils.getResponse();
        response.setHeader("Access-Control-Allow-Credentials","true");
        CookieUtils.setCookie(response, Constants.FRONT_ADMIN_COOKIE_USERNAME, username, Constants.SECONDS_7_DAYS);
        if (rememberMe) {//记住密码
            CookieUtils.setCookie(response, Constants.FRONT_ADMIN_COOKIE_PASSWORD, password, Constants.SECONDS_7_DAYS);
            CookieUtils.setCookie(response, Constants.FRONT_ADMIN_COOKIE_REMEMBERME, "1", Constants.SECONDS_7_DAYS);
        } else {
            CookieUtils.setCookie(response, Constants.FRONT_ADMIN_COOKIE_PASSWORD, password,0);//0表示清除Cookie
            CookieUtils.setCookie(response, Constants.FRONT_ADMIN_COOKIE_REMEMBERME, "0", 86400);
        }
    }
 
    public static Map<String, String> getMap(HttpServletRequest req) {
        Map<String, String> map=new HashMap<String, String>();
        Enumeration enu = req.getParameterNames();
        while(enu.hasMoreElements()) {
            String paramName = (String)enu.nextElement();
            String[] paramValues = req.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        return map;
    }
}