天天看点

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;

}

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;

}

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();

 }

}

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; 

 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; 

 }

 public boolean deletefile(string ftpdirandfilename)

 {

  if(!open())

  return false;

  ftpclient.sendserver("dele "+ftpdirandfilename+"\r\n");

  return true;

 }

 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)

  {

  }

 }

}

狗粮排行榜