我自己本机装了一个ftp服务器,设了一个目录.我用这个程序都可以把里面的文件全部下载到本地服务器.但是当我登录到外网的FTP上怎么一个文件都不能下载啊...我看了一下外网的那个ftp程序.他里面有两个目录.一个是cm 一个是pm.而我要下载的是pm里的文件.是不是我还要设定路径才可以啊
下面的是部分代码..请各位看看
连接到ftp
public FTPClient connect() throws Exception
{
try
{
ftp = new FTPClient();
if (verbose)
{
ftp.addProtocolCommandListener(new PrintCommandListener());
}
ftp.connect(hostname);
log.debug("Now connecting to " + hostname);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
disconnect();
throw new Exception("FTP server refused connection.");
}
if (!ftp.login(username, password))
{
disconnect();
throw new Exception("Username or password is wrong.");
}
log.debug("Remote system is " + ftp.getSystemName());
}
catch (java.net.ConnectException ex)
{
disconnect();
throw new java.net.ConnectException("服务器【" + hostname + "】连接失败!");
}
catch (java.net.NoRouteToHostException ex)
{
disconnect();
throw new java.net.NoRouteToHostException("服务器【" + hostname + "】连接失败!");
}
catch (IOException ex)
{
disconnect();
log.error("", ex);
throw ex;
}
return ftp;
}
下载文件
private void downloadFile(String localWorkingDir) throws Exception{   
    FTPClient fc = getConnectedFTPClient();    
   // fc.setFileType(FTPClient.BINARY_FILE_TYPE);   
       
    localWorkingDir = localWorkingDir.concat(File.separator);   
    FTPFile[] sourceFiles = fc.listFiles();   
    String localFilePath = null;   
    String FileName = null;   
    for(FTPFile sourceFile : sourceFiles){
        if(sourceFile.isFile()){
         FileName = sourceFile.getName(); 
            String soucceFileName=new String(FileName.getBytes("GBK"),"iso-8859-1");
            localFilePath = localWorkingDir.concat(soucceFileName); 
            try {   
                FileOutputStream os = new FileOutputStream(localFilePath);   
                fc.retrieveFile(soucceFileName, os);
                System.out.println(soucceFileName);
                os.close();
            } catch (IOException e) {
                e.printStackTrace();   
            }   
        }  
    } 主方法
public static void main(String args[]) throws Exception{
FTPBean ftpBean = new FTPBean(Configure.HOSTNAME, Configure.USERNAME, Configure.PASSWORD, false);
ftpBean.connect();
ftpBean.setBinary();
ftpBean.downloadFile(Configure.SERVER);
ftpBean.disconnect();
}  

解决方案 »

  1.   

    用一个账号登陆上去会让你操作制定目录下的文件
    你登上去的不一定是你要用的目录
    本地连接上就是操作那个目录了
    就好比用tomcat用户登陆  登陆以后就在tomcat目录下
      

  2.   

    FTPFile[] sourceFiles = fc.listFiles();  
    你下载的是账号登陆上去之后FTP服务器的默认路径,
    你可以使用类似  cwd 命令获取当前工作路径,
    你需要 switch 到你要下载的目录下载才行