ServletUtils.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * 定义移动端请求的所有可能类型
  19. */
  20. private final static String[] agent = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" };
  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. * @return null
  95. */
  96. public static String renderString(HttpServletResponse response, String string)
  97. {
  98. try
  99. {
  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. return null;
  109. }
  110. /**
  111. * 是否是Ajax异步请求
  112. *
  113. * @param request
  114. */
  115. public static boolean isAjaxRequest(HttpServletRequest request)
  116. {
  117. String accept = request.getHeader("accept");
  118. if (accept != null && accept.contains("application/json"))
  119. {
  120. return true;
  121. }
  122. String xRequestedWith = request.getHeader("X-Requested-With");
  123. if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
  124. {
  125. return true;
  126. }
  127. String uri = request.getRequestURI();
  128. if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
  129. {
  130. return true;
  131. }
  132. String ajax = request.getParameter("__ajax");
  133. return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
  134. }
  135. /**
  136. * 判断User-Agent 是不是来自于手机
  137. */
  138. public static boolean checkAgentIsMobile(String ua)
  139. {
  140. boolean flag = false;
  141. if (!ua.contains("Windows NT") || (ua.contains("Windows NT") && ua.contains("compatible; MSIE 9.0;")))
  142. {
  143. // 排除 苹果桌面系统
  144. if (!ua.contains("Windows NT") && !ua.contains("Macintosh"))
  145. {
  146. for (String item : agent)
  147. {
  148. if (ua.contains(item))
  149. {
  150. flag = true;
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. return flag;
  157. }
  158. }