ServletUtils.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.ruoyi.common.utils;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.extra.servlet.ServletUtil;
  4. import cn.hutool.http.HttpStatus;
  5. import com.ruoyi.common.constant.Constants;
  6. import lombok.AccessLevel;
  7. import lombok.NoArgsConstructor;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.web.context.request.RequestAttributes;
  10. import org.springframework.web.context.request.RequestContextHolder;
  11. import org.springframework.web.context.request.ServletRequestAttributes;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15. import java.io.IOException;
  16. import java.io.UnsupportedEncodingException;
  17. import java.net.URLDecoder;
  18. import java.net.URLEncoder;
  19. import java.nio.charset.StandardCharsets;
  20. /**
  21. * 客户端工具类
  22. *
  23. * @author ruoyi
  24. */
  25. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  26. public class ServletUtils extends ServletUtil {
  27. /**
  28. * 获取String参数
  29. */
  30. public static String getParameter(String name) {
  31. return getRequest().getParameter(name);
  32. }
  33. /**
  34. * 获取String参数
  35. */
  36. public static String getParameter(String name, String defaultValue) {
  37. return Convert.toStr(getRequest().getParameter(name), defaultValue);
  38. }
  39. /**
  40. * 获取Integer参数
  41. */
  42. public static Integer getParameterToInt(String name) {
  43. return Convert.toInt(getRequest().getParameter(name));
  44. }
  45. /**
  46. * 获取Integer参数
  47. */
  48. public static Integer getParameterToInt(String name, Integer defaultValue) {
  49. return Convert.toInt(getRequest().getParameter(name), defaultValue);
  50. }
  51. /**
  52. * 获取Boolean参数
  53. */
  54. public static Boolean getParameterToBool(String name) {
  55. return Convert.toBool(getRequest().getParameter(name));
  56. }
  57. /**
  58. * 获取Boolean参数
  59. */
  60. public static Boolean getParameterToBool(String name, Boolean defaultValue) {
  61. return Convert.toBool(getRequest().getParameter(name), defaultValue);
  62. }
  63. /**
  64. * 获取request
  65. */
  66. public static HttpServletRequest getRequest() {
  67. return getRequestAttributes().getRequest();
  68. }
  69. /**
  70. * 获取response
  71. */
  72. public static HttpServletResponse getResponse() {
  73. return getRequestAttributes().getResponse();
  74. }
  75. /**
  76. * 获取session
  77. */
  78. public static HttpSession getSession() {
  79. return getRequest().getSession();
  80. }
  81. public static ServletRequestAttributes getRequestAttributes() {
  82. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  83. return (ServletRequestAttributes) attributes;
  84. }
  85. /**
  86. * 将字符串渲染到客户端
  87. *
  88. * @param response 渲染对象
  89. * @param string 待渲染的字符串
  90. */
  91. public static void renderString(HttpServletResponse response, String string) {
  92. try {
  93. response.setStatus(HttpStatus.HTTP_OK);
  94. response.setContentType(MediaType.APPLICATION_JSON_VALUE);
  95. response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
  96. response.getWriter().print(string);
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. /**
  102. * 是否是Ajax异步请求
  103. *
  104. * @param request
  105. */
  106. public static boolean isAjaxRequest(HttpServletRequest request) {
  107. String accept = request.getHeader("accept");
  108. if (accept != null && accept.contains(MediaType.APPLICATION_JSON_VALUE)) {
  109. return true;
  110. }
  111. String xRequestedWith = request.getHeader("X-Requested-With");
  112. if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
  113. return true;
  114. }
  115. String uri = request.getRequestURI();
  116. if (StringUtils.equalsAnyIgnoreCase(uri, ".json", ".xml")) {
  117. return true;
  118. }
  119. String ajax = request.getParameter("__ajax");
  120. return StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml");
  121. }
  122. public static String getClientIP() {
  123. return getClientIP(getRequest());
  124. }
  125. /**
  126. * 内容编码
  127. *
  128. * @param str 内容
  129. * @return 编码后的内容
  130. */
  131. public static String urlEncode(String str) {
  132. try {
  133. return URLEncoder.encode(str, Constants.UTF8);
  134. } catch (UnsupportedEncodingException e) {
  135. return StringUtils.EMPTY;
  136. }
  137. }
  138. /**
  139. * 内容解码
  140. *
  141. * @param str 内容
  142. * @return 解码后的内容
  143. */
  144. public static String urlDecode(String str) {
  145. try {
  146. return URLDecoder.decode(str, Constants.UTF8);
  147. } catch (UnsupportedEncodingException e) {
  148. return StringUtils.EMPTY;
  149. }
  150. }
  151. }