问题描述
最近做的项目中需要一个提取ftp信息一个小程序,程序建立在apachecomments-net的基础之上。ftp服务器程序师FileZilla。控制连接要用iso-8859-1的编码才能让服务器识别命令,而在comments-net的数据处理里面,数据连接的编码是调用控制连接的。这样过来,中文就成为乱码了,恳请高人指教!FileZilla已经在附件中,comments-net,可以再【http://www.dirsphere.com/apachemirror/commons/net/binaries/commons-net-2.0.zip】下载。下面是测试程序的源码:importjava.io.IOException;importjava.util.Date;importorg.apache.commons.net.ftp.FTP;importorg.apache.commons.net.ftp.FTPClient;importorg.apache.commons.net.ftp.FTPFile;importorg.apache.commons.net.ftp.FTPReply;publicclassTest{privatestaticFTPClientftp;privatestaticStringhost="222.22.7.215";privatestaticStringuser="anonymous";privatestaticStringpass="";privatestaticintrecNum=0;publicstaticvoidmain(String[]args){ftp=newFTPClient();booleanerror=false;try{intreply;ftp.connect(host);System.out.println("Connectedto"+host);System.out.print(ftp.getReplyString());//Afterconnectionattempt,youshouldcheckthereplycodeto//verify//success.reply=ftp.getReplyCode();//ftp.setControlEncoding("UTF-8");ftp.setFileTransferMode(FTP.ASCII_FILE_TYPE);if(!FTPReply.isPositiveCompletion(reply)){ftp.disconnect();System.err.println("FTPserverrefusedconnection.");System.exit(1);}ftp.login(user,pass);//transferfiles/**FTPFile[]files=ftp.listFiles();for(inti=0;i<*files.length;i++){System.out.println(files[i].getName());**}*/DatestartTime=newDate();traverse(ftp,ftp.printWorkingDirectory());DateendTime=newDate();System.out.println("遍历耗时"+String.valueOf(endTime.getTime()-startTime.getTime()/60000.0)+"分钟");System.out.println("共有文件数:"+recNum+"个");/**String[]names=ftp.listNames();for(Stringstring:names){*System.out.println(string);}*System.out.println(ftp.printWorkingDirectory());*System.out.println(ftp.getBufferSize());*/ftp.logout();}catch(IOExceptione){error=true;e.printStackTrace();}finally{if(ftp.isConnected()){try{ftp.disconnect();}catch(IOExceptionioe){//donothing}}System.exit(error?1:0);}}publicstaticvoidtraverse(FTPClientclient,Stringpath){Stringprefix="";try{FTPFile[]files=client.listFiles(path);for(inti=0;i<files.length;i++){if(files[i].getName().equals(".")||files[i].getName().equals("..")){continue;}elseif(files[i].isDirectory()){StringtempDir=client.printWorkingDirectory()+"/"+files[i].getName();//tempDir=newString(tempDir.getBytes(),"UTF-8");//System.out.println("Directory:"+tempDir);//System.out.println(client.changeWorkingDirectory(tempDir))//;client.changeWorkingDirectory(tempDir);traverse(client,tempDir);prefix+=client.printWorkingDirectory();client.changeToParentDirectory();}else{Stringtemp=client.printWorkingDirectory();if(temp.equals("/")){recNum++;System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");System.out.println("URL:"+temp+newString(files[i].getName().getBytes()));System.out.println(files[i].getSize()/1000000.0+"MB");System.out.println(files[i].getTimestamp().getTime());System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");System.out.println("rnrn");}else{recNum++;System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");System.out.println("URL:"+temp+"/"+newString(files[i].getName().getBytes()));System.out.println(files[i].getSize()/1000000.0+"MB");System.out.println(files[i].getTimestamp().getTime());System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");System.out.println("rnrn");}}}}catch(IOExceptione){e.printStackTrace();}}}
解决方案
解决方案二:
up,mark,学习,先标记上。
解决方案三:
system.out.println("URL:"+temp+newString(files[i].getName().getBytes()));在取得文件名称后,要重新解码,system.out.println("URL:"+temp+newString(files[i].getName().getBytes("GBK"),"iso-8859-1")));但是前提条件是,在遍历文件名的时候,使用iso-8859-1没有丢失中文信息,如果已经丢失,是没有办法取回来的。楼主试试。
解决方案四:
谢谢,我已经找到解决的办法了,谢谢你!
解决方案五:
解决方法分享一下啊!
解决方案六:
解决方法就在主函数publicstaticvoidmain(String[]args){ftp=newFTPClient();ftp.setControlEncoding("GBK");//添上这句设置编码就没中文乱码了