tomcat为utf8编码,解析gbk编码的URL请求,中文乱码处理

阅读更多
项目中tomcat设置为utf8解码,当对外的url接口(如ReceiveAction)接收到gbk编码请求时候会自动被tomcat解码为utf8,
此时中文直接request.getParameter("content")出来就是乱码,无法直接转换回gbk,怎么转换都是乱码。

解决方法:通过requset.getQueryString() 来获取到 %D6%D0%B9%FA 再urldecode 下就得到了 正确的中文了
此类,就是基于通过自己解析http的queryString和postdata来处理乱码问题的
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;

/**
 * 项目中tomcat设置为utf8解码,当对外的url接口(如ReceiveAction)接收到gbk编码请求时候会自动被tomcat解码为utf8,
 * 此时中文直接request.getParameter("content")出来就是乱码,无法直接转换回gbk,怎么转换都是乱码。
 * 
 * 解决方法:通过requset.getQueryString() 来获取到 %D6%D0%B9%FA 再urldecode 下就得到了 正确的中文了
 * 此类,就是基于通过自己解析http的queryString和postdata来处理乱码问题的
 * 
 */
public class HttpRequestReader {

	Hashtable pairs = new Hashtable();

	/**
	 * RawParameterReader constructor comment.
	 */
	public HttpRequestReader(HttpServletRequest request, String encoding)
			throws IOException {
		super();
		parse(request.getQueryString(), encoding);
		parse(request.getReader().readLine(), encoding);
	}

	public static String decode(String s, String encoding) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			switch (c) {
			case '+':
				sb.append(' ');
				break;
			case '%':
				try {
					sb.append((char) Integer.parseInt(
							s.substring(i + 1, i + 3), 16));
				} catch (NumberFormatException e) {
					throw new IllegalArgumentException();
				}
				i += 2;
				break;
			default:
				sb.append(c);
				break;
			}
		}
		// Undo conversion to external encoding
		String result = sb.toString();
		byte[] inputBytes = result.getBytes("8859_1");
		return new String(inputBytes, encoding);
	}

	/**
	 * Insert the method's description here. Creation date: (2001-2-4 17:30:59)
	 * 
	 * @return java.lang.String
	 * @param name
	 *            java.lang.String
	 */
	public String getParameter(String name) {
		if (pairs == null || !pairs.containsKey(name))
			return null;
		return (String) (((ArrayList) pairs.get(name)).get(0));
	}

	/**
	 * Insert the method's description here. Creation date: (2001-2-4 17:28:17)
	 * 
	 * @return java.util.Enumeration
	 */
	public Enumeration getParameterNames() {
		if (pairs == null)
			return null;
		return pairs.keys();
	}

	/**
	 * Insert the method's description here. Creation date: (2001-2-4 17:33:40)
	 * 
	 * @return java.lang.String[]
	 * @param name
	 *            java.lang.String
	 */
	public String[] getParameterValues(String name) {
		if (pairs == null || !pairs.containsKey(name))
			return null;
		ArrayList al = (ArrayList) pairs.get(name);
		String[] values = new String[al.size()];
		for (int i = 0; i < values.length; i++)
			values[i] = (String) al.get(i);
		return values;
	}

	/**
	 * Insert the method's description here. Creation date: (2001-2-4 20:34:37)
	 * 
	 * @param urlenc
	 *            java.lang.String
	 */
	private void parse(String urlenc, String encoding)
			throws java.io.IOException {
		if (urlenc == null)
			return;
		StringTokenizer tok = new StringTokenizer(urlenc, "&");
		try {
			while (tok.hasMoreTokens()) {
				String aPair = tok.nextToken();
				int pos = aPair.indexOf("=");
				String name = null;
				String value = null;
				if (pos != -1) {
					name = decode(aPair.substring(0, pos), encoding);
					value = decode(aPair.substring(pos + 1), encoding);
				} else {
					name = aPair;
					value = "";
				}
				if (pairs.containsKey(name)) {
					ArrayList values = (ArrayList) pairs.get(name);
					values.add(value);
				} else {
					ArrayList values = new ArrayList();
					values.add(value);
					pairs.put(name, values);
				}
			}
		} catch (Exception e) {
			throw new java.io.IOException(e.getMessage());
		}
	}
}



然后action或jsp中使用如下代码获取到的就是正常的中文了


	HttpRequestReader paramReader = new HttpRequestReader(request, "gbk");
	String name = paramReader.getParameter("name");
	System.out.println(name);


参见: http://bbs.csdn.net/topics/390360298?page=1#post-393608658

你可能感兴趣的:(gbk,utf8,乱码)