ServletUtils.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 org.springframework.http.MediaType;
  6. import org.springframework.web.context.request.RequestAttributes;
  7. import org.springframework.web.context.request.RequestContextHolder;
  8. import org.springframework.web.context.request.ServletRequestAttributes;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. import java.io.IOException;
  13. import java.nio.charset.StandardCharsets;
  14. /**
  15. * 客户端工具类
  16. *
  17. * @author ruoyi
  18. */
  19. public class ServletUtils extends ServletUtil {
  20. /**
  21. * 获取String参数
  22. */
  23. public static String getParameter(String name) {
  24. return getRequest().getParameter(name);
  25. }
  26. /**
  27. * 获取String参数
  28. */
  29. public static String getParameter(String name, String defaultValue) {
  30. return Convert.toStr(getRequest().getParameter(name), defaultValue);
  31. }
  32. /**
  33. * 获取Integer参数
  34. */
  35. public static Integer getParameterToInt(String name) {
  36. return Convert.toInt(getRequest().getParameter(name));
  37. }
  38. /**
  39. * 获取Integer参数
  40. */
  41. public static Integer getParameterToInt(String name, Integer defaultValue) {
  42. return Convert.toInt(getRequest().getParameter(name), defaultValue);
  43. }
  44. /**
  45. * 获取Boolean参数
  46. */
  47. public static Boolean getParameterToBool(String name) {
  48. return Convert.toBool(getRequest().getParameter(name));
  49. }
  50. /**
  51. * 获取Boolean参数
  52. */
  53. public static Boolean getParameterToBool(String name, Boolean defaultValue) {
  54. return Convert.toBool(getRequest().getParameter(name), defaultValue);
  55. }
  56. /**
  57. * 获取request
  58. */
  59. public static HttpServletRequest getRequest() {
  60. return getRequestAttributes().getRequest();
  61. }
  62. /**
  63. * 获取response
  64. */
  65. public static HttpServletResponse getResponse() {
  66. return getRequestAttributes().getResponse();
  67. }
  68. /**
  69. * 获取session
  70. */
  71. public static HttpSession getSession() {
  72. return getRequest().getSession();
  73. }
  74. public static ServletRequestAttributes getRequestAttributes() {
  75. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  76. return (ServletRequestAttributes) attributes;
  77. }
  78. /**
  79. * 将字符串渲染到客户端
  80. *
  81. * @param response 渲染对象
  82. * @param string 待渲染的字符串
  83. * @return null
  84. */
  85. public static String renderString(HttpServletResponse response, String string) {
  86. try {
  87. response.setStatus(HttpStatus.HTTP_OK);
  88. response.setContentType(MediaType.APPLICATION_JSON_VALUE);
  89. response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
  90. response.getWriter().print(string);
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. return null;
  95. }
  96. /**
  97. * 是否是Ajax异步请求
  98. *
  99. * @param request
  100. */
  101. public static boolean isAjaxRequest(HttpServletRequest request) {
  102. String accept = request.getHeader("accept");
  103. if (accept != null && accept.indexOf("application/json") != -1) {
  104. return true;
  105. }
  106. String xRequestedWith = request.getHeader("X-Requested-With");
  107. if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
  108. return true;
  109. }
  110. String uri = request.getRequestURI();
  111. if (StringUtils.equalsAnyIgnoreCase(uri, ".json", ".xml")) {
  112. return true;
  113. }
  114. String ajax = request.getParameter("__ajax");
  115. if (StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml")) {
  116. return true;
  117. }
  118. return false;
  119. }
  120. public static String getClientIP() {
  121. return getClientIP(getRequest());
  122. }
  123. }