48e5cc38db4b46bec0cbc1421829dd328fd7b459.svn-base 4.4 KB

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