ServletUtils.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.ozs.common.utils;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.URLDecoder;
  5. import java.net.URLEncoder;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import javax.servlet.http.HttpSession;
  13. import org.springframework.web.context.request.RequestAttributes;
  14. import org.springframework.web.context.request.RequestContextHolder;
  15. import org.springframework.web.context.request.ServletRequestAttributes;
  16. import com.ozs.common.constant.Constants;
  17. import com.ozs.common.core.text.Convert;
  18. /**
  19. * 客户端工具类
  20. *
  21. * @author ruoyi
  22. */
  23. public class ServletUtils
  24. {
  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. * 获得所有请求参数
  69. *
  70. * @param request 请求对象{@link ServletRequest}
  71. * @return Map
  72. */
  73. public static Map<String, String[]> getParams(ServletRequest request)
  74. {
  75. final Map<String, String[]> map = request.getParameterMap();
  76. return Collections.unmodifiableMap(map);
  77. }
  78. /**
  79. * 获得所有请求参数
  80. *
  81. * @param request 请求对象{@link ServletRequest}
  82. * @return Map
  83. */
  84. public static Map<String, String> getParamMap(ServletRequest request)
  85. {
  86. Map<String, String> params = new HashMap<>();
  87. for (Map.Entry<String, String[]> entry : getParams(request).entrySet())
  88. {
  89. params.put(entry.getKey(), StringUtils.join(entry.getValue(), ","));
  90. }
  91. return params;
  92. }
  93. /**
  94. * 获取request
  95. */
  96. public static HttpServletRequest getRequest()
  97. {
  98. return getRequestAttributes().getRequest();
  99. }
  100. /**
  101. * 获取response
  102. */
  103. public static HttpServletResponse getResponse()
  104. {
  105. return getRequestAttributes().getResponse();
  106. }
  107. /**
  108. * 获取session
  109. */
  110. public static HttpSession getSession()
  111. {
  112. return getRequest().getSession();
  113. }
  114. public static ServletRequestAttributes getRequestAttributes()
  115. {
  116. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  117. return (ServletRequestAttributes) attributes;
  118. }
  119. /**
  120. * 将字符串渲染到客户端
  121. *
  122. * @param response 渲染对象
  123. * @param string 待渲染的字符串
  124. */
  125. public static void renderString(HttpServletResponse response, String string)
  126. {
  127. try
  128. {
  129. response.setStatus(200);
  130. response.setContentType("application/json");
  131. response.setCharacterEncoding("utf-8");
  132. response.getWriter().print(string);
  133. }
  134. catch (IOException e)
  135. {
  136. e.printStackTrace();
  137. }
  138. }
  139. /**
  140. * 是否是Ajax异步请求
  141. *
  142. * @param request
  143. */
  144. public static boolean isAjaxRequest(HttpServletRequest request)
  145. {
  146. String accept = request.getHeader("accept");
  147. if (accept != null && accept.contains("application/json"))
  148. {
  149. return true;
  150. }
  151. String xRequestedWith = request.getHeader("X-Requested-With");
  152. if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
  153. {
  154. return true;
  155. }
  156. String uri = request.getRequestURI();
  157. if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
  158. {
  159. return true;
  160. }
  161. String ajax = request.getParameter("__ajax");
  162. return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
  163. }
  164. /**
  165. * 内容编码
  166. *
  167. * @param str 内容
  168. * @return 编码后的内容
  169. */
  170. public static String urlEncode(String str)
  171. {
  172. try
  173. {
  174. return URLEncoder.encode(str, Constants.UTF8);
  175. }
  176. catch (UnsupportedEncodingException e)
  177. {
  178. return StringUtils.EMPTY;
  179. }
  180. }
  181. /**
  182. * 内容解码
  183. *
  184. * @param str 内容
  185. * @return 解码后的内容
  186. */
  187. public static String urlDecode(String str)
  188. {
  189. try
  190. {
  191. return URLDecoder.decode(str, Constants.UTF8);
  192. }
  193. catch (UnsupportedEncodingException e)
  194. {
  195. return StringUtils.EMPTY;
  196. }
  197. }
  198. }