一个简单的FTP客户端 实现上传下载

package  pub.ftp;

import  java.io.BufferedReader;
import  java.io.DataOutputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.InputStreamReader;
import  java.util.ArrayList;

import  sun.net.TelnetInputStream;
import  sun.net.TelnetOutputStream;
import  sun.net.ftp.FtpClient;

/**
 * FTP Client端调用模块
 * 作者: 
 * 版本:1.0
 * 时间:2007.07.30
 
*/


public   class  FTPClient  {
    
/**
     * 类的初始化,建立ftp的连接,用户登录,指定ftp的传输流
     * 
@param host
     * 
@param port
     * 
@param user
     * 
@param psw
     
*/

//    建立一条与指定主机、指定端口上的FTP服务器的连接
    private FtpClient aftp = new FtpClient(); ; 
    
private DataOutputStream outputs ; 
    
private TelnetOutputStream outs;
    
private TelnetInputStream  inps;


    
public void FTPClass(String host,int port,String user,String psw){
        
        
try {

            
//            注册到FTP服务器
            aftp.openServer(host);
            System.out.println(
"登陆....");
            aftp.login(user, psw);
            
            System.out.println(
"登陆FTP服务器成功!");
            aftp.binary();
            
        }
 catch (IOException e) {
            
            System.out.println(
"连接FTP服务器失败!");
            e.printStackTrace();
        }

        
        
    }

    
/**
     * 通过ftp上传文件到服务器上
     * 
@param localFile 本地所要上传的文件
     * 
@param remoteFile 传到服务器上的文件名称
     
*/

    
public boolean upFile(String localFile,String remoteFile){
        
boolean result =true;
        
if(aftp != null){
            System.out.println(
"正在上传文件"+localFile+",请等待....");
            
try{
                File file 
= new File(localFile);
                outs 
= aftp.put(remoteFile);
                FileInputStream in 
= new FileInputStream(file);
                
byte[] bytes = new byte[1024];
                   
int c;
                   
while ((c = in.read(bytes)) != -1{
                    outs.write(bytes, 
0, c);
                   }

                outs.close();
                in.close();
                System.out.println(
"上传文件"+localFile+"成功!");
                System.out.println(
"上传文件所在目录:"+remoteFile+"");
            }
catch(Exception e){
                e.printStackTrace();
                System.out.println(
"上传文件"+localFile+"失败!");
                result 
= false;
            }

            
        }
else{
            result 
= false;
        }

        
return result;
    }

    
/**
     * 下载FTP服务器上的文件
     * 
@param localFile 本地文件名
     * 
@param remoteFile 远程服务器文件名
     
*/

    
public boolean downFile(String remoteFile,String localFile){
        
boolean result =true;
        
if(aftp != null){
            System.out.println(
"正在下载文件"+remoteFile+",请等待....");
            
try{
                File file 
= new File(remoteFile);
                inps 
= aftp.get(remoteFile);
                aftp.cd(
"D;/jiadong");
                FileInputStream in 
= new FileInputStream(file);
//                RandomAccessFile getFile = new RandomAccessFile(file,"rw");
//                getFile.seek(0);
                FileOutputStream os = new FileOutputStream(file);

                
byte[] bytes = new byte[1024];
                   
int c;
                   
while ((c = in.read(bytes)) != -1{
                   
// .write(bytes, 0, c);
                       os.write(bytes, 0, c);
                       
                   }

                inps.close();
                in.close();
                os.close();
                System.out.println(
"下载文件"+remoteFile+"成功!");
                System.out.println(
"上传文件所在目录:"+localFile+"");
            }
catch(Exception e){
                e.printStackTrace();
                System.out.println(
"下载文件"+remoteFile+"失败!");
                result 
= false;
            }

        }

        
return false;
    }

    
/**
     * 断开ftp连接
     * 
@throws IOException 
     *
     
*/

    
public void disconnect() throws IOException{
        aftp.closeServer();
        System.out.println(
"FTP服务器连接断开!");
    }

//     返回当前目录的所有文件及文件夹

    
public ArrayList getFileList() throws IOException {
      BufferedReader dr 
= new BufferedReader(new InputStreamReader(aftp.list()));
      ArrayList al 
= new ArrayList();
      String s 
= "";
      
while ( (s = dr.readLine()) != null{
      al.add(s);
      }

      
return al;
    }

    
    
public static void main(String[] args) throws IOException{
        FTPClient ftpClient 
= new FTPClient();
        ftpClient.FTPClass(
"192.168.8.154"21"jiadong""jiadong");
        
//ftpClient.upFile("F:/music/想念.mp3","/jia/hehe.mp3");
        
        
//ftpClient.upFile("d:/jiadong","D:/workspace/msgfield.xml");
        ftpClient.downFile("hehe.xml""/jia/ruru.xml");
        ftpClient.disconnect();
        

    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值