You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
6.1 KiB
226 lines
6.1 KiB
package com.fr.plugin.zjsso.utils; |
|
|
|
import com.fr.base.TemplateUtils; |
|
import com.fr.data.NetworkHelper; |
|
import com.fr.io.utils.ResourceIOUtils; |
|
import com.fr.json.JSONObject; |
|
import com.fr.stable.CodeUtils; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.third.org.apache.commons.codec.digest.DigestUtils; |
|
import com.fr.web.utils.WebUtils; |
|
|
|
import javax.servlet.http.Cookie; |
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.BufferedReader; |
|
import java.io.InputStream; |
|
import java.net.URLEncoder; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
import java.util.UUID; |
|
import java.util.regex.Matcher; |
|
import java.util.regex.Pattern; |
|
|
|
public class Utils { |
|
|
|
/** |
|
* 判断字符串是否为空 |
|
* @param str |
|
* @return true 空字符串 false 非空字符串 |
|
*/ |
|
public static boolean isNullStr(String str){ |
|
return !(str != null && !str.isEmpty() && !"null".equals(str)); |
|
} |
|
|
|
/** |
|
* 判断字符串是否非空 |
|
* @param str |
|
* @return |
|
*/ |
|
public static boolean isNotNullStr(String str){ |
|
return !isNullStr(str); |
|
} |
|
|
|
/** |
|
* MD5加密 |
|
* @param str |
|
* @return |
|
*/ |
|
public static String getMd5Str(String str) |
|
{ |
|
return DigestUtils.md5Hex(str); |
|
} |
|
|
|
/** |
|
* 帆软shaEncode加密 |
|
*/ |
|
|
|
public static String shaEncode(String str){ |
|
return CodeUtils.sha256Encode(str); |
|
} |
|
|
|
/** |
|
* 获取uuid |
|
*/ |
|
public static String uuid(){ |
|
return UUID.randomUUID().toString(); |
|
} |
|
|
|
/** |
|
* 替换空字符串 |
|
* @param str |
|
* @param replace |
|
* @return |
|
*/ |
|
public static String replaceNullStr(String str,String replace){ |
|
if(isNullStr(str)){ |
|
return replace; |
|
} |
|
|
|
return str; |
|
} |
|
|
|
/** |
|
* 获取请求体 |
|
* @param req |
|
* @return |
|
*/ |
|
public static JSONObject getRequestBody(HttpServletRequest req){ |
|
StringBuffer sb = new StringBuffer(); |
|
String line = null; |
|
try { |
|
BufferedReader reader = req.getReader(); |
|
while ((line = reader.readLine()) != null) |
|
sb.append(line); |
|
} catch (Exception e) { |
|
FRUtils.FRLogInfo("getRequestBody:exception:"+e.getMessage()); |
|
} |
|
//将空格和换行符替换掉避免使用反序列化工具解析对象时失败 |
|
String jsonString = sb.toString().replaceAll("\\s","").replaceAll("\n",""); |
|
|
|
JSONObject json = new JSONObject(jsonString); |
|
|
|
return json; |
|
} |
|
|
|
/** |
|
* 获取ip |
|
* @return |
|
*/ |
|
public static String getIp(HttpServletRequest req){ |
|
String realIp = req.getHeader("X-Real-IP"); |
|
String fw = req.getHeader("X-Forwarded-For"); |
|
if (StringUtils.isNotEmpty(fw) && !"unKnown".equalsIgnoreCase(fw)) { |
|
int var3 = fw.indexOf(","); |
|
return var3 != -1 ? fw.substring(0, var3) : fw; |
|
} else { |
|
fw = realIp; |
|
if (StringUtils.isNotEmpty(realIp) && !"unKnown".equalsIgnoreCase(realIp)) { |
|
return realIp; |
|
} else { |
|
if (StringUtils.isBlank(realIp) || "unknown".equalsIgnoreCase(realIp)) { |
|
fw = req.getHeader("Proxy-Client-IP"); |
|
} |
|
|
|
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
|
fw = req.getHeader("WL-Proxy-Client-IP"); |
|
} |
|
|
|
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
|
fw = req.getHeader("HTTP_CLIENT_IP"); |
|
} |
|
|
|
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
|
fw = req.getHeader("HTTP_X_FORWARDED_FOR"); |
|
} |
|
|
|
if (StringUtils.isBlank(fw) || "unknown".equalsIgnoreCase(fw)) { |
|
fw = req.getRemoteAddr(); |
|
} |
|
|
|
return fw; |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 根据key获取cookie |
|
* @param req |
|
* @return |
|
*/ |
|
public static String getCookieByKey(HttpServletRequest req,String key){ |
|
Cookie[] cookies = req.getCookies(); |
|
String cookie = ""; |
|
|
|
if(cookies == null || cookies.length <=0){ |
|
return ""; |
|
} |
|
|
|
for(int i = 0; i < cookies.length; i++) { |
|
Cookie item = cookies[i]; |
|
if (item.getName().equalsIgnoreCase(key)) { |
|
cookie = item.getValue(); |
|
} |
|
} |
|
|
|
FRUtils.FRLogInfo("cookie:"+cookie); |
|
|
|
return cookie; |
|
} |
|
|
|
/** |
|
* 判断是否是手机端的链接 |
|
* @param req |
|
* @return |
|
*/ |
|
public static boolean isMobile(HttpServletRequest req) { |
|
String[] mobileArray = {"iPhone", "iPad", "android", "windows phone", "xiaomi"}; |
|
String userAgent = req.getHeader("user-agent"); |
|
if (userAgent != null && userAgent.toUpperCase().contains("MOBILE")) { |
|
for(String mobile : mobileArray) { |
|
if(userAgent.toUpperCase().contains(mobile.toUpperCase())) { |
|
return true; |
|
} |
|
} |
|
} |
|
return NetworkHelper.getDevice(req).isMobile(); |
|
} |
|
|
|
/** |
|
* 只编码中文 |
|
* @param url |
|
* @return |
|
*/ |
|
public static String encodeCH(String url ){ |
|
Matcher matcher = Pattern.compile("[\\u4e00-\\u9fa5]").matcher(url); |
|
|
|
while(matcher.find()){ |
|
String chn = matcher.group(); |
|
url = url.replaceAll(chn, URLEncoder.encode(chn)); |
|
} |
|
|
|
return url; |
|
} |
|
|
|
/** |
|
* 获取web-inf文件夹下的文件 |
|
* filename /resources/ip4enc.properties |
|
*/ |
|
public static InputStream getResourcesFile(String filename){ |
|
return ResourceIOUtils.read(filename); |
|
} |
|
|
|
public static void toErrorPage(HttpServletResponse res,String path,Map<String, String> parameterMap){ |
|
if(parameterMap == null){ |
|
parameterMap = new HashMap<String, String>(); |
|
} |
|
|
|
try { |
|
String macPage = TemplateUtils.renderTemplate(path, parameterMap); |
|
WebUtils.printAsString(res, macPage); |
|
}catch (Exception e){ |
|
FRUtils.FRLogError("跳转页面异常"); |
|
} |
|
|
|
} |
|
}
|
|
|