ServletUtils.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package com.xlht.xlhtproject.utils;
  2. import org.springframework.web.context.request.RequestAttributes;
  3. import org.springframework.web.context.request.RequestContextHolder;
  4. import org.springframework.web.context.request.ServletRequestAttributes;
  5. import javax.servlet.ServletRequest;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. import java.io.IOException;
  10. import java.io.UnsupportedEncodingException;
  11. import java.net.URLDecoder;
  12. import java.net.URLEncoder;
  13. import java.util.Collections;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * 客户端工具类
  18. *
  19. *
  20. */
  21. public class ServletUtils
  22. {
  23. /**
  24. * 获取String参数
  25. */
  26. public static String getParameter(String name)
  27. {
  28. return getRequest().getParameter(name);
  29. }
  30. /**
  31. * 获取String参数
  32. */
  33. public static String getParameter(String name, String defaultValue)
  34. {
  35. return Convert.toStr(getRequest().getParameter(name), defaultValue);
  36. }
  37. /**
  38. * 获取Integer参数
  39. */
  40. public static Integer getParameterToInt(String name)
  41. {
  42. return Convert.toInt(getRequest().getParameter(name));
  43. }
  44. /**
  45. * 获取Integer参数
  46. */
  47. public static Integer getParameterToInt(String name, Integer defaultValue)
  48. {
  49. return Convert.toInt(getRequest().getParameter(name), defaultValue);
  50. }
  51. /**
  52. * 获取Boolean参数
  53. */
  54. public static Boolean getParameterToBool(String name)
  55. {
  56. return Convert.toBool(getRequest().getParameter(name));
  57. }
  58. /**
  59. * 获取Boolean参数
  60. */
  61. public static Boolean getParameterToBool(String name, Boolean defaultValue)
  62. {
  63. return Convert.toBool(getRequest().getParameter(name), defaultValue);
  64. }
  65. /**
  66. * 获得所有请求参数
  67. *
  68. * @param request 请求对象{@link ServletRequest}
  69. * @return Map
  70. */
  71. public static Map<String, String[]> getParams(ServletRequest request)
  72. {
  73. final Map<String, String[]> map = request.getParameterMap();
  74. return Collections.unmodifiableMap(map);
  75. }
  76. /**
  77. * 获得所有请求参数
  78. *
  79. * @param request 请求对象{@link ServletRequest}
  80. * @return Map
  81. */
  82. public static Map<String, String> getParamMap(ServletRequest request)
  83. {
  84. Map<String, String> params = new HashMap<>();
  85. for (Map.Entry<String, String[]> entry : getParams(request).entrySet())
  86. {
  87. params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
  88. }
  89. return params;
  90. }
  91. /**
  92. * 获取request
  93. */
  94. public static HttpServletRequest getRequest()
  95. {
  96. return getRequestAttributes().getRequest();
  97. }
  98. /**
  99. * 获取response
  100. */
  101. public static HttpServletResponse getResponse()
  102. {
  103. return getRequestAttributes().getResponse();
  104. }
  105. /**
  106. * 获取session
  107. */
  108. public static HttpSession getSession()
  109. {
  110. return getRequest().getSession();
  111. }
  112. public static ServletRequestAttributes getRequestAttributes()
  113. {
  114. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  115. return (ServletRequestAttributes) attributes;
  116. }
  117. /**
  118. * 将字符串渲染到客户端
  119. *
  120. * @param response 渲染对象
  121. * @param string 待渲染的字符串
  122. */
  123. public static void renderString(HttpServletResponse response, String string)
  124. {
  125. try
  126. {
  127. response.setStatus(200);
  128. response.setContentType("application/json");
  129. response.setCharacterEncoding("utf-8");
  130. response.getWriter().print(string);
  131. }
  132. catch (IOException e)
  133. {
  134. e.printStackTrace();
  135. }
  136. }
  137. /**
  138. * 是否是Ajax异步请求
  139. *
  140. * @param request
  141. */
  142. public static boolean isAjaxRequest(HttpServletRequest request)
  143. {
  144. String accept = request.getHeader("accept");
  145. if (accept != null && accept.contains("application/json"))
  146. {
  147. return true;
  148. }
  149. String xRequestedWith = request.getHeader("X-Requested-With");
  150. if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
  151. {
  152. return true;
  153. }
  154. String uri = request.getRequestURI();
  155. if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
  156. {
  157. return true;
  158. }
  159. String ajax = request.getParameter("__ajax");
  160. return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
  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, "UTF-8");
  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, "UTF-8");
  190. }
  191. catch (UnsupportedEncodingException e)
  192. {
  193. return StringUtils.EMPTY;
  194. }
  195. }
  196. }