我的代码是这样的:
mobilemessage = new String((servletRequest.getParameter(
"mobilemessage")).getBytes("iso-8859-1"), "UTF-8");
我首先把iso8859-1转成utf-8,我现在需要再把这个utf-8转成GBK,请问该怎么转啊??
我用了几个方法,都行不通。以下是我用的几个方法: 1。mobilemessage =(String)servletRequest.getParameter("mobilemessage");
mobilemessage= new String(URLDecoder.decode(mobilemessage, "utf-8").getBytes("utf-8"),"GBK");
2。mobilemessage=URLDecoder.decode(URLEncoder.encode(mobilemessage,"GBK"),"GBK");
3。mobilemessage = new String(mobilemessage.getBytes("iso-8859-1"),"gbk")请各位帮忙看看。
mobilemessage = new String((servletRequest.getParameter(
"mobilemessage")).getBytes("iso-8859-1"), "UTF-8");
我首先把iso8859-1转成utf-8,我现在需要再把这个utf-8转成GBK,请问该怎么转啊??
我用了几个方法,都行不通。以下是我用的几个方法: 1。mobilemessage =(String)servletRequest.getParameter("mobilemessage");
mobilemessage= new String(URLDecoder.decode(mobilemessage, "utf-8").getBytes("utf-8"),"GBK");
2。mobilemessage=URLDecoder.decode(URLEncoder.encode(mobilemessage,"GBK"),"GBK");
3。mobilemessage = new String(mobilemessage.getBytes("iso-8859-1"),"gbk")请各位帮忙看看。
try{ URL url = new URL(urlstring);
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
String buff=""; while ((line = reader.readLine()) != null) //读取内容
buff=buff+line+"\n"; reader.close();
String GBK=new String(buff.getBytes("GBK"),"UTF-8"); //转换
System.out.println(GBK);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
String urlstring="http://**.com";
try{
URL url = new URL(urlstring);
URLConnection conn = url.openConnection();
InputStream in =conn.getInputStream();
byte[] tempbuff=new byte[100]; //临时数组
byte[] buff =new byte[10240]; //定义一下足够大的数组
int count=0; //读取字节个数
int rbyte=0; //每次读取的个数
while((rbyte=in.read(tempbuff))!=-1){
for(int i=0;i<rbyte;i++)
buff[count+i]=tempbuff[i];
count+=rbyte;
}
byte[] result=new byte[count];
for(int i=0;i<count;i++)
result[i]=buff[i];
String output=new String(result,"UTF-8"); System.out.println(output);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
http://www.uuzone.com/blog/mao/98921.htm
* 将字符编码转换成GBK码
*/
public String toGBK(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, GBK);
}
http://igogogo9.javaeye.com/blog/105326
这句话的意思是把urlstring转换成UTF-8吗??
String utf8_value;
utf8_value = request.getParameter("NAME");//从HTTP流中取"NAME"的UTF8数据
b = utf8_value.getBytes("8859_1"); //中间用ISO-8859-1过渡
String name = new String(b, "GB2312"); //转换成GB2312字符这是我做的一个项目程序的一段:
byte[] b;
String gbk_value;
gbk_value=request.getParameter("address");//从HTTP流中取"name"的GBK数据(由于web.xml中过滤器设置默认编码为GBK,所以外网从UTF-8变为GBK)
。。
http://igogogo9.javaeye.com/blog/105326
public static String toUtf8String(String src){
byte[] b = src.getBytes();
char[] c = new char[b.length];
for (int i=0;i<b.length;i++){
c[i] = (char)(b[i]&0x00FF);
}
return new String(c);
}
utf8String 中字符,皆变为 UTF-8 编码。
借助的借助 Apache Commons-IO 项目中提供的实用工具来编写代码。
com.apache.commons.io.IOUtils 中相关代码如下:
/**
* Convert the specified string to an input stream, encoded as bytes
* using the specified character encoding.
* * Character encoding names can be found at
* IANA.
*
* @param input the string to convert
* @param encoding the encoding to use, null means platform default
* @throws IOException if the encoding is invalid
* @return an input stream
* @since Commons IO 1.1
*/
public static InputStream toInputStream(String input, String encoding) throws IOException {
byte[] bytes = encoding != null ? input.getBytes(encoding) : input.getBytes();
return new ByteArrayInputStream(bytes);
}
网上大多是 汉字乱码的解决方法 不适合楼主
utf8String 中字符,皆变为 UTF-8 编码。
写错了 sorry
String gbkString = IOUtils.toString(IOUtils.toInputStream(utf8String, "GBK "))
utf8String 中字符,皆变为 GBK 编码。
我重新说一下我程序的过程。
1。获得mobilemessage的utf-8的编码。mobilemessage= new String(servletRequest.getParameter("mobilemessage").getBytes("iso-8859-1"),"utf-8");
2。因为我们需要调用第三方提供的api函数,他们的函数要求用gbk编码,所以我这个utf-8的编码必须要转成gbk编码,才能成功调用他们的函数。但是,我用上面这个方法还是不能成功调用他们的函数。
mobilemessage =org.apache.commons.io.IOUtils.toString(org.apache.commons.io.IOUtils.toInputStream(mobilemessage,"GBK")) ;
mobilemessage= new String(servletRequest.getParameter( "mobilemessage ").getBytes( "iso-8859-1 "), "gbk");
这个我也试过了,不行,愁死我了,这个问题折腾我好几天了~~
mobilemessage= new String(servletRequest.getParameter( "mobilemessage ").getBytes( "iso-8859-1 "), "gbk");
这个我也试过了,不行,愁死我了,这个问题折腾我好几天了~~
import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class SetCharacterEncodingFilter implements Filter {/*
* (非 Javadoc)
*
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {this.encoding = null;
this.filterConfig = null;}public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 选择使用的字符编码
if (ignore ¦ ¦ (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);}
// 将控制权交给下一个Filter
chain.doFilter(request, response);
}}// 将这个filter放置在服务中
/*
* (非 Javadoc)
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter( "encoding ");
String value = filterConfig.getInitParameter( "ignore ");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase( "true "))
this.ignore = true;
else if (value.equalsIgnoreCase( "yes "))
this.ignore = true;
else
this.ignore = false;}protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}protected String encoding = null;protected FilterConfig filterConfig = null;protected boolean ignore = true;}
在web.xml文件中加入以下代码
<filter>
<filter-name> SetCharacterEncodingFilter </filter-name>
<filter-class> SetCharacterEncodingFilter </filter-class>
<init-param>
<param_name> encoding </param_name>
<param-value> GBK </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> SetCharacterEncodingFilter </filter-name>
<url_pattern> /* </url_pattern>
</filter-mapping>
我不知道 filter是否可以针对一个类的特定方法。。
byte [] b;
String utf8_value;
utf8_value = request.getParameter("NAME");//从HTTP流中取"NAME"的UTF8数据
b = utf8_value.getBytes("8859_1"); //中间用ISO-8859-1过渡
String name = new String(b, "utf-8"); //其实最终显示的就是无乱码的汉字了,前提是前面传递的参数的时候是utf-8进行