现在的位置: 首页 > 综合 > 正文

android中文乱码解决大全

2013年08月15日 ⁄ 综合 ⁄ 共 9188字 ⁄ 字号 评论关闭

 1.读取中文文件乱码解决方法

Java代码
复制代码
 收藏代码
  1. package com.apj.conv;   
  2.   
  3. import java.io.BufferedInputStream;   
  4. import java.io.BufferedReader;   
  5. import java.io.File;   
  6. import java.io.FileInputStream;   
  7. import java.io.FileNotFoundException;   
  8. import java.io.IOException;   
  9. import java.io.InputStreamReader;   
  10.   
  11. import android.app.Activity;   
  12. import android.os.Bundle;   
  13. import android.os.Environment;   
  14. import android.widget.TextView;   
  15.   
  16. public class ConverActivity extends Activity {   
  17.        
  18.     private TextView textview ;   
  19.        
  20.        
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {   
  23.         super.onCreate(savedInstanceState);   
  24.         setContentView(R.layout.main);   
  25.            
  26.         textview = (TextView) findViewById(R.id.lrctext);   
  27.            
  28.          
  29.         System.out.println("===================convertCodeAndGetText begin=================== ");   
  30.         ///获得SDCard中文件的路径   
  31.         String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator ;   
  32.         String tochinese = convertCodeAndGetText(path+"a.txt");   
  33.         System.out.println(tochinese);   
  34.         System.out.println("===================cconvertCodeAndGetText end===================");   
  35.         textview.setText(tochinese);   
  36.            
  37.     }   
  38.       
  39.     public String convertCodeAndGetText(String str_filepath) {// ת��  
  40.   
  41.         File file = new File(str_filepath);   
  42.         BufferedReader reader;   
  43.         String text = "";   
  44.         try {   
  45.                
  46.             FileInputStream fis = new FileInputStream(file);   
  47.             BufferedInputStream in = new BufferedInputStream(fis);   
  48.             in.mark(4);   
  49.             byte[] first3bytes = new byte[3];   
  50.             in.read(first3bytes);   
  51.             in.reset();   
  52.             if (first3bytes[0] == (byte0xEF && first3bytes[1] == (byte0xBB  
  53.                     && first3bytes[2] == (byte0xBF) {// utf-8  
  54.   
  55.                 reader = new BufferedReader(new InputStreamReader(in, "utf-8"));   
  56.   
  57.             } else if (first3bytes[0] == (byte0xFF  
  58.                     && first3bytes[1] == (byte0xFE) {   
  59.   
  60.                 reader = new BufferedReader(   
  61.                         new InputStreamReader(in, "unicode"));   
  62.             } else if (first3bytes[0] == (byte0xFE  
  63.                     && first3bytes[1] == (byte0xFF) {   
  64.   
  65.                 reader = new BufferedReader(new InputStreamReader(in,   
  66.                         "utf-16be"));   
  67.             } else if (first3bytes[0] == (byte0xFF  
  68.                     && first3bytes[1] == (byte0xFF) {   
  69.   
  70.                 reader = new BufferedReader(new InputStreamReader(in,   
  71.                         "utf-16le"));   
  72.             } else {   
  73.   
  74.                 reader = new BufferedReader(new InputStreamReader(in, "GBK"));   
  75.             }   
  76.             String str = reader.readLine();   
  77.   
  78.             while (str != null) {   
  79.                 text = text + str + "\n";   
  80.                 str = reader.readLine();   
  81.   
  82.             }   
  83.             reader.close();   
  84.   
  85.         } catch (FileNotFoundException e) {   
  86.             // TODO Auto-generated catch block  
  87.             e.printStackTrace();   
  88.         } catch (IOException e) {   
  89.             e.printStackTrace();   
  90.         }   
  91.         return text;   
  92.     }   
  93.   
  94. }  
  1. package com.apj.conv;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedReader;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileNotFoundException;  
  8. import java.io.IOException;  
  9. import java.io.InputStreamReader;  
  10.   
  11. import android.app.Activity;  
  12. import android.os.Bundle;  
  13. import android.os.Environment;  
  14. import android.widget.TextView;  
  15.   
  16. public class ConverActivity extends Activity {  
  17.       
  18.     private TextView textview ;  
  19.       
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.           
  26.         textview = (TextView) findViewById(R.id.lrctext);  
  27.           
  28.         
  29.         System.out.println("===================convertCodeAndGetText begin=================== ");  
  30.         ///获得SDCard中文件的路径  
  31.         String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator ;  
  32.         String tochinese = convertCodeAndGetText(path+"a.txt");  
  33.         System.out.println(tochinese);  
  34.         System.out.println("===================cconvertCodeAndGetText end===================");  
  35.         textview.setText(tochinese);  
  36.           
  37.     }  
  38.      
  39.     public String convertCodeAndGetText(String str_filepath) {// ת��  
  40.   
  41.         File file = new File(str_filepath);  
  42.         BufferedReader reader;  
  43.         String text = "";  
  44.         try {  
  45.               
  46.             FileInputStream fis = new FileInputStream(file);  
  47.             BufferedInputStream in = new BufferedInputStream(fis);  
  48.             in.mark(4);  
  49.             byte[] first3bytes = new byte[3];  
  50.             in.read(first3bytes);  
  51.             in.reset();  
  52.             if (first3bytes[0] == (byte0xEF && first3bytes[1] == (byte0xBB  
  53.                     && first3bytes[2] == (byte0xBF) {// utf-8  
  54.   
  55.                 reader = new BufferedReader(new InputStreamReader(in, "utf-8"));  
  56.   
  57.             } else if (first3bytes[0] == (byte0xFF  
  58.                     && first3bytes[1] == (byte0xFE) {  
  59.   
  60.                 reader = new BufferedReader(  
  61.                         new InputStreamReader(in, "unicode"));  
  62.             } else if (first3bytes[0] == (byte0xFE  
  63.                     && first3bytes[1] == (byte0xFF) {  
  64.   
  65.                 reader = new BufferedReader(new InputStreamReader(in,  
  66.                         "utf-16be"));  
  67.             } else if (first3bytes[0] == (byte0xFF  
  68.                     && first3bytes[1] == (byte0xFF) {  
  69.   
  70.                 reader = new BufferedReader(new InputStreamReader(in,  
  71.                         "utf-16le"));  
  72.             } else {  
  73.   
  74.                 reader = new BufferedReader(new InputStreamReader(in, "GBK"));  
  75.             }  
  76.             String str = reader.readLine();  
  77.   
  78.             while (str != null) {  
  79.                 text = text + str + "\n";  
  80.                 str = reader.readLine();  
  81.   
  82.             }  
  83.             reader.close();  
  84.   
  85.         } catch (FileNotFoundException e) {  
  86.             // TODO Auto-generated catch block  
  87.             e.printStackTrace();  
  88.         } catch (IOException e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.         return text;  
  92.     }  
  93.   
  94. }  

2. 连接网络读取文件内容中文乱码解决办法

Java代码
复制代码
 收藏代码
  1. URL myFileUrl = null;      
  2.    myFileUrl = new URL(url);   
  3.    HttpURLConnection conn;   
  4.    conn = (HttpURLConnection) myFileUrl.openConnection();   
  5.    conn.setDoInput(true);   
  6.    conn.connect();   
  7.    InputStream is = conn.getInputStream();   
  8.    BufferedReader br = new BufferedReader(new InputStreamReader(is,   
  9.      "GB2312"));   
  10.    sb = new StringBuffer();   
  11.    String data = "";   
  12.    while ((data = br.readLine()) != null) {      
  13.         sb.append(data+"\n");      
  14.    }      
  15.    String result = sb.toString();  
  1. URL myFileUrl = null;     
  2.    myFileUrl = new URL(url);  
  3.    HttpURLConnection conn;  
  4.    conn = (HttpURLConnection) myFileUrl.openConnection();  
  5.    conn.setDoInput(true);  
  6.    conn.connect();  
  7.    InputStream is = conn.getInputStream();  
  8.    BufferedReader br = new BufferedReader(new InputStreamReader(is,  
  9.      "GB2312"));  
  10.    sb = new StringBuffer();  
  11.    String data = "";  
  12.    while ((data = br.readLine()) != null) {     
  13.         sb.append(data+"\n");     
  14.    }     
  15.    String result = sb.toString();  

3.读取网络文件中文名下载乱码解决办法
1.先在设置服务器编码:找到Tomcat安装目录下的server.xml文件(Tomcat 6.0\conf\server.xml)。设置编码为UTF-8

<Connectorport="8080" URIEncoding="UTF-8" redirectPort="8443" connectionTimeout="20000" protocol="HTTP/1.1"/>

2. android 中代码为:

Java代码
复制代码
 收藏代码
  1.     try {   
  2.          lrcUrl = "http://192.168.0.214/vote/mp3/" + URLEncoder.encode("中文.mp3","UTF-8");   
  3.         } catch (UnsupportedEncodingException e) {   
  4.             // TODO Auto-generated catch block  
  5.             e.printStackTrace();   
  6.         }   
  7.   
  8.         int result1 = downFile(lrcUrl, "mp3/""中文.mp3");   
  9. **   
  10.  * 该函数返回整型( -1:代表下载文件出错 ;0:代表下载成功;1:代表文件已存在)   
  11.  **/   
  12. public int downFile(String urlStr, String path, String fileName) {   
  13.     InputStream inputStream = null;   
  14.     try {   
  15.         FileUtils fileUtils = new FileUtils();   
  16.         if (fileUtils.isFileExist( fileName,path )) {   
  17.             return 1;   
  18.         } else {   
  19.             inputStream = getInputStreamFromUrl(urlStr);   
  20.             File resultFile = fileUtils.write2SDFromInput(path, fileName,   
  21.                     inputStream);   
  22.             if (resultFile == null) {   
  23.                 return -1;   
  24.             }   
  25.   
  26.         }   
  27.     } catch (Exception e) {   
  28.         // TODO: handle exception   
  29.         e.printStackTrace();   
  30.         return -1;   
  31.     } finally {   
  32.         try {   
  33.             inputStream.close();   
  34.         } catch (Exception e2) {   
  35.             e2.printStackTrace();   
  36.         }   
  37.     }   
  38.     return 0;   
  39. }   
  40.   
  41. /**  
  42.  * 根据URL得到输入流  
  43.  *   
  44.  * @param urlStr  
  45.  * @return  
  46.  * @throws MalformedURLException  
  47.  * @throws IOException  
  48.  */  
  49.   
  50. public InputStream getInputStreamFromUrl(String urlStr)   
  51.         throws MalformedURLException, IOException {   
  52.     url = new URL(urlStr);   
  53.     HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();   
  54.     InputStream inputStream = urlConn.getInputStream();   
  55.     return inputStream;   

抱歉!评论已关闭.