ServletUtils.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.zhongzheng.common.utils;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.util.StrUtil;
  4. import org.springframework.web.context.request.RequestAttributes;
  5. import org.springframework.web.context.request.RequestContextHolder;
  6. import org.springframework.web.context.request.ServletRequestAttributes;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10. import java.io.IOException;
  11. /**
  12. * 客户端工具类
  13. *
  14. * @author zhongzheng
  15. */
  16. public class ServletUtils
  17. {
  18. /**
  19. * 获取模块编码参数
  20. */
  21. public static String getEncoded(String tag)
  22. {
  23. String time = String.valueOf(System.currentTimeMillis()/1000);
  24. return tag+time.substring(1)+(Math.random()*10);
  25. }
  26. /**
  27. * 获取String参数
  28. */
  29. public static String getParameter(String name)
  30. {
  31. return getRequest().getParameter(name);
  32. }
  33. /**
  34. * 获取String参数
  35. */
  36. public static String getParameter(String name, String defaultValue)
  37. {
  38. return Convert.toStr(getRequest().getParameter(name), defaultValue);
  39. }
  40. /**
  41. * 获取Integer参数
  42. */
  43. public static Integer getParameterToInt(String name)
  44. {
  45. return Convert.toInt(getRequest().getParameter(name));
  46. }
  47. /**
  48. * 获取Integer参数
  49. */
  50. public static Integer getParameterToInt(String name, Integer defaultValue)
  51. {
  52. return Convert.toInt(getRequest().getParameter(name), defaultValue);
  53. }
  54. /**
  55. * 获取request
  56. */
  57. public static HttpServletRequest getRequest()
  58. {
  59. return getRequestAttributes().getRequest();
  60. }
  61. /**
  62. * 获取response
  63. */
  64. public static HttpServletResponse getResponse()
  65. {
  66. return getRequestAttributes().getResponse();
  67. }
  68. /**
  69. * 获取session
  70. */
  71. public static HttpSession getSession()
  72. {
  73. return getRequest().getSession();
  74. }
  75. public static ServletRequestAttributes getRequestAttributes()
  76. {
  77. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  78. return (ServletRequestAttributes) attributes;
  79. }
  80. /**
  81. * 将字符串渲染到客户端
  82. *
  83. * @param response 渲染对象
  84. * @param string 待渲染的字符串
  85. * @return null
  86. */
  87. public static String renderString(HttpServletResponse response, String string)
  88. {
  89. try
  90. {
  91. response.setStatus(200);
  92. response.setContentType("application/json");
  93. response.setCharacterEncoding("utf-8");
  94. response.getWriter().print(string);
  95. }
  96. catch (IOException e)
  97. {
  98. e.printStackTrace();
  99. }
  100. return null;
  101. }
  102. /**
  103. * 是否是Ajax异步请求
  104. *
  105. * @param request
  106. */
  107. public static boolean isAjaxRequest(HttpServletRequest request)
  108. {
  109. String accept = request.getHeader("accept");
  110. if (accept != null && accept.indexOf("application/json") != -1)
  111. {
  112. return true;
  113. }
  114. String xRequestedWith = request.getHeader("X-Requested-With");
  115. if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
  116. {
  117. return true;
  118. }
  119. String uri = request.getRequestURI();
  120. if (StrUtil.equalsAnyIgnoreCase(uri, ".json", ".xml"))
  121. {
  122. return true;
  123. }
  124. String ajax = request.getParameter("__ajax");
  125. if (StrUtil.equalsAnyIgnoreCase(ajax, "json", "xml"))
  126. {
  127. return true;
  128. }
  129. return false;
  130. }
  131. }