要用到的jar:oro-2.0.8.jar,commons-net-1.3.0.jar,转载后纠错的版本


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class s {
    public static int port=21;
    public static String url="172.19.9.99";
    public static String username="administrator";
    public static String password="321";
    /**
     * Description: 向FTP服务器上传文件
     * @param url FTP服务器hostname
     * @param port FTP服务器端口,如果默认端口请写-1
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param path FTP服务器保存目录
     * @param filename 上传到FTP服务器上的文件名
     * @param input 输入流
     * @return 成功返回true,否则返回false
     */
    public static boolean uploadFile(String url, int port, String username, String password, String path,
        String filename, InputStream input)
    {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        ftp.setControlEncoding("GBK");
        try
        {
            int reply;
                               
            // 连接FTP服务器
            if (port > -1)
            {
                ftp.connect(url, port);
            }
            else
            {
                ftp.connect(url);
            }
                               
            // 登录FTP
            ftp.login(username, password);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return success;
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE); 
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(filename, input);
                               
            input.close();
            ftp.logout();
            success = true;
        }
        catch (IOException e)
        {
            success = false;
            e.printStackTrace();
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }
                       
    /**
     * Description: 从FTP服务器下载文件
     * @Version1.0 Jul 27, 2008 5:32:36 PM by 崔红保(cuihongbao@d-heaven.com)创建
     * @param url FTP服务器hostname
     * @param port FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param remotePath FTP服务器上的相对路径
     * @param fileName 要下载的文件名
     * @param localPath 下载后保存到本地的路径
     * @return
     */
    public static boolean downloadFile(String url, int port, String username, String password, String remotePath,
        String fileName, String localPath)
    {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        ftp.setControlEncoding("GBK");
        try
        {
            int reply;
                               
            // 连接FTP服务器
            if (port > -1)
            {
                ftp.connect(url, port);
            }
            else
            {
                ftp.connect(url);
            }
                               
            ftp.login(username, password);//登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs)
            {
                if (ff.getName().equals(fileName))
                {
                    File f=new File(localPath);
                    if(!f.exists()){
                        f.mkdir();
                    }
                    File localFile = new File(localPath + "/" + ff.getName());
                                       
                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }
                               
            ftp.logout();
            success = true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }
                       
    /**
     * <删除FTP上的文件>
     * <远程删除FTP服务器上的录音文件>
     * @param url FTP服务器IP地址
     * @param port FTP服务器端口
     * @param username FTP服务器登录名
     * @param password FTP服务器密码
     * @param remotePath 远程文件路径
     * @param fileName 待删除的文件名
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static boolean deleteFtpFile(String url, int port, String username, String password, String remotePath,
        String fileName)
    {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        ftp.setControlEncoding("GBK");
        try
        {
            int reply;
                               
            // 连接FTP服务器
            if (port > -1)
            {
                ftp.connect(url, port);
            }
            else
            {
                ftp.connect(url);
            }
                               
            // 登录
            ftp.login(username, password);
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                return success;
            }
                               
            // 转移到FTP服务器目录
            ftp.changeWorkingDirectory(remotePath);
            success = ftp.deleteFile( fileName);
            ftp.logout();
        }
        catch (IOException e)
        {
             e.printStackTrace();
            success = false;
        }
        finally
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }
    public static void main(String []args){
        //downloadFile(url, port, username, password, "data/innovate/", "1.jpg", "d:/1/");
                           
//      boolean s=deleteFtpFile(url, port, username, password, "data/innovate/", "95598服务情况.jpg");
//      System.out.println(s);
                           
//      File f = new File("d:/instPlug4MyEclipse.jar");
//        InputStream input;
//      try {
//          input = new FileInputStream(f);
//          boolean x=uploadFile(url, port, username, password, "data/innovate/", "instPlug4MyEclipse.jar", input);
//          System.out.println(x);
//      } catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
                           
    }
                       
}