如下代码:用斜杠标出的是问题之处。package ftp;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;public class FtpManager {
private String hostName="ftp.*****.edu.cn";
private int port=21;
private String userName = "download";
private String password = "download"; 

FTPClient ftpClient = new FTPClient();
         String remote="/music/1.中文流行/female_女歌手/阿桑/2005.02 寂寞在唱歌/08.一直很安静.mp3";
         String local ="08.一直很安静.mp3";//remote.split("/")[remote.split("/").length-1];
          
          //下载文件到本地
public void run() {
OutputStream outStream=null;
try {
ftpClient.connect(hostName, port);
ftpClient.login(userName, password);
ftpClient.setControlEncoding("GBK");

FTPFile[] files = ftpClient.listFiles();
for (int i=0;i<files.length;i++){
System.out.println(files[i]);
}
  outStream = new FileOutputStream(local);
                       ///////////////////////////////////////////////////////////////////////
System.out.println(ftpClient.retrieveFile(remote, outStream));
                        
                        从打印结果看 ftpClient.retrieveFile(remote, outStream)的返回值为false;
                           不知什么原因?而我实际得到的文件大小恒为0kb。
                       //////////////////////////////////////////////////////////////////////

catch (FileNotFoundException e) {
System.out.println("1");

catch (IOException e) {
System.out.println("2");
}
finally{
IOUtils.closeQuietly(outStream);
try{
ftpClient.disconnect();
}
catch (IOException e) {
// TODO: handle exception
System.out.println("3");
}
}
}
public static void main(String[] args) throws IOException {
FtpManager ftp = new FtpManager();
ftp.run();
}

}

解决方案 »

  1.   

    谢谢果然是路径问题
    我把路径换成 String remote="/software/Download/Programming/SUN.Java.SDK/jdk-1_5_0-doc.zip";就能直接拷贝下来了
    但ftp上有好多文件夹都是以中文命名的,这个得怎么解决?
    现在我只能以FTPFile[] files1 = ftpClient.listFiles(ftpClient.printWorkingDirectory());得到返回的files1数组,然后再通过
    ftpClient.changeWorkingDirectory(remote)比如说remote=files1[2]进入该子目录3(子目录3是中文命名的),有什么直接的方法进入一个中文目录吗?
      

  2.   

    试试看FTPClientConfig:FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
    conf.setServerLanguageCode("zh");
    ftpClient.configure(conf);
      

  3.   

    这个貌似不行唉String remote="/music/1.中文流行/";
    //并在try块中添加了如下语句
    FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); 
    conf.setServerLanguageCode("zh"); 
    ftpClient.configure(conf);

    System.out.println(ftpClient.changeWorkingDirectory(remote));
    System.out.println(ftpClient.printWorkingDirectory());结果System.out.println(ftpClient.changeWorkingDirectory(remote));打印的是false
    System.out.println(ftpClient.printWorkingDirectory());打印的是/。
    这还是说明进不去中文名命名的文件夹
    这个remote="/music/1.中文流行/";在作为参数传输时,"/music/1.中文流行/"编码方式是不是会改变,导致不可认?
      

  4.   

    解决了结贴了
    String remote = "/music/1.中文流行/female_女歌手/阿桑";

    remote = new String(remote.getBytes("GBK"),"ISO-8859-1");
    得到的中文目录再转诲编码。
      

  5.   

    大家好,我做了一个FTPClient下载的小程序测试,可以下载可是有个问题,就是我把项目部署在A电脑上面,FTP服务器也在A电脑上面,在A电脑上面可以成功下载到我指定的目录里面去,可以当我在B电脑访问我的项目下载东西的时候却也下载到了A电脑我指定的目录里面去了,有高手解决过这个问题没,可以把源代码给我参考吗?