用java,怎样把GB2312的中文字转成utf-8编码.

解决方案 »

  1.   

    String tempStr = "中文";//准备转换的字符
    String result = new String(tempStr.getBytes("GB2312"),"UTF-8");//转换后的结果
      

  2.   

    我用JSPSMARTUPLOAD控件下载的时候出现此错误:
    /jspsmartupload/jsp/12345.jsp:14: method 中的参数数量错误。
    b = Character.toString(c).getBytes("utf-8"); 我的代码是这样的兄弟看看:
    <%@ page contentType="text/html; charset=gb2312" %> 
    <%@ page import="java.io.*"%> <%! 
    public String toUtf8String(String s) {///源于网上 
    StringBuffer sb = new StringBuffer(); 
    for (int i=0;i<s.length();i++) { 
    char c = s.charAt(i); 
    if (c >= 0 && c <= 255) { 
    sb.append(c); 
    } else { 
    byte[] b; 
    try { 
    b = Character.toString(c).getBytes("utf-8"); String tempStr = "中文";//准备转换的字符
    String result = new String(tempStr.getBytes("GB2312"),"UTF-8");//转换后的结果} catch (Exception ex) { 
    System.out.println(ex); 
    b = new byte[0]; 

    for (int j = 0; j < b.length; j++) { 
    int k = b[j]; 
    if (k < 0) k += 256; 
    sb.append("%" + Integer.toHexString(k). 
    toUpperCase()); 



    return sb.toString(); 

    %> 
    <% String filename="INDEX2.jsp"; 
    String dirName=application.getRealPath("/upload"); 
    java.io.File ff=null; 
    String dd=dirName+System.getProperties().getProperty("file.separator")+filename; 
    try{ 
    ff=new java.io.File(dd); 

    catch(Exception e){ 
    e.printStackTrace(); 

    if (ff!=null&&ff.exists()&&ff.isFile()) 

    long filelength = ff.length(); 
    InputStream inStream=new FileInputStream(dd); 
    //设置输出的格式 
    response.reset(); 
    response.setContentType("application/x-msdownload"); 
    response.setContentLength((int)filelength); 
    response.addHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(filename) + "\""); 
    //循环取出流中的数据 
    byte[] b = new byte[100]; 
    int len; 
    while((len=inStream.read(b)) >0) 
    response.getOutputStream().write(b,0,len); 
    inStream.close(); } 
    %>
      

  3.   

    String result = new String(tempStr.getBytes("GB2312"),"UTF-8"