最近在做个MP3播放器,出现中文乱码问题,在网上找了很多解决办法,我整理了出现乱码的点和解决方案,拿出来和大家共享一下
1.读取中文文件乱码解决方法
packagecom.apj.conv;
importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.widget.TextView;
publicclassConverActivityextendsActivity{
privateTextViewtextview;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview=(TextView)findViewById(R.id.lrctext);
System.out.println("==============convertCodeAndGetTextbegin==============");
///获得SDCard中文件的路径
Stringpath=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
Stringtochinese=convertCodeAndGetText(path+"a.txt");
System.out.println(tochinese);
System.out.println("==============cconvertCodeAndGetTextend==============");
textview.setText(tochinese);
}
publicStringconvertCodeAndGetText(Stringstr_filepath){//转变编码
Filefile=newFile(str_filepath);
BufferedReaderreader;
Stringtext="";
try{
FileInputStreamfis=newFileInputStream(file);
BufferedInputStreamin=newBufferedInputStream(fis);
in.mark(4);
byte[]first3bytes=newbyte[3];
in.read(first3bytes);
in.reset();
if(first3bytes[0]==(byte)0xEF&&first3bytes[1]==(byte)0xBB
&&first3bytes[2]==(byte)0xBF){//utf-8
reader=newBufferedReader(newInputStreamReader(in,"utf-8"));
}elseif(first3bytes[0]==(byte)0xFF
&&first3bytes[1]==(byte)0xFE){
reader=newBufferedReader(newInputStreamReader(in,"unicode"));
}elseif(first3bytes[0]==(byte)0xFE&&first3bytes[1]==(byte)0xFF){
reader=newBufferedReader(newInputStreamReader(in,"utf-16be"));
}elseif(first3bytes[0]==(byte)0xFF&&first3bytes[1]==(byte)0xFF){
reader=newBufferedReader(newInputStreamReader(in,"utf-16le"));
}else{
reader=newBufferedReader(newInputStreamReader(in,"GBK"));
}
Stringstr=reader.readLine();
while(str!=null){
text=text+str+"\n";
str=reader.readLine();
}
reader.close();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
returntext;
}
}
packagecom.apj.conv;
importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.os.Environment;
importandroid.widget.TextView;
publicclassConverActivityextendsActivity{
privateTextViewtextview;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textview=(TextView)findViewById(R.id.lrctext);
System.out.println("===================convertCodeAndGetTextbegin===================");
///获得SDCard中文件的路径
Stringpath=Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator;
Stringtochinese=convertCodeAndGetText(path+"a.txt");
System.out.println(tochinese);
System.out.println("===================cconvertCodeAndGetTextend===================");
textview.setText(tochinese);
}
publicStringconvertCodeAndGetText(Stringstr_filepath){//ת��
Filefile=newFile(str_filepath);
BufferedReaderreader;
Stringtext="";
try{
FileInputStreamfis=newFileInputStream(file);
BufferedInputStreamin=newBufferedInputStream(fis);
in.mark(4);
byte[]first3bytes=newbyte[3];
in.read(first3bytes);
in.reset();
if(first3bytes[0]==(byte)0xEF&&first3bytes[1]==(byte)0xBB
&&first3bytes[2]==(byte)0xBF){//utf-8
reader=newBufferedReader(newInputStreamReader(in,"utf-8"));
}elseif(first3bytes[0]==(byte)0xFF
&&first3bytes[1]==(byte)0xFE){
reader=newBufferedReader(
newInputStreamReader(in,"unicode"));
}elseif(first3bytes[0]==(byte)0xFE
&&first3bytes[1]==(byte)0xFF){
reader=newBufferedReader(newInputStreamReader(in,
"utf-16be"));
}elseif(first3bytes[0]==(byte)0xFF
&&first3bytes[1]==(byte)0xFF){
reader=newBufferedReader(newInputStreamReader(in,
"utf-16le"));
}else{
reader=newBufferedReader(newInputStreamReader(in,"GBK"));
}
Stringstr=reader.readLine();
while(str!=null){
text=text+str+"\n";
str=reader.readLine();
}
reader.close();
}catch(FileNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
returntext;
}
}
2.连接网络读取文件内容中文乱码解决办法
URLmyFileUrl=null;
myFileUrl=newURL(url);
HttpURLConnectionconn;
conn=(HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStreamis=conn.getInputStream();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is,"GB2312"));
sb=newStringBuffer();
Stringdata="";
while((data=br.readLine())!=null){
sb.append(data+"\n");
}
Stringresult=sb.toString();
URLmyFileUrl=null;
myFileUrl=newURL(url);
HttpURLConnectionconn;
conn=(HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStreamis=conn.getInputStream();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is,
"GB2312"));
sb=newStringBuffer();
Stringdata="";
while((data=br.readLine())!=null){
sb.append(data+"\n");
}
Stringresult=sb.toString();
3.读取网络文件中文名下载乱码解决办法
1.先在设置服务器编码:找到Tomcat安装目录下的server.xml文件(Tomcat6.0\conf\server.xml)。设置编码为UTF-8
<Connectorport="8080"URIEncoding="UTF-8"redirectPort="8443"connectionTimeout="20000"protocol="HTTP/1.1"/>
2.android中代码为:
try{
lrcUrl="http://192.168.0.214/vote/mp3/"+URLEncoder.encode("中文.mp3","UTF-8");
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
intresult1=downFile(lrcUrl,"mp3/","中文.mp3");
**
*该函数返回整型(-1:代表下载文件出错;0:代表下载成功;1:代表文件已存在)
**/
publicintdownFile(StringurlStr,Stringpath,StringfileName){
InputStreaminputStream=null;
try{
FileUtilsfileUtils=newFileUtils();
if(fileUtils.isFileExist(fileName,path)){
return1;
}else{
inputStream=getInputStreamFromUrl(urlStr);
FileresultFile=fileUtils.write2SDFromInput(path,fileName,inputStream);
if(resultFile==null){
return-1;
}
}
}catch(Exceptione){
//TODO:handleexception
e.printStackTrace();
return-1;
}finally{
try{
inputStream.close();
}catch(Exceptione2){
e2.printStackTrace();
}
}
return0;
}
/**
*根据URL得到输入流
*
*@paramurlStr
*@return
*@throwsMalformedURLException
*@throwsIOException
*/
publicInputStreamgetInputStreamFromUrl(StringurlStr)
throwsMalformedURLException,IOException{
url=newURL(urlStr);
HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
InputStreaminputStream=urlConn.getInputStream();
returninputStream;
}