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.
330 lines
8.5 KiB
330 lines
8.5 KiB
2 years ago
|
package com.eco.plugin.xx.gfjjwhite.utils;
|
||
|
|
||
|
import com.fr.base.TemplateUtils;
|
||
|
import com.fr.data.NetworkHelper;
|
||
|
import com.fr.decision.webservice.v10.user.UserService;
|
||
|
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.List;
|
||
|
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","");
|
||
|
FRUtils.FRLogInfo("reqBody:"+jsonString);
|
||
|
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 para3 = fw.indexOf(",");
|
||
|
return para3 != -1 ? fw.substring(0, para3) : 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);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param res
|
||
|
* @param path /com/fr/plugin/loginAuth/html/getMac.html
|
||
|
* @param parameterMap
|
||
|
*/
|
||
|
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("跳转页面异常");
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 判断是否是管理员
|
||
|
* @param username
|
||
|
* @return
|
||
|
*/
|
||
|
public static boolean isAdmin(String username) throws Exception{
|
||
|
return UserService.getInstance().isAdmin(UserService.getInstance().getUserByUserName(username).getId());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 去掉浏览器中的参数
|
||
|
* @param url
|
||
|
* @param param
|
||
|
* @return
|
||
|
*/
|
||
|
public static String removeParam(String url,String param){
|
||
|
if(!url.contains("?"+param) && !url.contains("&"+param)){
|
||
|
return url;
|
||
|
}
|
||
|
|
||
|
return url.substring(0,url.indexOf(url.contains("?"+param) ? "?"+param : "&"+param));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取跳转链接
|
||
|
* @param req
|
||
|
* @param param
|
||
|
* @return
|
||
|
*/
|
||
|
public static String getRedirectUrl(HttpServletRequest req,String param){
|
||
|
String url = FRUtils.getAllUrl(req);
|
||
|
|
||
|
if(isNotNullStr(param)){
|
||
|
url = removeParam(url,param);
|
||
|
}
|
||
|
|
||
|
url = encodeCH(url);
|
||
|
|
||
|
return url;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 去除空格换行
|
||
|
* @param str
|
||
|
* @return
|
||
|
*/
|
||
|
public static String trim(String str){
|
||
|
return str.trim().replaceAll("\n","").replaceAll("\r","");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* list 转化为指定字符分割的字符串
|
||
|
* @param list
|
||
|
* @param list
|
||
|
* @return
|
||
|
*/
|
||
|
public static String listToStr(List<String> list, String split){
|
||
|
String result = "";
|
||
|
|
||
|
if(list == null || list.size() <= 0){
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
for(String str : list){
|
||
|
result+=","+str;
|
||
|
}
|
||
|
|
||
|
result = result.substring(1);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* array 转化为指定字符分割的字符串
|
||
|
* @param list
|
||
|
* @param list
|
||
|
* @return
|
||
|
*/
|
||
|
public static String arrayToStr(String[] list, String split){
|
||
|
String result = "";
|
||
|
|
||
|
if(list == null ||list.length <= 0){
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
for(int i=0;i<list.length;i++){
|
||
|
String str = list[i];
|
||
|
result+=","+str;
|
||
|
}
|
||
|
|
||
|
result = result.substring(1);
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
}
|