1.读取自定义.properties文件
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
/**
*
* @Param fileName 文件相对/绝对路径
* @author bobo
*/
@Component("RSProUtils")
public class ProUtils {
public static Properties loadProps(String fileName) {
Properties props = null;
InputStreamReader is = null;
try {
Resource fileRource = new ClassPathResource(fileName);
if (fileRource == null) {
throw new FileNotFoundException(fileName + " file is not found");
}
is = new InputStreamReader(fileRource.getInputStream(), "UTF-8");
props = new Properties();
props.load(is);
} catch (IOException var12) {
var12.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException var11) {
var11.printStackTrace();
}
}
}
return props;
}
}
2.读取XML文件
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import com.inspur.gs.orp.service.service.PublicMethodOrp;
import org.springframework.beans.factory.annotation.Autowired;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ReadXMLUtils {
//定义变量:xml中标签名
private String BTDCLASSIFY;
private String RULE;
private String FILELD;
private String CONTAINS;
public String getBTDCLASSIFY() {
return BTDCLASSIFY;
}
public void setBTDCLASSIFY(String BTDCLASSIFY) {
this.BTDCLASSIFY = BTDCLASSIFY;
}
public String getRULE() {
return RULE;
}
public void setRULE(String RULE) {
this.RULE = RULE;
}
public String getFILELD() {
return FILELD;
}
public void setFILELD(String FILELD) {
this.FILELD = FILELD;
}
public String getCONTAINS() {
return CONTAINS;
}
public void setCONTAINS(String CONTAINS) {
this.CONTAINS = CONTAINS;
}
/**
*
* @Param fileName 文件相对/绝对路径
* @author bobo
*/
public ReadXMLUtils(String fileName){
long lasting = System.currentTimeMillis();
try {
File f = new File(fileName);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("RULE");
//顺序读取标签
for (int i = 0; i < nl.getLength(); i++) {
FILELD = doc.getElementsByTagName("FILELD").item(i).getFirstChild().getNodeValue();
CONTAINS = doc.getElementsByTagName("CONTAINS").item(i).getFirstChild().getNodeValue();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.通过http请求头获取用户ip工具类
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
public class HttpUtils {
/**
*
* @Param request 请求信息
* @author bobo
*/
public static String getIp(HttpServletRequest request) {
if (request == null)
return "";
String ip = request.getHeader("X-Requested-For");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if ("0:0:0:0:0:0:0:1".equals(ip)) {
return "127.0.0.1";
}
return ip;
}
public static boolean isLocalHost(String ip) {
return "127.0.0.1".equals(ip) || "localhost".equals(ip);
}
}