Commit c5b29911 authored by Quxl's avatar Quxl
parents 9643dbbf 59d59db7
package com.egolm.common.web;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import com.egolm.common.StringUtil;
import com.egolm.common.exception.PluginException;
public class ServletUtil {
public static String remoteIp(HttpServletRequest request) {
String ip = request.getHeader("X-Real_IP");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getRemoteAddr();
}
String[] ips = ip.trim().split(", ");
return ips[ips.length - 1].trim();
}
public static String getLocalIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
throw new PluginException(e);
}
}
/**
*
* @Description 读取传入的json字符
* @param request
* @return
* @return String
* @throws
* @author 曲欣亮
* @date 2016年5月17日
* @since 2016年5月17日
*/
public static String readReqJson(HttpServletRequest request) {
String json = "";
try {
Enumeration<?> names = request.getParameterNames();
if(names.hasMoreElements()) {
json = (String)names.nextElement();
}
if(json == null || json.length() == 0) {
json = StringUtil.read(request.getReader());
}
} catch (Exception e) {
e.printStackTrace();
}
try {
String encodeJson = URLDecoder.decode(json, "UTF-8");
return encodeJson;
} catch (Exception e) {
e.printStackTrace();
return json;
}
}
}
package com.egolm.common.web;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import com.egolm.common.StringUtil;
import com.egolm.common.exception.PluginException;
public class ServletUtil {
public static String remoteIp(HttpServletRequest request) {
String ip = request.getHeader("X-Real_IP");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
ip = request.getRemoteAddr();
}
String[] ips = ip.trim().split(", ");
return ips[ips.length - 1].trim();
}
public static String getLocalIP() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
throw new PluginException(e);
}
}
/**
*
* @Description 读取传入的json字符
* @param request
* @return
* @return String
* @throws
* @author 曲欣亮
* @date 2016年5月17日
* @since 2016年5月17日
*/
public static String readReqJson(HttpServletRequest request) {
String json = "";
try {
Enumeration<?> names = request.getParameterNames();
if(names.hasMoreElements()) {
json = (String)names.nextElement();
}
if(json == null || json.length() == 0) {
json = StringUtil.read(request.getReader());
}
} catch (Exception e) {
e.printStackTrace();
}
try {
String encodeJson = URLDecoder.decode(json, "UTF-8");
return encodeJson;
} catch (Exception e) {
e.printStackTrace();
return json;
}
}
public static String covertFileName(HttpServletRequest request, String fileName)throws UnsupportedEncodingException {
String userAgent = request.getHeader("User-Agent").toLowerCase();
System.out.println(userAgent);
String excelFileName = "";
if (userAgent.indexOf("msie") != -1) {// IE浏览器
excelFileName = URLEncoder.encode(fileName, "UTF8");
} else if (userAgent.indexOf("firefox") != -1) {// google,火狐浏览器
excelFileName = new String(fileName.getBytes(), "ISO-8859-1");
} else {
excelFileName = URLEncoder.encode(fileName, "UTF8");// 其他浏览器
}
return excelFileName;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment