<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无码一区二区乱子伦as| 特级无码毛片免费视频| 久久天天躁狠狠躁夜夜免费观看 | 亚洲精品宾馆在线精品酒店| 亚洲午夜福利精品无码| 亚洲一区二区三区高清不卡| 亚洲成av人片一区二区三区| 永久免费视频网站在线观看| 美女被免费视频网站| 亚洲电影在线播放| 亚洲大片免费观看| 成人福利在线观看免费视频| 亚洲欧洲精品一区二区三区| 国产成人免费福利网站| 亚洲日本在线免费观看| ww在线观视频免费观看w| 亚洲AV一二三区成人影片| 成年性午夜免费视频网站不卡| 国产福利免费视频| 日韩国产欧美亚洲v片 | 亚洲人成人网站18禁| 亚洲国产精品久久久久婷婷软件| 亚洲免费在线视频| 日韩精品无码免费视频| 亚洲av永久无码精品漫画| 国产乱子伦片免费观看中字| 999久久久免费精品国产| 97在线视频免费公开视频| 久久亚洲色WWW成人欧美| 亚洲最大中文字幕| 亚洲AV永久无码精品| 久久久青草青青国产亚洲免观 | 国产香蕉九九久久精品免费| 亚洲欧洲免费视频| 精品一区二区三区免费| 国产黄片不卡免费|