SimpleFtpClient


import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

public class SimpleFtpClient
{
 private FTPClient ftpClient;

 public void connectServer(String server, int port, String user,
   String password) throws SocketException, IOException
 {
  this.connectServer(server, port, user, password, null);
 }

 public void connectServer(String server, int port, String user,
   String password, String path) throws SocketException, IOException
 {
  ftpClient = new FTPClient();
  ftpClient.setControlEncoding("GBK");
  ftpClient.connect(server, port);
  // System.out.println("Connected to " + server + ":" + port);
  // System.out.println(ftpClient.getReplyCode());

  ftpClient.login(user, password);

  if (null != path && 0 != path.length())
  {
   ftpClient.changeWorkingDirectory(path);
   // System.out.println("wordDir:" + path);
  }

  // default bin
  ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
 }

 public void closeConnect() throws IOException
 {
  ftpClient.logout();

  if (ftpClient.isConnected())
  {
   ftpClient.disconnect();
  }
 }

 public boolean changeDirectory(String path) throws IOException
 {
  return ftpClient.changeWorkingDirectory(path);
 }

 public boolean deleteFile(String pathName) throws IOException
 {
  return ftpClient.deleteFile(pathName);
 }

 public boolean uploadFile(String filePathName, String newName)
   throws IOException
 {
  boolean flag = false;
  InputStream iStream = null;
  try
  {
   iStream = new FileInputStream(filePathName);
   flag = ftpClient.storeFile(newName, iStream);
  } catch (IOException e)
  {
   flag = false;
   return flag;
  } finally
  {
   if (iStream != null)
   {
    iStream.close();
   }
  }
  return flag;
 }

 public boolean uploadFile(InputStream iStream, String newName)
   throws IOException
 {
  boolean flag = false;
  try
  {
   flag = ftpClient.storeFile(newName, iStream);
  } catch (IOException e)
  {
   flag = false;
   return flag;
  } finally
  {
   if (iStream != null)
   {
    iStream.close();
   }
  }
  return flag;
 }

 public boolean download(String remoteFileName, String localFileName)
   throws IOException
 {
  boolean flag = false;
  File outfile = new File(localFileName);
  OutputStream oStream = null;
  try
  {
   oStream = new FileOutputStream(outfile);
   flag = ftpClient.retrieveFile(remoteFileName, oStream);
  } catch (IOException e)
  {
   flag = false;
   return flag;
  } finally
  {
   oStream.close();
  }
  return flag;
 }

 public InputStream downFile(String sourceFileName) throws IOException
 {
  return ftpClient.retrieveFileStream(sourceFileName);
 }

 public FTPClient getFtpClient()
 {
  return ftpClient;
 }

 /**
  * 是否不存在
  */
 public boolean isNotExist(String fileName) throws IOException
 {
  String[] listNames = ftpClient.listNames();
  for (int i = 0; i < listNames.length; i++)
  {
   if (listNames.equals(fileName))
   {
    return false;
   }
  }
  return true;
 }

 /**
  * 获得相类似的文件名
  */
 public String getSimlarFileName(String fileName) throws IOException
 {
  String[] listNames = ftpClient.listNames();
  for (int i = 0; i < listNames.length; i++)
  {
   if (listNames.indexOf(fileName) >= 0)
   {
    return listNames;
   }
  }
  return null;
 }

 /**
  * 打印当前工作目录
  */
 public String printWorkingDirectory() throws IOException
 {
  return ftpClient.printWorkingDirectory();
 }

 /**
  * 两个ftp之间传输
  */
 public static boolean transFromftpToftp(SimpleFtpClient srcSimpleFtpClient,
   SimpleFtpClient dstSimpleFtpClient, String srcFileSimpleName,
   String dstFileSimpleName)
 {
  if (null == srcSimpleFtpClient || null == dstSimpleFtpClient)
  {
   return false;
  }

  FTPClient srcFtpClient = srcSimpleFtpClient.getFtpClient();
  FTPClient dstFtpClient = dstSimpleFtpClient.getFtpClient();

  if (null == srcFtpClient || null == dstFtpClient)
  {
   return false;
  }

  boolean flag = false;
  InputStream retrieveFileStream = null;
  try
  {
   retrieveFileStream = srcFtpClient
     .retrieveFileStream(srcFileSimpleName);
   dstFtpClient.storeFile(dstFileSimpleName, retrieveFileStream);
   flag = true;
  } catch (IOException e)
  {
   flag = false;
   return flag;
  } finally
  {
   try
   {
    retrieveFileStream.close();
   } catch (IOException ignore)
   {
    // NO OP
   }
  }
  return flag;
 }
 
 /**
  * 重命名
  */
    public boolean reName( String oldName, String newName) throws IOException
    {
        boolean flag = false;
        try
        {
            flag = ftpClient.rename(oldName, newName);
        }
        catch (IOException e)
        {
            flag = false;
            return flag;
        }
        finally
        {
            
        }
        return flag;
    }
   
    // public static void main(String[] args)
    // throws SocketException, IOException
    // {
    // SimpleFtpClient client = new SimpleFtpClient();
    //
    // String server = "10.168.69.224";
    //       
    // client.connectServer(server, 21, "hmsftp", "hmsftp", "test/sub");
    //       
    // String localFileName = "localKeep.txt";
    // // client.download("wenzhou.mpg", "D://ftp//wenzhou.mpg");
    // // client.uploadFile("D://ftp//lin.ts","linxxx.ts");
    // client.deleteFile("toobig1.ts");
    //       
    // client.closeConnect();
    // }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值