Commit c5b29911 authored by Quxl's avatar Quxl
parents 9643dbbf 59d59db7
package com.egolm.common.web; package com.egolm.common.web;
import java.net.InetAddress; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.URLDecoder;
import java.util.Enumeration; import java.net.URLEncoder;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest; import java.util.Enumeration;
import com.egolm.common.StringUtil; import javax.servlet.http.HttpServletRequest;
import com.egolm.common.exception.PluginException;
import com.egolm.common.StringUtil;
public class ServletUtil { import com.egolm.common.exception.PluginException;
public static String remoteIp(HttpServletRequest request) { public class ServletUtil {
String ip = request.getHeader("X-Real_IP");
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { public static String remoteIp(HttpServletRequest request) {
ip = request.getHeader("X-Forwarded-For"); String ip = request.getHeader("X-Real_IP");
} if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { ip = request.getHeader("X-Forwarded-For");
ip = request.getHeader("Proxy-Client-IP"); }
} if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { ip = request.getHeader("Proxy-Client-IP");
ip = request.getHeader("WL-Proxy-Client-IP"); }
} if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) { ip = request.getHeader("WL-Proxy-Client-IP");
ip = request.getRemoteAddr(); }
} if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {
String[] ips = ip.trim().split(", "); ip = request.getRemoteAddr();
return ips[ips.length - 1].trim(); }
} String[] ips = ip.trim().split(", ");
return ips[ips.length - 1].trim();
public static String getLocalIP() { }
try {
return InetAddress.getLocalHost().getHostAddress(); public static String getLocalIP() {
} catch (UnknownHostException e) { try {
throw new PluginException(e); return InetAddress.getLocalHost().getHostAddress();
} } catch (UnknownHostException e) {
} throw new PluginException(e);
/** }
* }
* @Description 读取传入的json字符 /**
* @param request *
* @return * @Description 读取传入的json字符
* @return String * @param request
* @throws * @return
* @author 曲欣亮 * @return String
* @date 2016年5月17日 * @throws
* @since 2016年5月17日 * @author 曲欣亮
*/ * @date 2016年5月17日
public static String readReqJson(HttpServletRequest request) { * @since 2016年5月17日
String json = ""; */
try { public static String readReqJson(HttpServletRequest request) {
Enumeration<?> names = request.getParameterNames(); String json = "";
if(names.hasMoreElements()) { try {
json = (String)names.nextElement(); Enumeration<?> names = request.getParameterNames();
} if(names.hasMoreElements()) {
if(json == null || json.length() == 0) { json = (String)names.nextElement();
json = StringUtil.read(request.getReader()); }
} if(json == null || json.length() == 0) {
} catch (Exception e) { json = StringUtil.read(request.getReader());
e.printStackTrace(); }
} } catch (Exception e) {
try { e.printStackTrace();
String encodeJson = URLDecoder.decode(json, "UTF-8"); }
return encodeJson; try {
} catch (Exception e) { String encodeJson = URLDecoder.decode(json, "UTF-8");
e.printStackTrace(); return encodeJson;
return json; } 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