首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

施用common upload中文字符乱码

2012-12-19 
使用common upload中文字符乱码我使用 FileItem item 里面的getString()方法想要得到表单字段的值结果老是

使用common upload中文字符乱码

我使用 FileItem item 里面的getString()方法想要得到表单字段的值结果老是乱码。

搞了一个下午没搞懂 ,我尝试了 把得到的字符串转GBK UTF-8 GB2312都无结果。

?

最终我下载了FileItem的类 查看了getString()方法 如下:

    public String getString() {        byte[] rawdata = get();        String charset = getCharSet();        if (charset == null) {            charset = DEFAULT_CHARSET;        }        try {            return new String(rawdata, charset);        } catch (UnsupportedEncodingException e) {            return new String(rawdata);        }    }

?偶原来里面有个DEFAULT_CHARSET

public static final String DEFAULT_CHARSET = "ISO-8859-1";

?

气死我了 我就说怎么转换不起嘛 呜呜。

这下这么来转一下就可以了:

value = item.getString();String words = new String(value.getBytes("iso-8859-1"),"UTF-8");

?

?

再仔细看看源码 原来里面还提供了一个getString的重载方法

public String getString(final String charset)        throws UnsupportedEncodingException {        return new String(get(), charset);    }

?

哎悲剧呀。。。

?

?

热点排行