ServletUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * 定义移动端请求的所有可能类型
  23. */
  24. private final static String[] agent = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" };
  25. /**
  26. * 获取String参数
  27. */
  28. public static String getParameter(String name)
  29. {
  30. return getRequest().getParameter(name);
  31. }
  32. /**
  33. * 获取String参数
  34. */
  35. public static String getParameter(String name, String defaultValue)
  36. {
  37. return Convert.toStr(getRequest().getParameter(name), defaultValue);
  38. }
  39. /**
  40. * 获取Integer参数
  41. */
  42. public static Integer getParameterToInt(String name)
  43. {
  44. return Convert.toInt(getRequest().getParameter(name));
  45. }
  46. /**
  47. * 获取Integer参数
  48. */
  49. public static Integer getParameterToInt(String name, Integer defaultValue)
  50. {
  51. return Convert.toInt(getRequest().getParameter(name), defaultValue);
  52. }
  53. /**
  54. * 获取Boolean参数
  55. */
  56. public static Boolean getParameterToBool(String name)
  57. {
  58. return Convert.toBool(getRequest().getParameter(name));
  59. }
  60. /**
  61. * 获取Boolean参数
  62. */
  63. public static Boolean getParameterToBool(String name, Boolean defaultValue)
  64. {
  65. return Convert.toBool(getRequest().getParameter(name), defaultValue);
  66. }
  67. /**
  68. * 获取request
  69. */
  70. public static HttpServletRequest getRequest()
  71. {
  72. return getRequestAttributes().getRequest();
  73. }
  74. /**
  75. * 获取response
  76. */
  77. public static HttpServletResponse getResponse()
  78. {
  79. return getRequestAttributes().getResponse();
  80. }
  81. /**
  82. * 获取session
  83. */
  84. public static HttpSession getSession()
  85. {
  86. return getRequest().getSession();
  87. }
  88. public static ServletRequestAttributes getRequestAttributes()
  89. {
  90. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  91. return (ServletRequestAttributes) attributes;
  92. }
  93. /**
  94. * 将字符串渲染到客户端
  95. *
  96. * @param response 渲染对象
  97. * @param string 待渲染的字符串
  98. * @return null
  99. */
  100. public static String renderString(HttpServletResponse response, String string)
  101. {
  102. try
  103. {
  104. response.setContentType("application/json");
  105. response.setCharacterEncoding("utf-8");
  106. response.getWriter().print(string);
  107. }
  108. catch (IOException e)
  109. {
  110. e.printStackTrace();
  111. }
  112. return null;
  113. }
  114. /**
  115. * 是否是Ajax异步请求
  116. *
  117. * @param request
  118. */
  119. public static boolean isAjaxRequest(HttpServletRequest request)
  120. {
  121. String accept = request.getHeader("accept");
  122. if (accept != null && accept.contains("application/json"))
  123. {
  124. return true;
  125. }
  126. String xRequestedWith = request.getHeader("X-Requested-With");
  127. if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
  128. {
  129. return true;
  130. }
  131. String uri = request.getRequestURI();
  132. if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
  133. {
  134. return true;
  135. }
  136. String ajax = request.getParameter("__ajax");
  137. return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
  138. }
  139. /**
  140. * 判断User-Agent 是不是来自于手机
  141. */
  142. public static boolean checkAgentIsMobile(String ua)
  143. {
  144. boolean flag = false;
  145. if (!ua.contains("Windows NT") || (ua.contains("Windows NT") && ua.contains("compatible; MSIE 9.0;")))
  146. {
  147. // 排除 苹果桌面系统
  148. if (!ua.contains("Windows NT") && !ua.contains("Macintosh"))
  149. {
  150. for (String item : agent)
  151. {
  152. if (ua.contains(item))
  153. {
  154. flag = true;
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. return flag;
  161. }
  162. /**
  163. * 内容编码
  164. *
  165. * @param str 内容
  166. * @return 编码后的内容
  167. */
  168. public static String urlEncode(String str)
  169. {
  170. try
  171. {
  172. return URLEncoder.encode(str, Constants.UTF8);
  173. }
  174. catch (UnsupportedEncodingException e)
  175. {
  176. return StringUtils.EMPTY;
  177. }
  178. }
  179. /**
  180. * 内容解码
  181. *
  182. * @param str 内容
  183. * @return 解码后的内容
  184. */
  185. public static String urlDecode(String str)
  186. {
  187. try
  188. {
  189. return URLDecoder.decode(str, Constants.UTF8);
  190. }
  191. catch (UnsupportedEncodingException e)
  192. {
  193. return StringUtils.EMPTY;
  194. }
  195. }
  196. }