日期:2014-05-19  浏览次数:20990 次

关于流InputStream 转码问题
Java code

String path = CmsContext.projectPath;
        String realPath = path + "/index.html";
        File file = new File(realPath);
        /* DEL */
        //System.out.println("#####:"+ realPath);
        if(file.exists() || file.isDirectory()){
            System.out.println("首页面可编辑... ");
            
            /** 将html内容读取出来 */
            try {  
                BufferedInputStream in = new BufferedInputStream(  
                        new FileInputStream(file));  
                ByteArrayOutputStream out = new ByteArrayOutputStream(1024);  
                byte[] temp = new byte[1024];  
                int size = 0;  
                while ((size = in.read(temp)) != -1) {  
                    out.write(temp, 0, size);  
                }  
                in.close();  
                /* DEL */
                //String clob =  new String(out.toString().getBytes("iso8859-1"),"GB2312"); 
                String encod_char = out.toString();  



如何将读出来的字符串的编码 正常显示,现在是乱码, index.html 格式是 UTF-8 的 但是直接这么读出来是乱码的.
求帮助 ...

------解决方案--------------------
你注释这段代码没用吗? //String clob = new String(out.toString().getBytes("iso8859-1"),"GB2312");
------解决方案--------------------
我用这个没问题啊。
Test.java代码:
Java code

/**
 * 
 */
package encodingTest;

/**
 * @author wkupaochuan
 * @time Apr 11, 2012
 * @version V 1.0
 */

import java.io.*;
public class Test {
    
    public static void main (String agrs[]) throws IOException
    {
        File f = new File("C://1.html");
         BufferedInputStream in = new BufferedInputStream(  
                 new FileInputStream(f));  
         ByteArrayOutputStream out = new ByteArrayOutputStream(1024);  
         byte[] temp = new byte[1024];  
         int size = 0;  
         while ((size = in.read(temp)) != -1) {  
             out.write(temp, 0, size);  
         }  
         in.close();  
         String encod_char = out.toString();  
         System.out.println(encod_char);
    }

}

------解决方案--------------------
html源码:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<head>实验</head>
<body>
主题
</body>
</html>



探讨
我用这个没问题啊。
Test.java代码:

Java code


/**
*
*/
package encodingTest;

/**
* @author wkupaochuan
* @time Apr 11, 2012
* @version V 1.0
*/

import java.io.*;
public class Test {
……