FtpClient read write create upload download

package com.chinasoft.ge.util;

 

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.StringTokenizer;

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

public class FtpServer {
    private FtpClient ftpClient;
    private String FtpIP,FtpUser,FtpPwd;
    private int FtpPort = 21;
    private Boolean bConnected = false;
   
    public FtpServer(String IP, int port, String user, String pwd){
     FtpIP = IP;
     FtpPort = port;
     FtpUser = user;
     FtpPwd = pwd;
     ConnectServer(FtpIP, FtpPort, FtpUser, FtpPwd);
    }

    public Boolean ConnectServer(String IP, int port, String user, String pwd){
     try{
      //System.out.println(IP + "   " + port);
         ftpClient = new FtpClient(IP,port);
         ftpClient.login(user, pwd);
         System.out.println(IP +" FTP��¼�ɹ�!");           
         bConnected = true;
         //System.out.println("|" + IP + "bConnected:" + bConnected);
     }
     catch(IOException e){
      e.printStackTrace();
     }

     return bConnected;
    }

    // ��FTP�Ͻ��ļ���
    public void CreateDirectory(String path){
        //System.out.println("bConnected:" + bConnected);
     if(!bConnected) {System.out.println("CreateDirectory:FTP������δl��!"); return;}
     try{
   //System.out.println("00a");
   ftpClient.binary();
   //System.out.println("00a");
   
   // ���ļ���
   ftpClient.ascii();
   StringTokenizer s = new StringTokenizer(path,"/");//"abc4/abc2/abc3", "/"); // sign
   //System.out.println(path);
   int count = s.countTokens();
   //System.out.println(count);
   String pathName = "";
   while (s.hasMoreElements()) {
    pathName = pathName + "/" + (String) s.nextElement();
    //System.out.println(pathName);
    try {
     ftpClient.sendServer("XMKD " + pathName + "\r\n");
    } catch (Exception e) {
     e.printStackTrace();
    }
    int reply = ftpClient.readServerResponse();
   }
   System.out.println(FtpIP + " FTP���ļ��гɹ�!");
     }catch(Exception e){}

    }
   
    // FileName ȫ·���ļ���
    public byte[] ReadFile(String FileName){
        byte rbyts[]=null;
        try{
   ftpClient.binary();
   //System.out.println("00");
         TelnetInputStream tis = ftpClient.get(FileName);
         ByteArrayOutputStream baos=new ByteArrayOutputStream();
   //System.out.println("001");
         byte[] byts = new byte[1024];
         int len;
         while ((len = tis.read(byts)) != -1) {
    //System.out.println(len);
          for(int i = 0; i<len; i++){
           baos.write(byts[i]);
          }
          //baos.write(byts);
             //tos.write(bytes, 0, c);
         }
   //System.out.println("002");
         tis.close();
        
         rbyts=baos.toByteArray();
        }
        catch(Exception e){
         e.printStackTrace();
        }
       
        return rbyts;
    }
   
    // FTP��д�ļ�
 public void WriteFile(String FileName, byte[] byts) {
  try {
   ftpClient.binary();

   TelnetOutputStream tos = ftpClient.put(FileName);
   tos.write(byts);
   tos.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
    /**
     * �ϴ��ļ�
     * @param localFile �����ļ�
     * @param remoteFile Զ���ļ�
     */
    public void upload(String localFile, String remoteFile) {
        TelnetOutputStream os = null;
        FileInputStream is = null;
        try {
            //��Զ���ļ������������
            os = ftpClient.put(remoteFile);
            //��ȡ�����ļ���������
            File file_in = new File(localFile);
            is = new FileInputStream(file_in);
            //����һ�����
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            System.out.println("upload success");
        } catch (IOException ex) {
            System.out.println("not upload");
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally{
            try {
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(os != null){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
   
    /**
     * �����ļ�
     * @param remoteFile Զ���ļ�·��(�������)
     * @param localFile �����ļ�·��(�ͻ���)
     */
    public void download(String remoteFile, String localFile) {
        TelnetInputStream is = null;
        FileOutputStream os = null;
        try {
            //��ȡԶ�̻����ϵ��ļ�filename������TelnetInputStream�Ѹ��ļ����͵����ء�
            is = ftpClient.get(remoteFile);
            File file_in = new File(localFile);
            os = new FileOutputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            System.out.println("download success");
        } catch (IOException ex) {
            System.out.println("not download");
            ex.printStackTrace();
            throw new RuntimeException(ex);
        } finally{
            try {
                if(is != null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(os != null){
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void CloseConnect() {
        try {
         if(bConnected) ftpClient.closeServer();
            System.out.println(FtpIP + " FTP�Ͽ��ɹ�!");
        } catch (IOException ex) {
            System.out.println(FtpIP + " FTP�Ͽ�Pʧ��");
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  System.out.println("ϵͳ��!");

  FtpServer ftp1 = new FtpServer("192.168.0.101", 21, "anonymous", "IEUser@");
  byte[] byts = ftp1.ReadFile("/12358(v2.0).ppt");
  ftp1.CloseConnect();
  
  FtpServer ftp2 = new FtpServer("192.168.0.102", 21, "anonymous", "IEUser@");
  String path2 = "abc1/abc2/abc3";
  ftp2.CreateDirectory(path2);
  ftp2.WriteFile(path2 + "/" + "abc.ppt", byts);
  ftp2.CloseConnect();
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值