apache的项目nets项目--commons-net-1.2.2.jar功能齐全,例子代码如下:

apache的项目nets项目--commons-net-1.2.2.jar功能齐全,例子代码如下:

import java.io.*;
import java.net.InetAddress;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.ftp.parser.*;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPFileEntryParser;

import sun.net.TelnetInputStream;

public class FtpAppache {
    public FtpAppache() throws Exception{
       // sun.net.ftp.FtpClient ft = null;
      //  TelnetInputStream t = ft.list();
       // t.setStickyCRLF(true);
    }

    public void test1() throws Exception {
        //
        String strTemp = "";

        //InetAddress ia = InetAddress.getByName("192.168.0.193");
        FTPClient ftp = new FTPClient();
        ftp.connect("192.168.0.193");

        boolean blogin = ftp.login("de", "123456");
        if (!blogin) {
            System.out.println("连接失败");
            ftp.disconnect();
            ftp = null;
            return;
        }

        /*//如果是中文名必需进行字符集转换
        boolean bMakeFlag = ftp.makeDirectory(new String("测试目录".getBytes(
            "gb2312"), "iso-8859-1")); //在服务器创建目录
        //上传文件到服务器,目录自由创建
        File file = new File("c:\\test.properties");
        ftp.storeFile("test.properties",new FileInputStream(file));*/

        System.out.println(ftp.getSystemName());

        FTPFile[] ftpFiles = ftp.listFiles();
        if (ftpFiles != null) {
            for (int i = 0; i < ftpFiles.length; i++) {
                System.out.println(ftpFiles[i].getName());
                //System.out.println(ftpFiles[i].isFile());
                if (ftpFiles[i].isFile()) {
                    FTPFile ftpf = new FTPFile();
                    /*System.err.println(ftpf.hasPermission(FTPFile.GROUP_ACCESS,
                                  FTPFile.EXECUTE_PERMISSION));
                    System.err.println("READ_PERMISSION="+ftpf.hasPermission(FTPFile.USER_ACCESS,
                                      FTPFile.READ_PERMISSION));
                    System.err.println("EXECUTE_PERMISSION="+ftpf.hasPermission(FTPFile.USER_ACCESS,
                                  FTPFile.EXECUTE_PERMISSION));
                    System.err.println("WRITE_PERMISSION="+ftpf.hasPermission(FTPFile.USER_ACCESS,
                                  FTPFile.WRITE_PERMISSION));
                    System.err.println(ftpf.hasPermission(FTPFile.WORLD_ACCESS,
                                      FTPFile.READ_PERMISSION));*/

                }
                //System.out.println(ftpFiles[i].getUser());
            }
        }

        //下载服务器文件
        FileOutputStream fos = new FileOutputStream("e:/proftpd-1.2.10.tar.gz");
        ftp.retrieveFile("proftpd-1.2.10.tar.gz",fos);
        fos.close();

        //改变ftp目录
        //ftp.changeToParentDirectory();//回到父目录
        //ftp.changeWorkingDirectory("");//转移工作目录
        //ftp.completePendingCommand();//

        //删除ftp服务器文件
        //ftp.deleteFile("");

        //注销当前用户,
        //ftp.logout();
        //ftp.structureMount("");
        ftp.disconnect();
        ftp = null;
    }

    public static void main(String[] args) {
        try {
            FtpAppache ftpAppache1 = new FtpAppache();
            ftpAppache1.test1();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}

posted on 2005-07-29 17:15  轻松逍遥子  阅读(2074)  评论(0编辑  收藏  举报