当前位置:   article > 正文

ServletUtils工具类

servletutils
  1. import com.zcys.admin.common.responses.Response;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.http.MediaType;
  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.PrintWriter;
  11. import java.io.UnsupportedEncodingException;
  12. import java.net.URLDecoder;
  13. import java.net.URLEncoder;
  14. import java.nio.charset.StandardCharsets;
  15. import java.util.Objects;
  16. /**
  17. * 客户端响应工具类
  18. */
  19. @Slf4j
  20. public class ServletUtils {
  21. /**
  22. * 获取String参数
  23. */
  24. public static String getParameter(String name) {
  25. return getRequest().getParameter(name);
  26. }
  27. /**
  28. * 获取String参数
  29. */
  30. public static String getHeader(String name) {
  31. return getRequest().getHeader(name);
  32. }
  33. /**
  34. * 获取String参数
  35. */
  36. public static String getParameter(String name, String defaultValue) {
  37. return ConvertUtils.toStr(getRequest().getParameter(name), defaultValue);
  38. }
  39. /**
  40. * 获取Integer参数
  41. */
  42. public static Integer getParameterToInt(String name) {
  43. return ConvertUtils.toInt(getRequest().getParameter(name));
  44. }
  45. /**
  46. * 获取Integer参数
  47. */
  48. public static Integer getParameterToInt(String name, Integer defaultValue) {
  49. return ConvertUtils.toInt(getRequest().getParameter(name), defaultValue);
  50. }
  51. /**
  52. * 获取Boolean参数
  53. */
  54. public static Boolean getParameterToBool(String name) {
  55. return ConvertUtils.toBool(getRequest().getParameter(name));
  56. }
  57. /**
  58. * 获取Boolean参数
  59. */
  60. public static Boolean getParameterToBool(String name, Boolean defaultValue) {
  61. return ConvertUtils.toBool(getRequest().getParameter(name), defaultValue);
  62. }
  63. /**
  64. * 获取request
  65. */
  66. public static HttpServletRequest getRequest() {
  67. return getRequestAttributes().getRequest();
  68. }
  69. /**
  70. * 获取response
  71. */
  72. public static HttpServletResponse getResponse() {
  73. return getRequestAttributes().getResponse();
  74. }
  75. /**
  76. * 获取session
  77. */
  78. public static HttpSession getSession() {
  79. return getRequest().getSession();
  80. }
  81. public static ServletRequestAttributes getRequestAttributes() {
  82. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  83. return (ServletRequestAttributes) attributes;
  84. }
  85. /**
  86. * 将字符串渲染到客户端
  87. *
  88. * @param response 渲染对象
  89. * @param failed 响应消息
  90. */
  91. public static void responseJson(HttpServletResponse response, Response<String> failed) {
  92. if (Objects.isNull(failed)) {
  93. return;
  94. }
  95. response.setCharacterEncoding(StandardCharsets.UTF_8.name());
  96. response.setContentType(MediaType.APPLICATION_JSON_VALUE);
  97. try (PrintWriter writer = response.getWriter()) {
  98. String json = JSONUtils.serialize(failed);
  99. writer.write(json);
  100. writer.flush();
  101. } catch (Exception e) {
  102. log.info("响应失败:", e);
  103. }
  104. }
  105. /**
  106. * 内容编码
  107. *
  108. * @param str 内容
  109. * @return 编码后的内容
  110. */
  111. public static String urlEncode(String str) {
  112. try {
  113. return URLEncoder.encode(str, StandardCharsets.UTF_8.name());
  114. } catch (UnsupportedEncodingException e) {
  115. log.info("内容编码失败:", e);
  116. return StringUtils.EMPTY;
  117. }
  118. }
  119. /**
  120. * 内容解码
  121. *
  122. * @param str 内容
  123. * @return 解码后的内容
  124. */
  125. public static String urlDecode(String str) {
  126. try {
  127. return URLDecoder.decode(str, StandardCharsets.UTF_8.name());
  128. } catch (UnsupportedEncodingException e) {
  129. log.info("内容解码失败:", e);
  130. return StringUtils.EMPTY;
  131. }
  132. }
  133. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/187011
推荐阅读
相关标签
  

闽ICP备14008679号