WebUtil.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.ytpm.attach;
  2. import cn.hutool.core.util.StrUtil;
  3. import org.springframework.web.util.WebUtils;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.InetAddress;
  9. import java.net.URLEncoder;
  10. import java.net.UnknownHostException;
  11. import java.util.Enumeration;
  12. public class WebUtil extends WebUtils {
  13. /**
  14. * 获取请求参数值,依次从paramter,header,cookie中查找
  15. *
  16. * @param request 请求
  17. * @param name 参数名
  18. * @return
  19. */
  20. public static String getRequestParamValue(HttpServletRequest request, String name) {
  21. String value = findParameterValue(request, name);
  22. if (StrUtil.isBlank(value)) {
  23. value = getHeaderValue(request, name);
  24. }
  25. if (StrUtil.isBlank(value)) {
  26. value = getCookieValue(request, name);
  27. }
  28. return value;
  29. }
  30. /**
  31. * 获取cookie值
  32. *
  33. * @param request 请求
  34. * @param name 参数名
  35. * @return
  36. */
  37. public static String getCookieValue(HttpServletRequest request, String name) {
  38. Cookie cookie = getCookie(request, name);
  39. String value = null;
  40. if (cookie != null) {
  41. value = cookie.getValue();
  42. }
  43. return value;
  44. }
  45. /**
  46. * 获取请求头header值
  47. *
  48. * @param request 请求
  49. * @param name 参数名
  50. * @return
  51. */
  52. public static String getHeaderValue(HttpServletRequest request, String name) {
  53. String value = null;
  54. Enumeration<String> headerNames = request.getHeaderNames();
  55. while (headerNames.hasMoreElements()) {
  56. String key = headerNames.nextElement();
  57. if (key.equals(name)) {
  58. value = request.getHeader(name);
  59. }
  60. }
  61. return value;
  62. }
  63. /**
  64. * 响应初始化(下载用)
  65. *
  66. * @param response 响应
  67. * @param fileName 文件名 例exp.xls
  68. * @throws UnsupportedEncodingException 转码异常
  69. */
  70. public static void initDownloadHeader(HttpServletResponse response, String fileName) {
  71. response.reset();
  72. try {
  73. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  74. response.setContentType("application/octet-stream");
  75. response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
  76. response.setCharacterEncoding("UTF-8");
  77. } catch (UnsupportedEncodingException e) {
  78. throw new RuntimeException(e.getMessage());
  79. }
  80. }
  81. /**
  82. * 响应初始化(pdf在线展示用)
  83. *
  84. * @param response 响应
  85. * @param fileName 文件名 例exp.xls
  86. * @throws UnsupportedEncodingException 转码异常
  87. */
  88. public static void initPdfOnlineHeader(HttpServletResponse response, String fileName) {
  89. response.reset();
  90. try {
  91. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  92. response.setContentType("application/pdf");
  93. response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
  94. response.setCharacterEncoding("UTF-8");
  95. } catch (UnsupportedEncodingException e) {
  96. throw new RuntimeException(e.getMessage());
  97. }
  98. }
  99. /**
  100. * 获取用户Ip地址
  101. *
  102. * @param request
  103. * @return
  104. */
  105. public static String getIpAddress(HttpServletRequest request) {
  106. String ip = request.getHeader("x-forwarded-for");
  107. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  108. ip = request.getHeader("Proxy-Client-IP");
  109. }
  110. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  111. ip = request.getHeader("WL-Proxy-Client-IP");
  112. }
  113. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  114. ip = request.getHeader("HTTP_CLIENT_IP");
  115. }
  116. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  117. ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  118. }
  119. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  120. ip = request.getRemoteAddr();
  121. if ("127.0.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
  122. //根据网卡取本机配置的IP
  123. InetAddress inet = null;
  124. try {
  125. inet = InetAddress.getLocalHost();
  126. } catch (UnknownHostException e) {
  127. e.printStackTrace();
  128. }
  129. ip = inet.getHostAddress();
  130. }
  131. }
  132. return ip;
  133. }
  134. }