sun.net.ftp.FtpClient上传,下载,移动文件,修改文件等等

public class ftpclientutil {
ftpclient ftpclient;
private string server;
private int port;
private string username;
private string userpassword;

public ftpclientutil(string server,int port,string username,string userpassword)
{
 this.server=server;
 this.port=port;
 this.username=username;
 this.userpassword=userpassword;
}
/**
 * 链接到服务器
 * @return
 */
public boolean open()
{
 if(ftpclient!=null&&ftpclient.serverisopen())
  return true;
 try
 {
     ftpclient= new ftpclient();
     ftpclient.openserver(server,port);
     ftpclient.login(username, userpassword);
     ftpclient.binary();
     return true;
 }
 catch(exception e)
 {
  e.printstacktrace();
  ftpclient=null;
  return false;
 }
}

public boolean cd(string dir){
 boolean f = false;
 try {
   ftpclient.cd (dir);
 } catch (ioexception e) {
  logs.error(e.tostring());
  return f;
 }
 return true;
}

/**
 * 上传文件到ftp服务器
 * @param localpathandfilename 本地文件目录和文件名
 * @param ftpfilename  上传后的文件名
 * @param ftpdirectory ftp目录如:/path1/pathb2/,如果目录不存在回自动创建目录
 * @throws exception
 */
public boolean upload(string localdirectoryandfilename,string ftpfilename,string ftpdirectory)throws exception {
 if(!open())
  return false;
 fileinputstream is=null;
 telnetoutputstream os=null;
 try
 {
  char ch = ' ';
  if (ftpdirectory.length() > 0)
   ch = ftpdirectory.charat(ftpdirectory.length() - 1);
  for (; ch == '/' || ch == '\\'; ch = ftpdirectory.charat(ftpdirectory.length() - 1))
   ftpdirectory = ftpdirectory.substring(0, ftpdirectory.length() - 1);

  int slashindex = ftpdirectory.indexof(47);
  int backslashindex = ftpdirectory.indexof(92);
  int index = slashindex;
  string dirall = ftpdirectory;
  if (backslashindex != -1 && (index == -1 || index > backslashindex))
   index = backslashindex;
  string directory = "";
  while (index != -1) {
   if (index > 0) {
    string dir = dirall.substring(0, index);
    directory = directory + "/" + dir;
    ftpclient.sendserver("xmkd " + directory + "\r\n");
    ftpclient.readserverresponse();
   }
   dirall = dirall.substring(index + 1);
   slashindex = dirall.indexof(47);
   backslashindex = dirall.indexof(92);
   index = slashindex;
   if (backslashindex != -1 && (index == -1 || index > backslashindex))
    index = backslashindex;
  }
  ftpclient.sendserver("xmkd " + ftpdirectory + "\r\n");
  ftpclient.readserverresponse();

  os = ftpclient.put(ftpdirectory + "/"
    + ftpfilename);
  file file_in = new file(localdirectoryandfilename);
  is = new fileinputstream(file_in);
  byte bytes[] = new byte[1024];
  int i;
  while ((i = is.read(bytes)) != -1)
   os.write(bytes, 0, i);
  //清理垃圾
  
   
  return true;
 }
 catch(exception e)
 {
  e.printstacktrace();
  return false;
 }
 finally
 {
  if (is != null) 
     is.close();
  if (os != null) 
     os.close();
 }
}
/**
 * 从ftp服务器上下载文件并返回下载文件长度
 * @param ftpdirectoryandfilename
 * @param localdirectoryandfilename
 * @return
 * @throws exception
 */
public long download(string ftpdirectoryandfilename,string localdirectoryandfilename)throws exception 
{
 long result = 0; 
 if(!open())
  return result;
 telnetinputstream is = null; 
 fileoutputstream os = null;
 try  
 { 
       is = ftpclient.get(ftpdirectoryandfilename);        
       java.io.file outfile = new java.io.file(localdirectoryandfilename); 
       os = new fileoutputstream(outfile); 
       byte[] bytes = new byte[1024]; 
       int c; 
       while ((c = is.read(bytes)) != -1) 
       { 
           os.write(bytes, 0, c); 
           result = result + c; 
       } 
    }
 catch (exception e)  
 { 
        throw e;
 } 
 finally 
 { 
     if (is != null) 
       is.close();
     if (os != null) 
       os.close();
     
  }
     return result; 

/**
 * 返回ftp目录下的文件列表
 * @param ftpdirectory
 * @return
 */
 public list<string> getfilenamelist(string ftpdirectory) 
 { 
    list<string> list = new arraylist<string>(); 
    if(!open())
  return list;
    try  
    { 
       datainputstream dis = new  datainputstream(ftpclient.namelist(ftpdirectory)); 
       string filename = ""; 
       while((filename=dis.readline())!=null)   
       {
         list.add(filename);         
       }   
    } catch (exception e)  
    { 
       e.printstacktrace(); 
    } 
    return list; 
 }
 /**
  * 删除ftp上的文件
  * @param ftpdirandfilename
  */
 public boolean deletefile(string ftpdirandfilename)
 {
  if(!open())
  return false;
  ftpclient.sendserver("dele "+ftpdirandfilename+"\r\n");
  return true;
 }
 /**
  * 删除ftp目录
  * @param ftpdirectory
  */
 public boolean deletedirectory(string ftpdirectory)
 {
  if(!open())
  return false;
  ftpclient.sendserver("xrmd "+ftpdirectory+"\r\n");
  return true;
 }
 /**
  * 关闭链接
  */
 public void close()
 {
  try
  {
      if(ftpclient!=null&&ftpclient.serverisopen())
       ftpclient.closeserver();
  }catch(exception e)
  {
   
  }
 }

}


狗粮排行榜

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值