我的代码是这样的:
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")请各位帮忙看看。

解决方案 »

  1.   

    String urlstring="http://**.com";
                   
            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();
                    }
      

  2.   

    上面的貌似有点小问题  不行的话看这个
    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();
                    }
      

  3.   

    关于转码 我推荐一篇文章 或许有用
    http://www.uuzone.com/blog/mao/98921.htm
      

  4.   

    /**
      * 将字符编码转换成GBK码
      */
     public String toGBK(String str) throws UnsupportedEncodingException{
      return this.changeCharset(str, GBK);
     }
      

  5.   

    终极解决 呵呵 
    http://igogogo9.javaeye.com/blog/105326
      

  6.   

     String output=new String(result,"UTF-8");
    这句话的意思是把urlstring转换成UTF-8吗??
      

  7.   

    UTF8转换成GB2312 当我们在基于HTTP协议的JSP或Servlet的应用中获取数据或发送请求时,JVM会把输送的数据编码成UTF8格式。如果我们直接从HTTP流中提取中文数据,提取的结果为“????”(可能更多问号),为转换成我们能够理解的中文字符,我们需要把UTF8转换成GB2312,借助ISO- 8859-1标准编码能够轻易的实现,下面的代码实现了这一功能: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, "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
      

  8.   

    谢谢nanjg!我试了你的方法,但是还是不行。
      

  9.   

       //UTF转换函数
       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);
       }
      

  10.   

    我是从utf-8转换成gbk,楼上的是转成UTF-8了吧??
      

  11.   

    String gbkString = IOUtils.toString(IOUtils.toInputStream(utf8String,"GBK"))
    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);
        } 
      

  12.   

    上面那个方法需要 commons-io-1.3.2.jar 
      

  13.   

    http://commons.apache.org/downloads/download_io.cgi   此处下载 
    网上大多是 汉字乱码的解决方法 不适合楼主
      

  14.   

    String   gbkString   =   IOUtils.toString(IOUtils.toInputStream(utf8String, "GBK "))
    utf8String   中字符,皆变为   UTF-8   编码。 

    写错了 sorry
    String   gbkString   =   IOUtils.toString(IOUtils.toInputStream(utf8String, "GBK "))
    utf8String   中字符,皆变为   GBK   编码。 
      

  15.   

    to nanjg:
    我重新说一下我程序的过程。
    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")) ;
                
      

  16.   

    你试试 把iso-8859-1 直接变成 gbk 
      

  17.   

    我被吃帖子了 你把 iso-8859-1直接转gbk 
      

  18.   

    我被吃帖子了 你把 iso-8859-1直接转gbk 
      

  19.   

    我被吃帖子了 你把 iso-8859-1直接转gbk 
      

  20.   

    我刚才的网络也不好。
    mobilemessage=   new   String(servletRequest.getParameter( "mobilemessage ").getBytes( "iso-8859-1 "), "gbk");
    这个我也试过了,不行,愁死我了,这个问题折腾我好几天了~~ 
      

  21.   

    我刚才的网络也不好。
    mobilemessage=   new   String(servletRequest.getParameter( "mobilemessage ").getBytes( "iso-8859-1 "), "gbk");
    这个我也试过了,不行,愁死我了,这个问题折腾我好几天了~~ 
      

  22.   

    我还有个法 就是设置过滤器 request response 针对这2个去设置 编码
    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>
      

  23.   

    这个方法可能再我们这是行不通,因为我们的系统是要用UTF-8编码的,不能改成GBK,只是我们再发短信的时候,因为要用到第三方提供的API,他们需要用GBK编码,我想只需要在发短信那改一下编码方式,其他的地方不能修改。
      

  24.   

    filter 可以针对文件夹 过滤的!你把你们新的 针对第三方的 放置一处 去单独过滤呢?
      

  25.   

    发短信的方法单独写一个类中,去过滤这个类
    我不知道 filter是否可以针对一个类的特定方法。。
      

  26.   

    nanjg在8楼的帖子小改可能可以:
    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进行