怎么通过TFTP或FTP方式收集配置数据!!
给点思路就可以!谢谢各位先辈了!
小弟在这谢谢了

解决方案 »

  1.   

    在网上找个FTP开源jar包,学习其用法即可。
    先把配置数据序列化到一个文件里,上传该文件即可。
    as:
    import cz.dhl.ftp.Ftp;
    import cz.dhl.ftp.FtpConnect;
    import cz.dhl.ftp.FtpFile;
    import cz.dhl.io.CoFile;
    import cz.dhl.io.CoLoad;
    import cz.dhl.io.LocalFile;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;import java.io.File;
    import java.io.IOException;public class FileServer { private static final Log LOG = LogFactory.getLog(FileServer.class); private static FileServer instance; public static FileServer getInstance() {
    if (instance == null) {
    instance = new FileServer();
    } return instance;
    } // 文件服务器的绝对路径
    private String fileServerDir; private FileServer() {
    } /**
     * 获取文件服务器的绝对路径
     */
    public String getFileServerDir() {
    return fileServerDir;
    } public static String getFullPath(String parent, String fileName) {
    File file = new File(parent, fileName);
    return file.getAbsolutePath();
    } public static void list(String host, String username, String passwd) {
    // host = ftp.datangmobile.com;
    // path = pub;
    // user = anonymous (default);
    // port = 21 (default); */
    FtpConnect cn = FtpConnect.newConnect(host);
        cn.setHostName(host);
        cn.setPathName("c:\\");
    // Guest login ok, send your password
    cn.setUserName(username);
    cn.setPassWord(passwd);
    Ftp cl = new Ftp(); try {
    // connect & login to host
    if(!cl.connect(cn)){
    LOG.info("连接FTP服务器失败,三秒后再次尝试连接!");
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    LOG.error("FTP三秒尝试中断过程中出现异常!", e);
    }
    LOG.info("再次尝试连接到FTP服务器.");
    cl.connect(cn);
    }

    // get current directory
    CoFile dir = new FtpFile(cl.pwd(), cl); // list & print current directory
    CoFile fls[] = dir.listCoFiles();
    if (fls != null) {
    for (CoFile fl : fls) {
    System.out.println(fl.getName()
    + (fl.isDirectory() ? "/" : ""));
    }
    }
    } catch (IOException e) {
    LOG.error("FTP ERROR!", e);
    } finally {
    // disconnect from server this must be always run
    cl.disconnect();
    }
    } public static boolean upload(String host, String username, String passwd,
    String path, String name, String remoteName) {
    // host = ftp.datangmobile.com;
    // path = pub;
    // user = anonymous (default);
    // port = 21 (default); */
    FtpConnect cn = FtpConnect.newConnect(host);
    cn.setHostName(host);
    //cn.setPathName("abc");
    cn.setUserName(username);
    cn.setPassWord(passwd);
    Ftp cl = new Ftp();
    int connectNum=0;
    try {
    // connect & login to host
    while(!cl.connect(cn)){
    LOG.info("连接FTP服务器失败,三十秒后再次尝试连接!");
    if(connectNum++>=5){
    LOG.info("累计尝试连接FTP已达五次,连接失败.");
    return false;
    }
    try {
    Thread.sleep(30000);
    } catch (InterruptedException e) {
    LOG.error("FTP三十秒尝试中断过程中出现异常!", e);
    }
    LOG.info("再次尝试连接到FTP服务器.......");
    }

    // source FtpFile remote file
    CoFile file = new FtpFile(remoteName, cl);

    // destination LocalFile
    CoFile to = new LocalFile(path, name); // 上传
    CoLoad.copy(file, to); // 上载
    //CoLoad.copy(to,file);
    } catch (IOException e) {
    LOG.error("FTP ERROR!", e);
    } finally {
    cl.disconnect();
    }
    return true;
    } public static void download(String host, String username, String passwd,
    String path, String name, String remoteName) {
    // host = ftp.datangmobile.com;
    // path = pub;
    // user = anonymous (default);
    // port = 21 (default); */
    FtpConnect cn = FtpConnect.newConnect(host);
    cn.setHostName(host);
    //cn.setPathName("abc");
    cn.setUserName(username);
    cn.setPassWord(passwd); Ftp cl = new Ftp();

    try {
    // connect & login to host
    cl.connect(cn); // source FtpFile remote file
    CoFile file = new FtpFile(remoteName, cl); // destination LocalFile
    CoFile to = new LocalFile(path, name); // 下载
    CoLoad.copy(to,file);
    } catch (IOException e) {
    LOG.error("FTP ERROR!", e);
    } finally {
    cl.disconnect();
    }
    }

    public static void delete(String host, String username, String passwd,
    String path, String name, String remoteName) {
    // host = ftp.datangmobile.com;
    // path = pub;
    // user = anonymous (default);
    // port = 21 (default); */
    FtpConnect cn = FtpConnect.newConnect(host);
    cn.setHostName(host);
    //cn.setPathName("abc");
    cn.setUserName(username);
    cn.setPassWord(passwd); Ftp cl = new Ftp();
    try {
    // connect & login to host
    cl.connect(cn); // source FtpFile remote file
    CoFile[] file = new FtpFile[]{new FtpFile(remoteName, cl)};

    //删除
    CoLoad.delete(file); } catch (IOException e) {
    LOG.error("FTP ERROR!", e);
    } finally {
    cl.disconnect();
    }
    }
    }