文字列GET方式コミット文字化問題の解決
1795 ワード
ソリューション
1:
postでコミット
2:
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);
}
}