赞
踩
-
- import com.zcys.admin.common.responses.Response;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.http.MediaType;
- import org.springframework.web.context.request.RequestAttributes;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.util.Objects;
-
- /**
- * 客户端响应工具类
- */
- @Slf4j
- public class ServletUtils {
-
- /**
- * 获取String参数
- */
- public static String getParameter(String name) {
- return getRequest().getParameter(name);
- }
-
- /**
- * 获取String参数
- */
- public static String getHeader(String name) {
- return getRequest().getHeader(name);
- }
-
- /**
- * 获取String参数
- */
- public static String getParameter(String name, String defaultValue) {
- return ConvertUtils.toStr(getRequest().getParameter(name), defaultValue);
- }
-
- /**
- * 获取Integer参数
- */
- public static Integer getParameterToInt(String name) {
- return ConvertUtils.toInt(getRequest().getParameter(name));
- }
-
- /**
- * 获取Integer参数
- */
- public static Integer getParameterToInt(String name, Integer defaultValue) {
- return ConvertUtils.toInt(getRequest().getParameter(name), defaultValue);
- }
-
- /**
- * 获取Boolean参数
- */
- public static Boolean getParameterToBool(String name) {
- return ConvertUtils.toBool(getRequest().getParameter(name));
- }
-
- /**
- * 获取Boolean参数
- */
- public static Boolean getParameterToBool(String name, Boolean defaultValue) {
- return ConvertUtils.toBool(getRequest().getParameter(name), defaultValue);
- }
-
- /**
- * 获取request
- */
- public static HttpServletRequest getRequest() {
- return getRequestAttributes().getRequest();
- }
-
- /**
- * 获取response
- */
- public static HttpServletResponse getResponse() {
- return getRequestAttributes().getResponse();
- }
-
- /**
- * 获取session
- */
- public static HttpSession getSession() {
- return getRequest().getSession();
- }
-
- public static ServletRequestAttributes getRequestAttributes() {
- RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
- return (ServletRequestAttributes) attributes;
- }
-
- /**
- * 将字符串渲染到客户端
- *
- * @param response 渲染对象
- * @param failed 响应消息
- */
- public static void responseJson(HttpServletResponse response, Response<String> failed) {
- if (Objects.isNull(failed)) {
- return;
- }
-
- response.setCharacterEncoding(StandardCharsets.UTF_8.name());
- response.setContentType(MediaType.APPLICATION_JSON_VALUE);
-
- try (PrintWriter writer = response.getWriter()) {
- String json = JSONUtils.serialize(failed);
- writer.write(json);
- writer.flush();
- } catch (Exception e) {
- log.info("响应失败:", e);
- }
- }
-
- /**
- * 内容编码
- *
- * @param str 内容
- * @return 编码后的内容
- */
- public static String urlEncode(String str) {
- try {
- return URLEncoder.encode(str, StandardCharsets.UTF_8.name());
- } catch (UnsupportedEncodingException e) {
- log.info("内容编码失败:", e);
- return StringUtils.EMPTY;
- }
- }
-
- /**
- * 内容解码
- *
- * @param str 内容
- * @return 解码后的内容
- */
- public static String urlDecode(String str) {
- try {
- return URLDecoder.decode(str, StandardCharsets.UTF_8.name());
- } catch (UnsupportedEncodingException e) {
- log.info("内容解码失败:", e);
- return StringUtils.EMPTY;
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。