日期:2014-05-20  浏览次数:20863 次

sun.net.ftp.FtpClient 上传出现含中英文混合字符的文件名乱码问题
各位大虾:
  小弟请教个问题:
  sun.net.ftp.FtpClient 上传出现含中英文混合字符的文件名乱码问题。
  我的操作系统的默认encoding是GBK,myeclipse是不是要将字符串先转换为UTF-16,然后再转UTF-8就出现
  含中英文混合字符的文件名乱码问题?
  奇怪的是我ftp服务器支持中文的,
  然后有些含有混合中英文字符的文件能正确上传,传后的文件名也是对的,例如:“复件 CM7.log”
  但有些就不行 比如:“A Lin 一瞬之光.mp3” 顺便说一下,我是用将文件名转换为 utf-8 编码去上传的

默认的编码传输上ftp服务器,只要上传的文件有中文就会出现乱码,
  比如,我上传“A Lin 一瞬之光.mp3”文件转码成“UTF-8后就成了 "A Lin 涓?灛涔嬪厜.mp3"
  服务器上的显示就成了“A Lin ?????.mp3”

Java code

public boolean uploadFile(String RWFileDir,String filepathname,String filename)
{  
    boolean result=true;  
    String message = ""; 
    String name ="";
    if (aftp.serverIsOpen())  
    {  
     System.out.println("正在上传文件"+filepathname+",请等待....");  
     try
     {      
       name = new String(filename.getBytes("UTF-8"));
     RandomAccessFile sendFile = new RandomAccessFile(filepathname,"r");  
     sendFile.seek(0);  
     outs = aftp.put(name); 
     outputs = new DataOutputStream(outs); 
     while (sendFile.getFilePointer() < sendFile.length() )  
    {  
     ch = sendFile.read();  
     outputs.write(ch);  
    }  
                   
    outputs.flush();
                  
    message = "上传"+filepathname+"文件成功!"; 
    System.out.println(message);       
                
    outputs.close();
    outs.close();  
    sendFile.close();  
    log(RWFileDir,message); //记录日志
    } 
    catch(IOException e)
    {  
     message = "上传"+filepathname+"文件失败!"+e; 
     System.out.println(message);  
    log(RWFileDir,message); 
    result = false ;          
     }  
      }  
  else
     {  
       result = false;  
      }
 return result;  
} 





------解决方案--------------------
不用utf8就用gbk试试