ServletUtils.java 3.8 KB

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