tangweijia

构客网首页  博客  论坛

 
  本文的标签
其他 (收录19784篇)技术 (收录2710篇)
  用户信息
 
帐号:  新手必读
密码: 保存密码
 
  分类列表
全部类别(16 篇)
我的文章(16 篇)
  按月归档
2008年-07月(8 篇)
2008年-11月(8 篇)
  SOA2007 - SOA实践
我们何时迈向SOA
——SOA在中国的整体发展现状究竟如何?
我们如何迈向SOA
——中国企业如何迈出实施SOA的第一步?
我们应采用何种技术
——SOA国际标准SCA/SDO的具体内涵?
我们还需要何种技能
——SOA将如何改变系统架构设计以及项目管理过程?

java ftp客户端

发布时间:2008年07月25日 作者:tangweijia

阅读次数:216次 类别:我的文章 永久链接 Trackback 
参加SOA我有话说
本文介绍如何通过java代码访问ftp服务器,从而实现ftp客户端

请看如下代码

 

 

package ftp;
import sun.net.ftp.FtpClient;
import java.io.*;
import sun.net.*;
import java.util.*;

public class Ftp {

  private TelnetInputStream fget;
  private  DataInputStream puts;
  private  final int BUFFER=2048;
  private static String ftpServer;
  private static String user;
  private static String pass;
  final private static String CONFIGFILE = "config.txt";

  public Ftp() {
    try {
     if(ftpServer==null){
       Properties p = new Properties();
       FileInputStream fin=new FileInputStream(CONFIGFILE);
       p.load(fin);
       ftpServer=p.getProperty("server");
       user=p.getProperty("user");
       pass=p.getProperty("pass");
       fin.close();
     }
   }
   catch (IOException ex) {
     ex.printStackTrace();
   }
  }

  public FtpClient getFtp(){
    FtpClient fc;
    try{
      fc = new FtpClient(ftpServer);
      if(!(user==null||user.equals(""))){
        fc.login(user, pass);
      }
      fc.binary();
      return fc;
    }catch(Exception e){
      e.printStackTrace();
      return null;
    }
  }

  public void cd(FtpClient fc,String dir){
    try {
      fc.cd(dir);
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public boolean downFile(FtpClient fc,String ftpDir,String ftpFileName,String localDir,String localFileName){
    try{
      int off=0;
      int len;
      byte data[]=new byte[BUFFER];
      FileOutputStream getFile;
      getFile = new FileOutputStream(localDir + localFileName,false);
      fc.cd(ftpDir);
      fget=fc.get(ftpFileName);
      puts = new DataInputStream(fget);
      while ((len=puts.read(data))>= 0){
        getFile.write(data,0,len);
        getFile.flush();
      }
      getFile.close();
      return true;
    }
    catch (Exception ex){
     ex.printStackTrace();
     return false;
    }
  }

 

  public static void main(String[] args) throws Exception{
    Ftp ftpClient1 = new Ftp();
    FtpClient f =ftpClient1.getFtp();
    //f.login("","");
   System.out.println(f.welcomeMsg);
    TelnetInputStream tis =f.list();
    OutputStream os = new FileOutputStream("c:\\te.t");
    byte[] b =new byte[tis.available()];
    tis.read(b);
    tis.close();
    os.write(b);
    os.flush();
    os.close();
  }

}


 评论 查看全部评论