GET请求中文乱码 ~~ 获取参数工具类,解放你的双手

背景

开发导出文件的功能,遇到了两个坑

  1. 用ajax请求后台的文件导出接口,不会导出
  2. 用window.location.href请求后台地址,但是中文乱码

那么,我们就来天天GET请求中文乱码的解决防范

解决方案

  1. 服务器配置

1 ) 浏 览 器 发 送 时 文 字 编 码 是 和 页 面 编 码 保 持 一 致 。 \color{red}1)浏览器发送时文字编码是和页面编码保持一致。 1
2 ) t o m c a t 中 接 收 请 求 没 有 设 置 编 码 的 情 况 下 , 默 认 使 用 I S O − 8859 − 1 编 码 。 \color{red}2)tomcat中接收请求没有设置编码的情况下,默认使用ISO-8859-1编码。 2tomcat使ISO88591
3 ) 页 面 编 码 使 用 U T F − 8 , g e t 方 式 自 然 使 用 U T F − 8 编 码 , 但 服 务 器 接 收 没 有 指 定 编 码 格 式 , 默 认 使 用 I S O − 8859 − 1 , 传 参 带 中 文 时 自 然 乱 码 。 \color{red}3)页面编码使用UTF-8,get方式自然使用UTF-8编码,但服务器接收没有指定编码格式,默认使用ISO-8859-1,传参带中文时自然乱码。 3使UTF8get使UTF8使ISO88591

tomcat的话可以在 server.xml 文件里指定编码格式为UTF-8。标签名:Connector 添加: URIEncoding=”UTF-8”

<Connector connectionTimeout="20000" port="8082" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

这 是 生 产 上 使 用 的 项 目 , 对 t o m c a t 配 置 不 合 适 , 不 够 兼 容 , 因 此 采 用 第 二 种 方 式 \color{blue}这是生产上使用的项目,对tomcat配置不合适,不够兼容,因此采用第二种方式 使tomcat

  1. 前台页面编码,后台解码
    如果不想在服务器上指定编码格式,那么可以在页面get提交时对中文进行url转码,后台进行解码即可。

前台页面:

var str=encodeURI(encodeURI(“这里是要加密的含有中文的字符串”));
或者
var str= escape(encodeURIComponent(“这里是要加密的含有中文的字符串”));
亲测第二种方法可以,第一种没试

在这里插入图片描述

String result= URLDecoder.decode(request.getParameter(“取到的加密字符串”), “utf-8);

在这里插入图片描述

工具类

对于很多的前端请求要么是POST,要么是GET,针对这两类请求,直接写工具类,以后就不需要一个个去获取了

POST请求获取参数

/**
	 * 获取请求中的参数值
	 * 
	 * @return
	 */
	public Map<String, Object> getParamMap() {
		Map<String, Object> parameters = new HashMap<String, Object>();
		Map map = getHttpRequest().getParameterMap();
		Set keys = map.keySet();
		for (Object key : keys) {
			if(!Objects.equal("_", key)) {
				parameters.put(key.toString(), this.getString(key.toString()));
			}
		}
		return parameters;
	}

/**
	 * 根据参数名从HttpRequest中获取String类型的参数值,无值则返回null .
	 * 
	 * @param key
	 *            .
	 * @return String or null .
	 */
	public String getString(String key) {
		String param = getHttpRequest().getParameter(key);	
		if (StringUtil.isEmpty(param)) {
			return param;
		} else {
			return param.trim();
		}
	}	
	
	/**
	 * 取得当前request
	 * 
	 * @return
	 */
	public HttpServletRequest getHttpRequest() {
		return ServletActionContext.getRequest();
	}

GET

/**
	 * 获取请求中的参数值
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public Map<String, Object> getParamMap_Utf8() {
		Map<String, Object> parameters = new HashMap<String, Object>();
		Map map = getHttpRequest().getParameterMap();
		Set keys = map.keySet();
		for (Object key : keys) {
			try {
				parameters.put(key.toString(), this.getString_UrlDecode_UTF8(key.toString()));
			} catch (Exception e) {
				parameters.put(key.toString(), "");
			}
		}
		parameters.remove("_");
		return parameters;
	}
/**
	 * 根据参数名从HttpRequest中获取String类型的参数值,无值则返回"" .
	 * 
	 * @param key
	 *            .
	 * @return String .
	 */
	public String getString_UrlDecode_UTF8(String key) {
		try {
			return URLDecoder.decode(this.getString(key), UTF_8);
		} catch (Exception e) {
			return "";
		}

	}


	/**
	 * 根据参数名从HttpRequest中获取String类型的参数值,无值则返回null .
	 * 
	 * @param key
	 *            .
	 * @return String or null .
	 */
	public String getString(String key) {
		String param = getHttpRequest().getParameter(key);	
		if (StringUtil.isEmpty(param)) {
			return param;
		} else {
			return param.trim();
		}
	}	
	
```

```java
	/**
	 * 取得当前request
	 * 
	 * @return
	 */
	public HttpServletRequest getHttpRequest() {
		return ServletActionContext.getRequest();
	}
```

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210324173557765.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW96aGVnYWE=,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/2021032417360824.png)
至此搞定,以后就该工具类即可

## 小总结
$\color{red}知道的越多,不知道的越多,希望对你有帮助!$
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值