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

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

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

    tinguo002

     

    java FTP Linux系統下 文件上傳下載

    http://m.tkk7.com/parable-myth/archive/2008/05/16/200879.html
    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);

    /**

    下載單個文件文件
    InputStream is = ftpClient.retrieveFileStream(remoteBaseDir);
       BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf8"));

       String str="";
       try {
        while((str=br.readLine())!=null){
         System.out.println(str);
        }
       } catch (Exception e) {       
        e.printStackTrace();
       }
    */
    }
    }

    }
     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;
    }

    }
     






    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"), "utf8")); 

    歡迎大家訪問我的個人網站 萌萌的IT人

    posted on 2014-04-10 18:42 一堣而安 閱讀(752) 評論(0)  編輯  收藏 所屬分類: java

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲免费在线观看| 国产成人精品免费视频大全五级 | 亚洲av无码专区首页| 久久精品免费一区二区喷潮| 亚洲导航深夜福利| 日日麻批免费40分钟日本的| 亚洲AV综合色区无码二区偷拍| 国产无人区码卡二卡三卡免费 | 国产成人免费福利网站| 久久精品国产亚洲AV电影网| 国产免费小视频在线观看| 在线精品自拍亚洲第一区| 亚洲国产精品13p| 久久久久久噜噜精品免费直播 | 久久精品亚洲乱码伦伦中文| 国产一区二区三区免费观看在线| 亚洲AV无码成人精品区天堂| 免费A级毛片av无码| 亚洲制服丝袜中文字幕| 免费看国产精品麻豆| 精品熟女少妇aⅴ免费久久| 亚洲AV无码一区二区三区DV| 美丽的姑娘免费观看在线播放| 亚洲丝袜中文字幕| 免费在线黄色网址| 免费黄网站在线观看| 久久精品国产亚洲AV忘忧草18 | 久久久久久A亚洲欧洲AV冫| 日韩免费观看一区| 亚洲色偷偷偷综合网| 亚洲精品视频在线观看你懂的| 久久久久国产精品免费网站| 亚洲伊人久久大香线蕉AV| 亚洲伊人久久综合中文成人网| 久久久99精品免费观看| 亚洲第一成年网站视频| 国产亚洲综合久久系列| 一个人免费观看视频www| 高清永久免费观看| 亚洲国产精品一区二区三区在线观看 | 亚洲码国产精品高潮在线|