ServletUtils.java 4.6 KB

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