xiaoyong931011
2022-07-27 3280d1bd977e8fb5c9c60e615612fabb7b99c3e3
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package cc.mrbird.febs.pay.util;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.util.Map;
 
/**
 * 针对ssh项目提供的一些实用性的方法。
 * 
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date Dec 11, 2017
 */
@Component
public class WebUtil implements ApplicationContextAware {
 
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        WebUtil.applicationContext = applicationContext;
    }
 
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }
 
    public static <T> T getBean(String beanName, Class<T> clazz) {
        return applicationContext.getBean(beanName, clazz);
    }
 
    /**
     * 获得web资源的绝对路径
     * 
     * @author JiangYouYao
     * @date 2014年10月14日-上午8:31:01
     * @param path
     * @return
     */
    public static String getResourceRealPath(String path) {
        return getServletContext().getRealPath(path);
    }
 
    /**
     * 
     *  获取web项目的访问URl
     * @author:姜友瑶
     * @return 返回类型 String
     * @date 2016年11月16日
     */
    public static String getWebUrl() {
        return getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort()
                + getRequest().getContextPath() + "/";
    }
 
    /**
     *  获得该类的泛型类型
     * @Return: Class 泛型的类型
     * @Author: JiangYouYao
     * @Version: V1.00 (版本号1.0)
     * @Create Date: 2014-8-12 (创建日期)
     */
    @SuppressWarnings("rawtypes")
    public static Class getClass(Class clazz) {
        // 泛型转换
        ParameterizedType pt = (ParameterizedType) clazz.getGenericSuperclass();
        return (Class) pt.getActualTypeArguments()[0];
    }
 
    /**
     * 2016/6/2
     * 
     * @author xieguangya
     * @return getRequest
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }
 
    /**
     * 
     *  在当前请求的request中获取一个值
     * @author:姜友瑶
     * @return
     * @date 2016年11月11日
     */
    public static Object getRequestAttribute(String name) {
        return getRequest().getAttribute(name);
    }
 
    /**
     * 
     *  在当前请求的request中新增一个值
     * @author:姜友瑶
     * @return
     * @date 2016年11月11日
     */
    public static void setRequestAttribute(String name, Object o) {
        getRequest().setAttribute(name, o);
    }
 
    /**
     * 
     *  在request中删除一个值
     * @author:姜友瑶
     * @return
     * @date 2016年11月11日
     */
    public static void removeRequestAttribute(String name) {
        getRequest().removeAttribute(name);
    }
 
    /**
     * 
     *  在Session中新增一个值
     * @author:姜友瑶
     * @return
     * @date 2016年11月11日
     */
    public static void setSessionAttribute(String name, Object o) {
        getSession().setAttribute(name, o);
    }
 
    /**
     * 
     *  在当前session中获取一个值
     * @author:姜友瑶
     * @param <T>
     * @return
     * @date 2016年11月11日
     */
    public static <T> T getSessionAttribute(String name) {
        return (T) getSession().getAttribute(name);
    }
 
    /**
     * 
     *  在Session中删除一个值
     * @author:姜友瑶
     * @return
     * @date 2016年11月11日
     */
    public static void removeSessionAttribute(String name) {
        getSession().removeAttribute(name);
    }
 
    /**
     * 获取session
     * 
     * @author Matrix-J
     * @return HttpSession
     */
    /**
     * 获取session
     * 
     * @author Matrix-J
     * @return HttpSession
     */
    public static HttpSession getSession() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    }
 
    /**
     * 2016年6月15日 获取ServletContext
     * 
     * @author Matrix-J
     * @return getServletContext
     */
    public static ServletContext getServletContext() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession()
                .getServletContext();
    }
 
    public static final String LEFT_SLASH = "/";
    /**
     * 常量“字符.”
     */
    public static final String CHARACTER_ALL = "\\.";
 
    /**
     * 常量“.class”
     */
    public static final String CLASS_FILE_EXTEND_NAME = ".class";
    /**
     * 常量“一个空格”
     */
    public static final String CHARACTER_BLANK = " ";
    /**
     * 空格转码后结果
     */
    public static final String SPACE_REPLEACE_STRING = "%20";
    /**
     * 常量“字符左斜杠”
     */
    public static final String CHARACTER_LEFT = "\\/";
    /**
     * 常量"WEB-INF"路径
     */
    public static final String CONFIG_ROOT = "WEB-INF/";
    /**
     * 文件协议
     */
    public static final String FILE_PROTOCOL = "file:";
 
    /**
     * <li>功能简述:获取项目的实际路径
     * <li>详细描述:WEB-INF
     */
    public static String getContextPath() {
 
        String name = WebUtil.class.getName();
        name = LEFT_SLASH + name.replaceAll(CHARACTER_ALL, CHARACTER_LEFT) + CLASS_FILE_EXTEND_NAME;
        String space = SPACE_REPLEACE_STRING;
        String path = WebUtil.class.getResource(name).getPath();
        path = path.substring(0, path.indexOf(CONFIG_ROOT) + CONFIG_ROOT.length());
        path = path.replaceAll(space, CHARACTER_BLANK);
        if (path.startsWith(FILE_PROTOCOL)) {
            path = path.substring(FILE_PROTOCOL.length());
        }
        return path;
    }
 
    /**
     * <li>功能简述:获得发布目录路径
     * <li>详细描述:webapps
     */
    public static String getDeployPath() {
 
        File tempDir = new File(getContextPath());
        return tempDir.getParentFile().getParentFile().getAbsolutePath();
    }
 
    /**
     * <li>功能简述:获得项目目录
     */
    public static String getWebPath() {
 
        File tempDir = new File(getContextPath());
        return tempDir.getParentFile().getAbsolutePath();
    }
 
    /**
     * 获取当前访问路径含参数
     */
    public static String getLocation() {
        Map<String, String[]> params = getRequest().getParameterMap();
        String queryString = "";
        if (params.keySet().size() > 0) {
            queryString = "?";
            for (String key : params.keySet()) {
                String[] values = params.get(key);
                for (int i = 0; i < values.length; i++) {
                    String value = values[i];
                    queryString += key + "=" + value + "&";
                }
            }
        }
        return getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort()
                + getRequest().getRequestURI() + queryString;
    }
 
    /**
     *
     *
     * @description 获取客户端ip地址
     * @data 2015年8月6日 下午7:15:38
     * @author Administrator
     * @return
     */
    public static String getCustomerIp() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}