现在的位置: 首页 > 综合 > 正文

解决字符串GET方式提交乱码问题

2017年12月26日 ⁄ 综合 ⁄ 共 1350字 ⁄ 字号 评论关闭

解决方案

1:

用post提交

2:

package com.tempus.common.utils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * 类 名 称: StringUtils
 * 类 描 述: 
 * 创 建 人: 肖鹏
 * 创建时间: 2012-11-19 下午03:19:49
 *
 * 修 改 人: 肖鹏
 * 操作时间: 2012-11-19 下午03:19:49
 * 操作原因: 
 *
 */
public class StringUtils
{
	static Logger logger = LoggerFactory.getLogger(StringUtils.class);
	
	/**
	 * 
	 * 方法描述:  (中文以GET方式请求参数)
	 * 作    者: 肖鹏
	 * 日    期: 2012-11-19-下午03:08:27
	 * @param str
	 * @param request
	 * @return 
	 * 返回类型: String
	 */
	public static String encodeString(String str, HttpServletRequest request){
		String result = "";
		String userAgent = request.getHeader("user-agent");
		try
		{
			if (userAgent.toLowerCase().indexOf("firefox") != -1){
					result = URLDecoder.decode(new String(str.getBytes("ISO-8859-1"),"utf-8"), "utf-8");
			}else{
				result = new String(str.getBytes("iso-8859-1"),"utf-8");
			}
		} catch (UnsupportedEncodingException ex)
		{
			ex.printStackTrace();
			logger.error(ex.getMessage(), ex);
		}
		return result;
	}
	
	public static <T> T convertType(String str, Class<T> cls, T t){
		try{
			Constructor<T> constructor =  cls.getConstructor(String.class);
			return (T)constructor.newInstance(str);
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(ex.getMessage(), ex);
		}
		return t;
	} 
	public static void main(String[] args)
	{
		String str = "1000";
		Float dou = StringUtils.convertType(str, Float.class, new Float(10));
		System.out.println(dou);
	}
}

抱歉!评论已关闭.