<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-200  評論-148  文章-15  trackbacks-0
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;

    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPClientConfig;
    import org.apache.commons.net.ftp.FTPFile;

    public class DownFTPFile {

    private String hostname;
    private String username;
    private String password;
    private String localBaseDir;
    private String remoteBaseDir;


    private static FTPClient ftpClient = new FTPClient();

    public static void main(String[] args) {
    String hostname = "172.18.72.66";
    String username = "sfdc3";
    String password = "sfdcadmin";
    String localBaseDir = "c:/temp/";
    String remoteBaseDir = "ptsinstall/v3.20.06";

    DownFTPFile downFTPClient = new DownFTPFile();
    downFTPClient.setHostname(hostname);
    downFTPClient.setUsername(username);
    downFTPClient.setPassword(password);
    downFTPClient.setLocalBaseDir(localBaseDir);
    downFTPClient.setRemoteBaseDir(remoteBaseDir);
    downFTPClient.startDown();
    }

    private void startDown() {
    try {
    ftpClient.connect(hostname);
    ftpClient.login(username, password);
    FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX);
    ftpClient.configure(conf);
    } catch (Exception e1) {
    e1.printStackTrace();
    }

    try {
    FTPFile[] files = null;
    boolean changedir = ftpClient.changeWorkingDirectory(remoteBaseDir);
    if ( changedir ){
    files = ftpClient.listFiles();
    for (int i = 0; i < files.length; i++) {
    downloadFile(files[i], localBaseDir, remoteBaseDir);
    }
    }
    } catch (Exception e) {
    System.out.println(e);
    }
    }

    private void downloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) {
    if (ftpFile.isFile()) {//down file
    if (ftpFile.getName().indexOf("?") == -1) {
    OutputStream outputStream = null;
    try {
    outputStream = new FileOutputStream(relativeLocalPath+ ftpFile.getName());
    ftpClient.retrieveFile(ftpFile.getName(), outputStream);
    outputStream.flush();
    outputStream.close();
    } catch (Exception e) {
    System.err.println( e );
    } finally {
    try {
    if (outputStream != null)
    outputStream.close();
    } catch (IOException e) {
    System.out.println("ShowasaFile");
    }
    }
    }
    } else { //deal dirctory
    String newlocalRelatePath = relativeLocalPath + ftpFile.getName();
    String newRemote = new String(relativeRemotePath+ ftpFile.getName().toString());

    File fl = new File(newlocalRelatePath);
    if (!fl.exists()) {
    fl.mkdirs();
    }
    try {
    newlocalRelatePath = newlocalRelatePath + '/';
    newRemote = newRemote + "/";
    String currentWorkDir = ftpFile.getName().toString();
    //enter relative workdirectory
    boolean changedir = ftpClient.changeWorkingDirectory(currentWorkDir);
    if (changedir) {
    FTPFile[] files = null;
    files = ftpClient.listFiles();
    for (int i = 0; i < files.length; i++) {
    downloadFile(files[i], newlocalRelatePath, newRemote);
    }
    }
    //return parent directory
    if (changedir)
    ftpClient.changeToParentDirectory();
    } catch (Exception e) {
    System.out.println(e);
    }
    }
    }


    public String getHostname() {
    return hostname;
    }


    public void setHostname(String hostname) {
    this.hostname = hostname;
    }


    public String getPassword() {
    return password;
    }


    public void setPassword(String password) {
    this.password = password;
    }


    public String getUsername() {
    return username;
    }


    public void setUsername(String username) {
    this.username = username;
    }


    public String getLocalBaseDir() {
    return localBaseDir;
    }


    public void setLocalBaseDir(String localBaseDir) {
    this.localBaseDir = localBaseDir;
    }


    public String getRemoteBaseDir() {
    return remoteBaseDir;
    }


    public void setRemoteBaseDir(String remoteBaseDir) {
    this.remoteBaseDir = remoteBaseDir;
    }
    }
    posted on 2008-05-16 13:22 無聲 閱讀(5347) 評論(2)  編輯  收藏 所屬分類: 職場生活

    評論:
    # re: 一個java 下載FTP( Linux)服務器文件夾的例子 2011-08-21 15:23 | ww
    非常感謝,代碼很詳盡哦  回復  更多評論
      
    # re: 一個java 下載FTP( Linux)服務器文件夾的例子 2012-04-18 20:32 | pingdhm
    感謝你  回復  更多評論
      
    主站蜘蛛池模板: 亚洲第一福利视频| 久久久久久AV无码免费网站 | 亚洲精品无码久久毛片波多野吉衣| 成人午夜免费视频| 国产精品自在自线免费观看| 亚洲大尺度无码无码专线一区| 国产精品成人免费视频网站京东 | 亚洲精品高清无码视频| 成av免费大片黄在线观看| 亚洲中久无码永久在线观看同| 亚洲免费无码在线| 亚洲bt加勒比一区二区| 免费看又黄又无码的网站| 亚洲成A∨人片在线观看无码| 午夜宅男在线永久免费观看网 | 四虎影视永久在线精品免费| 亚洲无线一二三四区手机| 国产精品美女免费视频观看| 国产亚洲人成网站在线观看不卡 | 精品一区二区三区免费| 亚洲欧洲精品国产区| 国语成本人片免费av无码| 久久精品国产亚洲AV天海翼| 亚洲精品成人a在线观看| 永久在线观看免费视频| 亚洲人成在线精品| 国产区卡一卡二卡三乱码免费| 一级a性色生活片久久无少妇一级婬片免费放 | 亚洲精品无AMM毛片| 国产福利电影一区二区三区,亚洲国模精品一区 | 亚洲日本中文字幕天天更新| 五月婷婷亚洲综合| 久久香蕉国产线看免费| 亚洲成年网站在线观看| 亚洲精品亚洲人成在线观看下载 | 小日子的在线观看免费| 亚洲AV无码一区二区三区在线| 免费h成人黄漫画嘿咻破解版| 久久久免费的精品| 色偷偷亚洲第一综合网| 亚洲人成网站在线播放影院在线|