1. 程式人生 > >轉碼utf-8還是gbk

轉碼utf-8還是gbk

 說今天寫這一篇,因為遇到個問題,在centos上匯出檔案一直亂碼。

原來就沒出現這問題,適了好多種方法。

最後把匯出的編碼設成gbk竟然好了。現在還不知其原因,如有高手還請賜教。

java程式碼編碼utf-8,

jsp編碼utf-8,

centos中i18n:utf-8,

tomcat:uriencoding utf-8.

 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);
    }

對於UTF-8編碼格式的文字檔案,其前3個位元組的值就是-17、-69、-65,所以,判定是否是UTF-8編碼格式的程式碼片段如下

   測試檔案編碼是否為UTF-8

     File file = new File(path);

     InputStream ios = new java.io.FileInputStream(file);

    te[] b = new byte;

     ios.read(b);

     ios.close();

     if (b[0] == -17 && b[1] == -69 && b == -65)

       System.out.println(file.getName() + “:編碼為UTF-8″);

     else

       System.out.println(file.getName() + “:可能是GBK,也可能是其他編碼。”);

   public static void downloadFile(HttpServletRequest request,
            HttpServletResponse response, String fileName)
    {
        if (StringUtils.isEmpty(fileName))
        {
            return;
        }

        //匯出檔案路徑
        String filePath = PlatformConfig.getUseDataPath() + Constants.EXPORT_PATH + fileName;
       
        // 處理中文名稱,需要編碼
        response.setContentType("application/octet-stream; CHARSET=GBK");
       
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try
        {
            response.setHeader("Content-Disposition", "attachment; filename="
              + EncodingUtil.getString(fileName.getBytes("gbk"),
        "ISO8859-1"));
            inputStream = new FileInputStream(filePath);
            outputStream = response.getOutputStream();
           
            transferFile(inputStream, outputStream);
        }
        catch (FileNotFoundException e)
        {
            logger.error(e.toString());
        }
        catch (IOException e)
        {
            logger.error(e.toString());
        }
        finally
        {
            try
            {
                if (inputStream != null)
                {
                    inputStream.close();
                }
            }
            catch (Exception e)
            {
                logger.error(e.toString());
            }
            try
            {
                if (outputStream != null)
                {
                    outputStream.close();
                }
            }
            catch (Exception e)
            {
                logger.error(e.toString());
            }
           
            //刪除臨時匯出檔案
            FileTools.delFile(filePath);
        }
    }