我有一段ftp上传的代码:import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class testFtp
{
 private static FtpClient ftpClient;
 public static boolean connectServer(String ip, int port,String user,  String password)
 {
  try 
  {
   ftpClient = new FtpClient();
   ftpClient.openServer(ip,port);
   ftpClient.login(user,password);
   System.out.println("login success!");
   return true;
  } 
  catch (IOException ex) 
  {
   System.out.println(ex);
   return false;
 }
}public static void closeConnect()
{
 try 
 {
  ftpClient.closeServer();
 } 
 catch (IOException ex)
 { 
  System.out.println("not disconnect");
  System.out.println(ex);
 }
}public static boolean upload(File localfilename,String remotefilename,String ftppath) 
{
  if (ftppath.length() != 0)
  {
try
{
  ftpClient.cd(ftppath);
  /*这是在服务器上建文件夹 
           String new_dir="20070810";
  String cmd_mkdir="MKD "+new_dir+"\r\n;      
           ftpClient.sendServer(cmd_mkdir);   
  ftpClient.cd(new_dir);*/
    //现在的问题是,如果我不在上传的时候建文件夹,则能上传成功
    //一旦建立文件夹,再上传的话,报错:
    //sun.net.ftp.FtpProtocolException: PORT :500 'EPRT    
    //|1|172.18.114.222|4467|': command not understood ---------------(1)
 }
 catch (IOException ex)
 {
  System.out.println(ex);
 }
 
  }
 try 
 {
  ftpClient.binary();
  TelnetOutputStream os = ftpClient.put(remotefilename);
  File file_in = localfilename;
  FileInputStream is = new FileInputStream(file_in);
  byte[] bytes = new byte[1024];
  int c;
  while ((c = is.read(bytes)) != -1) 
      os.write(bytes, 0, c);   
  is.close();
  os.close();
  System.out.println("upload success"); 
  return true;
 } 
 catch (IOException ex) 
 {
  System.out.println("upload fail");
  System.out.println(ex);
  return false;
 }}public static void main(String agrs[]) {
 testFtp.connectServer("172.18.114.169",21,"aa","aa");
 testFtp.upload(new File("./ftpdir/abcdef.txt"),"aa.txt","./testdir");
 testFtp.closeConnect(); 

 }
}问题就是(1)所说的那样,请大家帮忙,代码可直接执行。

解决方案 »

  1.   

    对了,报错的地方是 TelnetOutputStream os = ftpClient.put(remotefilename);//建文件
    谢谢大家
      

  2.   

    無法測試你的,以下是我寫過的一部分,看能不能對你有啟發
    /**
       * 備份到網絡磁碟機上
       *
       * @throws Exception
       */
      private void backtonet() throws Exception {
        try {
          Prop prop = Prop.getInstance(oContext_);
          NetPath_ = prop.get("CSMADJBAT_FOLDER").trim();
        } catch (Exception e) {
          throw new Exception("取得 CSMADJBAT_FOLDER 失敗 " + e.getMessage());
        }    try {
          File netdir = new File(NetPath_);
          if (!netdir.exists()) {
            netdir.mkdirs();
          }      try {
            File file = new File(sUpFilePath);
            sUpFileName = file.getName();
            FileOutputStream outs = new FileOutputStream(NetPath_ + File.separator + sUpFileName, false);
            BufferedOutputStream bouts = new BufferedOutputStream(outs);
            FileInputStream ins = new FileInputStream("sUpFilePath");
            try {
              byte[] buf = new byte[8192];
              int readed;
              BufferedInputStream bis = new BufferedInputStream(ins);
              while ((readed = bis.read(buf)) != -1) {
                bouts.write(buf, 0, readed);
              }
              bouts.flush();
            } finally {
              outs.close();
              outs = null;
              ins.close();
              ins = null;
            }
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        } catch (Exception ex) {
          throw new Exception("備份到網絡磁碟失敗 " + ex.getMessage());
        }
      }
      

  3.   

    楼上甩的是文件上传的代码吧......
    貌似sun的ftpClient没有建立目录的api哦 
    还是用别的吧...
      

  4.   

    上面没经过测试就妄言 请勿见怪
    使用sendserver发送命令后,还应该要解析发回来的返回信息。并不是一个命令发送完后就可以发送下一个命令。有时候要等待到就绪状态才行。
    FtpClient中有issueCommand()方法就是完成这个工作的。不过是protected方法。可以直接继承该类后写一个mkd方法。
    import java.io.IOException;import sun.net.ftp.FtpClient;
    /**
     * @author zxum 2007-8-10 下午07:57:39
     * 
     */
    public class MyFtpClient extends FtpClient
    {
        MyFtpClient(){
            super();
        }
        
        public void mkd(String dirName){
            String cmd = "MKD " + dirName;
            try
            {
                this.issueCommandCheck(cmd);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        /**
         * @param args
         */
        public static void main(String[] args)
        {
            // TODO 自动生成方法存根    }}
      

  5.   

    这几天忙得没顾上上网。
    非常感谢windvscloud(大翼) 
    请问怎么判断ftp服务器上是否存在指定目录?
    分不够可以再加
      

  6.   

    如果文件夹存在 mkd中应该会catch到failed to create.的异常。在catch中处理就是了。
    或者你可以先用list()方法取回所有的目录,判断一下就是了撒
    drw-rw-rw-   1 user     group           0 Aug 10 17:09 .
    drw-rw-rw-   1 user     group           0 Aug 10 17:09 ..
    drw-rw-rw-   1 user     group           0 Aug 15 20:05 20070810
      

  7.   

    谢谢!其实我用的就是list的方法,只不过我想有没有更好的方法.先给分.
    顺便问一下,我的程序对有的ftp地址可以成功上传,有的抛异常
    501 PORT not allowed after EPSV ALL,你有过这方面的经验吗?