本段程序是实现按照本地机器上的一个文件夹目录结构传送到FTP服务器上的功能,为了简化方便高手的解答我只传送一层,多层跟这个一样的问题!下面程序中一个是判断文件和文件夹的,两个相互分开时,可以运行,但放在一起会出错!
错误信息:
java.io.FileNotFoundException: CWD /2: 550 /2/???¨??????: Failed to create. at sun.net.ftp.FtpClient.readReply(Unknown Source)
at sun.net.ftp.FtpClient.issueCommand(Unknown Source)
at sun.net.ftp.FtpClient.issueCommandCheck(Unknown Source)
at sun.net.ftp.FtpClient.cd(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:110)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)
sun.net.ftp.FtpProtocolException: PORT :500 'EPRT': command not understood. at sun.net.ftp.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.FtpClient.put(Unknown Source)
at shareit.ftp.FtpConnection.run(FTPtxt.java:118)源程序是:
package shareit.ftp;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;class Node
{
File[] fle;
}
 class FtpConnection extends Thread
{   //private  String user;
    //private  String password;

    private  FtpClient ftpclient;
FtpConnection()
{
 try{ftpclient=new FtpClient("192.168.0.66",21);
 
     Login();
    }
catch(Exception e){}
}
 public void Login()
{
try {

ftpclient.login("winterzd", "2251780");


} catch (IOException e) {
e.printStackTrace();
}
}
 public void Logout()
 {
 
 ftpclient.sendServer("QUIT\r\n");
         int reply = 0;
try {
reply = ftpclient.readServerResponse();
} catch (IOException e) {

e.printStackTrace();
}
System.out.println(reply);
 }
public void run()
{
try {

 
 //readirt();
  //*********************************************
File file=new File("D:\\2");

File[] filelist=file.listFiles();

 int count=filelist.length;
 System.out.println(count);
      int i=count-1;
      while(i>=0)
      {   
          
       if(filelist[i].isDirectory()==false)
      /*{try{
        
        String path=filelist[i].getAbsolutePath();
        String[] pathsplit=path.split("\\\\");
        int j=pathsplit.length-1;
               
        int m=1;
        String pathafter="/";
        while(m<=j)
        {
     if(m<j)pathafter=pathafter.concat(pathsplit[m])+"/";
     else pathafter=pathafter.concat(pathsplit[m]);
    
         m++;
        
        }
        System.out.println(pathafter);
       // pathafter="/test";
        ftpclient.sendServer("XMKD" + pathafter + "\r\n");
        
       
           }
        catch (Exception e) {e.printStackTrace();}
       }
       else{*/
       try{
       
       String path=filelist[i].getParent();
       
       String[] pathsplit=path.split("\\\\");
       int j=pathsplit.length-1;
              
       int m=1;
       String pathafter="/";
       while(m<=j)
       {
        if(m<j)pathafter=pathafter.concat(pathsplit[m])+"/";
        else pathafter=pathafter.concat(pathsplit[m]);
        m++;
       }
       ftpclient.cd(pathafter);
       String name=filelist[i].getName();        //File f=new File(path,name);
      
       ftpclient.binary();
       
       FileInputStream input=new FileInputStream(filelist[i]);    
       TelnetOutputStream output=ftpclient.put(name);
       int bytesRead;
       while((bytesRead=input.read())!=-1) 
       output.write(bytesRead);
       input.close();
       output.close();
               }
                catch (Exception e) {e.printStackTrace();}
      //} 
      i--;
      }
         //**********************************************
  }
catch(Exception ee){}
 Logout();
}
}public class FTPtxt {
public static void main(String args[])
{

FtpConnection ftp=new FtpConnection();
ftp.start();

}
}

解决方案 »

  1.   

    你只需要提供最上层的目录路径就可以了,也就是替换我的程序中初始化类中的File f=new File("d:\\temp");参数即可
    import java.io.File;public class test2 {
      public test2() {
        File f=new File("d:\\temp");
        String path=f.getAbsolutePath();
        String list[]=f.list();    for (int i=0;i<list.length;i++)
        {
          File f2=new File(path+"\\"+list[i]);      if (f2.isDirectory()==true)
          {
            this.showlist(path+"\\"+list[i]);
          }
          else
          {
            System.out.println(path+"\\"+list[i]);
          }
        }
      }  public void showlist(String path){
        File f3=new File(path);
        String path2=f3.getAbsolutePath();
        String list2[]=f3.list();    for(int j=0;j<list2.length;j++)
        {
          File ff=new File(path2+"\\"+list2[j]);
          if(ff.isDirectory()==true)
          {
            this.showlist(path+"\\"+list2[j]);
          }
          else
          {
            System.out.println(path2+"\\"+list2[j]);
          }
        }
      }  public static void main(String[] args) {
        test2 test21 = new test2();
      }
    }
      

  2.   

    谢谢你的回答,你的程序是遍历文件夹下目录和文件,我的问题是不能把FILE传到FTP上,能在上面建立文件夹,但是不能 写文件,把判断是文件夹的那部分屏蔽掉,可以写,但是两个在一起 ,文件就不能写!请帮我解决,谢谢!
      

  3.   

    来领分呵呵
    ===
    在ftpclient.sendServer("XMKD" + pathafter + "\r\n");之后加上这句:
    ftpclient.readServerResponse();
      

  4.   

    CrazyGou(阿狗)(...)强人,不仅程序帮我搞定,而且我发布的每个帖子都让你找到了,我不会吝啬的!哈……能否留下邮箱?谢谢!