我现在用sun.net.ftp.*实现从ftp服务器上下载文件后删除服务器上文件,我没找到实现方法,求解。有其他方法也可以。谢谢。

解决方案 »

  1.   

    去jakarta.apache.com上下相应的ftp包就行了
      

  2.   

    package nssol.util;
    import sun.net.ftp.*;
    import sun.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.util.*;
    public class FTP {  public FTP() {
      }  private static FtpClient m_client;  protected static void disconnect()
      {
        if (m_client != null)
        {
          try
          {
            m_client.closeServer();
          }
          catch (IOException ex)
          {
          }      m_client = null;
        }
      }  protected static boolean connect(String sHost, String user,
                String password ,String sDir)
      {
        try
        {
          m_client = new FtpClient(sHost);
          m_client.login(user, password);
          m_client.cd(sDir);
          m_client.binary();
        }
        catch (Exception ex)
        {
          return false;
        }    return true;
      }  protected static boolean putFiletoServer(String m_sLocalFile,String m_sHostFile)
      {
        if (m_sLocalFile.length()==0)
        {
          return false;
        }    byte[] buffer = new byte[10240];    try
        {
          File f = new File(m_sLocalFile);
          int size = (int)f.length();
          FileInputStream in = new FileInputStream(m_sLocalFile);
          OutputStream out = m_client.put(m_sHostFile);      int counter = 0;
          while(true)
          {
            int bytes = in.read(buffer);
            if (bytes < 0)
              break;
            out.write(buffer, 0, bytes);
            counter += bytes;
          }      out.close();
          in.close();
        }
        catch (Exception ex)
        {
          return false;
        }    return true;
      }  protected static boolean renameFileFromServer(String src, String dst)
      {
    try
    {
    m_client.rename(src, dst);
    }
    catch (Exception ex)
    {
    return false;
    }
    return true;
      }  public static boolean putFile(String pathname,String ftpServer, String ftpUser,
                               String ftpPasswd, String ftpPath)
      {
    String filename;
    String tmpPathName;
    String tmpFileName; // step 0 : init
      {
      File f = new File(pathname);
      filename = f.getName();   tmpPathName = pathname + ".tmp";
      tmpFileName = filename + ".tmp";   //System.out.println(pathname);
      //System.out.println(filename);
      //System.out.println(tmpPathName);
      //System.out.println(tmpFileName);   } // step 1 :  rename local filename
      {
    File f = new File(pathname);
    File g = new File(tmpPathName); if (!f.renameTo(g))
    return false;
      }

    if (!connect(ftpServer,ftpUser,ftpPasswd,ftpPath))
        {
          return false;
        } // step 2 : ftping
        if (!putFiletoServer(tmpPathName,tmpFileName))
        {
          return false;
        } // step 3 : rename server name
    if (!renameFileFromServer(tmpFileName,filename))
    {
      return false;
    } // step 4 : rename local name
      {
    File f = new File(tmpPathName);
    File g = new File(pathname); if (!f.renameTo(g))
    return false;
      }    disconnect();
        return true;

      }  
      /*   sample   */
      
      public static void main(String[] args)
      {
        boolean b = putFile("c:/2.txt","192.168.2.79","dexin","123456","html");
        System.out.println(b);
      }
      
    }//这是一个利用sun.net.ftp.*包实现FTP的例子,你可以看看,具体的再查找一下帮助
      

  3.   

    楼上的兄弟,我需要知道怎么实现ftp 删除服务器上的已经下载过得文件,其他方法我是实现了。
      

  4.   

    jakarta.apache.com登陆不了,可以发给我吗?[email protected],非常感谢