56 lines
1.4 KiB
Java
56 lines
1.4 KiB
Java
|
package com.isu.gaswellwatch.utils;
|
||
|
|
||
|
import jakarta.servlet.http.HttpServletRequest;
|
||
|
|
||
|
import java.io.UnsupportedEncodingException;
|
||
|
import java.net.URLDecoder;
|
||
|
import java.net.URLEncoder;
|
||
|
import java.util.Enumeration;
|
||
|
import java.util.LinkedHashMap;
|
||
|
import java.util.Map;
|
||
|
|
||
|
public class ServletUtils {
|
||
|
/**
|
||
|
* 内容编码
|
||
|
*
|
||
|
* @param str 内容
|
||
|
* @return 编码后的内容
|
||
|
*/
|
||
|
public static String urlEncode(String str) {
|
||
|
try {
|
||
|
return URLEncoder.encode(str, "UTF-8");
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
return "";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 内容解码
|
||
|
*
|
||
|
* @param str 内容
|
||
|
* @return 解码后的内容
|
||
|
*/
|
||
|
public static String urlDecode(String str) {
|
||
|
try {
|
||
|
return URLDecoder.decode(str, "UTF-8");
|
||
|
} catch (UnsupportedEncodingException e) {
|
||
|
return "";
|
||
|
} catch (NullPointerException n) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static Map<String, String> getHeaders(HttpServletRequest request) {
|
||
|
Map<String, String> map = new LinkedHashMap<>();
|
||
|
Enumeration<String> enumeration = request.getHeaderNames();
|
||
|
if (enumeration != null) {
|
||
|
while (enumeration.hasMoreElements()) {
|
||
|
String key = enumeration.nextElement();
|
||
|
String value = request.getHeader(key);
|
||
|
map.put(key, value);
|
||
|
}
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
|
}
|