ServletUtils.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public static HttpServletRequest httpServletRequest;
  19. /**
  20. * 获取模块编码参数
  21. */
  22. public static String getEncoded(String tag)
  23. {
  24. String time = String.valueOf(System.currentTimeMillis()/1000);
  25. return tag+Integer.valueOf(time.substring(1))+(int)((Math.random()*10));
  26. }
  27. //导入生成编号使用
  28. public static String getImportEncoded(String tag)
  29. {
  30. String time = String.valueOf(System.currentTimeMillis()/1000);
  31. return tag+Integer.valueOf(time.substring(2));
  32. }
  33. /**
  34. * 获取String参数
  35. */
  36. public static String getParameter(String name)
  37. {
  38. return getRequest().getParameter(name);
  39. }
  40. /**
  41. * 获取String参数
  42. */
  43. public static String getParameter(String name, String defaultValue)
  44. {
  45. return Convert.toStr(getRequest().getParameter(name), defaultValue);
  46. }
  47. /**
  48. * 获取Integer参数
  49. */
  50. public static Integer getParameterToInt(String name)
  51. {
  52. return Convert.toInt(getRequest().getParameter(name));
  53. }
  54. /**
  55. * 获取Integer参数
  56. */
  57. public static Integer getParameterToInt(String name, Integer defaultValue)
  58. {
  59. return Convert.toInt(getRequest().getParameter(name), defaultValue);
  60. }
  61. /**
  62. * 获取request
  63. */
  64. public static HttpServletRequest getRequest()
  65. {
  66. if(getRequestAttributes()!=null){
  67. return getRequestAttributes().getRequest();
  68. }else{
  69. return httpServletRequest;
  70. }
  71. }
  72. /**
  73. * 获取response
  74. */
  75. public static HttpServletResponse getResponse()
  76. {
  77. return getRequestAttributes().getResponse();
  78. }
  79. /**
  80. * 获取session
  81. */
  82. public static HttpSession getSession()
  83. {
  84. return getRequest().getSession();
  85. }
  86. public static ServletRequestAttributes getRequestAttributes()
  87. {
  88. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  89. return (ServletRequestAttributes) attributes;
  90. }
  91. /**
  92. * 将字符串渲染到客户端
  93. *
  94. * @param response 渲染对象
  95. * @param string 待渲染的字符串
  96. * @return null
  97. */
  98. public static String renderString(HttpServletResponse response, String string)
  99. {
  100. try
  101. {
  102. response.setStatus(200);
  103. response.setContentType("application/json");
  104. response.setCharacterEncoding("utf-8");
  105. response.getWriter().print(string);
  106. }
  107. catch (IOException e)
  108. {
  109. e.printStackTrace();
  110. }
  111. return null;
  112. }
  113. /**
  114. * 是否是Ajax异步请求
  115. *
  116. * @param request
  117. */
  118. public static boolean isAjaxRequest(HttpServletRequest request)
  119. {
  120. String accept = request.getHeader("accept");
  121. if (accept != null && accept.indexOf("application/json") != -1)
  122. {
  123. return true;
  124. }
  125. String xRequestedWith = request.getHeader("X-Requested-With");
  126. if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
  127. {
  128. return true;
  129. }
  130. String uri = request.getRequestURI();
  131. if (StrUtil.equalsAnyIgnoreCase(uri, ".json", ".xml"))
  132. {
  133. return true;
  134. }
  135. String ajax = request.getParameter("__ajax");
  136. if (StrUtil.equalsAnyIgnoreCase(ajax, "json", "xml"))
  137. {
  138. return true;
  139. }
  140. return false;
  141. }
  142. }