网站搜索框搜索乱码,网址链接正常显示转码后的文字,可搜索内容是乱码。
网站用tomcat跑,看网上解决方案,在server.xml中设置
URIEncoding="UTF-8"
没起作用
由于是用nginx做反向代理,一直认为是nginx转发那一道出了问题,就一直研究nginx编码,拖了两天.......
由于设计师催促,只好找了其他服务器做测试
一模一样的环境,这次不用nginx 只用tomcat,结果还是乱码,有点思绪了,不是nginx的问题,于是把环境部署到Windows上
问题解决
思考:同样的tomcat,同样的配置,不一样的只有系统了,Linux系统编码为utf8
查看乱码,使用工具将乱码转换为文字,转换不过去,将文字使用工具编码,然后和乱码做对比,发现转换的%大小不一样,也就是说,他将%转换为了中文的%,所以再次转码就转换不过来,现在问题就很清楚了
Linux系统编码问题,可上面放了不止一个网站,又是生产机,乱改编码肯定不行,于是,只能改代码。。。
在requestutils里面添加下面代码,将中文%转为英文%
System.out.println("替换前:" + s);
s = s.replace("%", "%");
System.out.println("替换后:" + s);
s = URLDecoder.decode(s, "UTF-8");
附RequestUtils所有代码
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.StringTokenizer;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.UrlPathHelper;
public class RequestUtils
{
private static final Logger log = LoggerFactory.getLogger(RequestUtils.class);
public static String getQueryParam(HttpServletRequest request, String name)
{
if (StringUtils.isBlank(name)) {
return null;
}
if (request.getMethod().equalsIgnoreCase("POST")) {
return request.getParameter(name);
}
String s = request.getQueryString();
if (StringUtils.isBlank(s))
return null;
try
{
System.out.println("替换前:" + s);
s = s.replace("%", "%");
System.out.println("替换后:" + s);
s = URLDecoder.decode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("encoding UTF-8 not support?", e);
}
String[] values = (String[])parseQueryString(s).get(name);
if ((values != null) && (values.length > 0)) {
return values[(values.length - 1)];
}
return null;
}
public static Map<String, Object> getQueryParams(HttpServletRequest request)
{
Map map;
Map map;
if (request.getMethod().equalsIgnoreCase("POST")) {
map = request.getParameterMap();
} else {
String s = request.getQueryString();
if (StringUtils.isBlank(s))
return new HashMap();
try
{
s = URLDecoder.decode(s, "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("encoding UTF-8 not support?", e);
}
map = parseQueryString(s);
}
Map params = new HashMap(map.size());
for (Map.Entry entry : map.entrySet()) {
int len = ((String[])entry.getValue()).length;
if (len == 1)
params.put((String)entry.getKey(), ((String[])entry.getValue())[0]);
else if (len > 1) {
params.put((String)entry.getKey(), entry.getValue());
}
}
return params;
}
public static Map<String, String[]> parseQueryString(String s)
{
String[] valArray = null;
if (s == null) {
throw new IllegalArgumentException();
}
Map ht = new HashMap();
StringTokenizer st = new StringTokenizer(s, "&");
while (st.hasMoreTokens()) {
String pair = st.nextToken();
int pos = pair.indexOf('=');
if (pos == -1) {
continue;
}
String key = pair.substring(0, pos);
String val = pair.substring(pos + 1, pair.length());
if (ht.containsKey(key)) {
String[] oldVals = (String[])ht.get(key);
valArray = new String[oldVals.length + 1];
for (int i = 0; i < oldVals.length; i++) {
valArray[i] = oldVals[i];
}
valArray[oldVals.length] = val;
} else {
valArray = new String[1];
valArray[0] = val;
}
ht.put(key, valArray);
}
return ht;
}
public static Map<String, String> getRequestMap(HttpServletRequest request, String prefix)
{
return getRequestMap(request, prefix, false);
}
public static Map<String, String> getRequestMapWithPrefix(HttpServletRequest request, String prefix)
{
return getRequestMap(request, prefix, true);
}
private static Map<String, String> getRequestMap(HttpServletRequest request, String prefix, boolean nameWithPrefix)
{
Map map = new HashMap();
Enumeration names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = (String)names.nextElement();
if (name.startsWith(prefix)) {
String key = nameWithPrefix ? name : name.substring(prefix.length());
String value = StringUtils.join(request.getParameterValues(name), ',');
map.put(key, value);
}
}
return map;
}
public static String getIpAddr(HttpServletRequest request)
{
String ip = request.getHeader("X-Real-IP");
if ((!StringUtils.isBlank(ip)) && (!"unknown".equalsIgnoreCase(ip))) {
if ((ip.contains("../")) || (ip.contains("..\\"))) {
return "";
}
return ip;
}
ip = request.getHeader("X-Forwarded-For");
if ((!StringUtils.isBlank(ip)) && (!"unknown".equalsIgnoreCase(ip)))
{
int index = ip.indexOf(',');
if (index != -1) {
ip = ip.substring(0, index);
}
if ((ip.contains("../")) || (ip.contains("..\\"))) {
return "";
}
return ip;
}
ip = request.getRemoteAddr();
if ((ip.contains("../")) || (ip.contains("..\\"))) {
return "";
}
if (ip.equals("0:0:0:0:0:0:0:1")) {
ip = "127.0.0.1";
}
return ip;
}
public static String getLocation(HttpServletRequest request)
{
UrlPathHelper helper = new UrlPathHelper();
StringBuffer buff = request.getRequestURL();
String uri = request.getRequestURI();
String origUri = helper.getOriginatingRequestUri(request);
buff.replace(buff.length() - uri.length(), buff.length(), origUri);
String queryString = helper.getOriginatingQueryString(request);
if (queryString != null) {
buff.append("?").append(queryString);
}
return buff.toString();
}
public static String getRequestedSessionId(HttpServletRequest request)
{
String sid = request.getRequestedSessionId();
String ctx = request.getContextPath();
if ((request.isRequestedSessionIdFromURL()) || (StringUtils.isBlank(ctx))) {
return sid;
}
Cookie cookie = CookieUtils.getCookie(request, "JSESSIONID");
if (cookie != null) {
return cookie.getValue();
}
return request.getSession().getId();
}
public static void main(String[] args)
{
}
}