java FTP传文件 .

来源:互联网 发布:小米手机数据恢复软件 编辑:程序博客网 时间:2024/06/03 17:11
 

sun.net.ftp.FtpClient.,该类库主要提供了用于建立FTP连接的类。利用这些类的方法,编程人员可以远程登录到FTP服务器,列举该服务器上的目录,设置传输协议,以及传送文件。FtpClient类涵盖了几乎所有FTP的功能,FtpClient的实例变量保存了有关建立"代理"的各种信息。下面给出了这些实例变量。

  public static boolean useFtpProxy

  这个变量用于表明FTP传输过程中是否使用了一个代理,因此,它实际上是一个标记,此标记若为TRUE,表明使用了一个代理主机。

  public static String ftpProxyHost

  此变量只有在变量useFtpProxy为TRUE时才有效,用于保存代理主机名。

  public static int ftpProxyPort

  此变量只有在变量useFtpProxy为TRUE时才有效,用于保存代理主机的端口地址。

  FtpClient有三种不同形式的构造函数,如下所示:

  1、public FtpClient(String hostname,int port)

   此构造函数利用给出的主机名和端口号建立一条FTP连接。

  2、public FtpClient(String hostname)

  此构造函数利用给出的主机名建立一条FTP连接,使用默认端口号。

  3、FtpClient()

  此构造函数将创建一FtpClient类,但不建立FTP连接。这时,FTP连接可以用openServer方法建立。

  一旦建立了类FtpClient,就可以用这个类的方法来打开与FTP服务器的连接。类FtpClient提供了如下两个可用于打开与FTP服务器之间的连接的方法。

  public void openServer(String hostname)

  这个方法用于建立一条与指定主机上的FTP服务器的连接,使用默认端口号。

  public void openServer(String host,int port)

  这个方法用于建立一条与指定主机、指定端口上的FTP服务器的连接。

  打开连接之后,接下来的工作是注册到FTP服务器。这时需要利用下面的方法。

  public void login(String username,String password)

  此方法利用参数username和password登录到FTP服务器。使用过Intemet的用户应该知道,匿名FTP服务器的登录用户名为anonymous,密码一般用自己的电子邮件地址。

  下面是FtpClient类所提供的一些控制命令。

  public void cd(String remoteDirectory)

  该命令用于把远程系统上的目录切换到参数remoteDirectory所指定的目录。

  public void cdUp():该命令用于把远程系统上的目录切换到上一级目录。

  public String pwd():该命令可显示远程系统上的目录状态。

  public void binary():该命令可把传输格式设置为二进制格式。

  public void ascii():该命令可把传输协议设置为ASCII码格式。

  public void rename(String string,String string1)

  该命令可对远程系统上的目录或者文件进行重命名操作。

  除了上述方法外,类FtpClient还提供了可用于传递并检索目录清单和文件的若干方法。这些方法返回的是可供读或写的输入、输出流。下面是其中一些主要的方法。

  public TelnetInputStream list()

  返回与远程机器上当前目录相对应的输入流。

  public TelnetInputStream get(String filename)

  获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。

  public TelnetOutputStream put(String filename)

  以写方式打开一输出流,通过这一输出流把文件filename传送到远程计算机。

 

再来Ftp的实际操作类:

package org.javawo.components.ftp;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

/**
*
* FTP消费类,提供一系列的FTP操作方法
*
* @author 杨涛
*
*/
public class FtpUtil {

/** 登录用户名,密码,IP等参数*/
private String name,password,ip;

/** 登录端口*/
private int port;

/** FTP操作对象*/
private FTPClient ftpClient = null;

/** 构造方法,初始化传入要连接的FTP参数信息*/
public FtpUtil(FtpInfo ftpInfo) throws Exception {
   if(ftpInfo==null) {
    /** 判断如果为null 就抛出初始化异常*/
    throw new Exception("传递的FTP连接参数对象是null");
   } else {
    this.init(ftpInfo);
   }
}

/**
* 下载文件到本地
*
* @param remoteFileName
* @param localFileName
*/
public void loadFile(String remoteFileName, String localFileName) {
   BufferedOutputStream buffOut = null;
   try {
    buffOut = new BufferedOutputStream(new FileOutputStream(localFileName));
    /** 写入本地文件*/
    ftpClient.retrieveFile(remoteFileName, buffOut);
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    try {
     if (buffOut != null)
      buffOut.close();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
}

/**
* 上传文件到FTP服务器上
*
* @param localFileStream
* @param fileName
* @return
* @throws IOException
*/
public boolean uploadFile(InputStream localFileStream, String fileName) throws IOException {
   /** 返回参数*/
   boolean fla = false;
   try{
    /** 设置文件类型*/
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    /** 上传之前先给文件命明为.TMP*/
    String tempName = fileName+".tmp";
    /** 开始上传文件*/
    ftpClient.storeFile(tempName, localFileStream);
    /** 上传完毕之后再该为原名*/
    renameFile(tempName,fileName);
    fla = true;
   }catch(Exception ex){
    ex.printStackTrace();
   }finally {
    localFileStream.close();
   }
   return fla;
}

/**
* 下载文件流
*
* @param remoteFileName
* @param output
* @return
*/
public boolean downloadFile(String remoteFileName, OutputStream output) throws IOException{
   try {    
    if ((remoteFileName!=null)&&(output!=null)) {
     ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
     boolean ret = ftpClient.retrieveFile(remoteFileName, output);
     return ret;
    } else {
     return false;
    }
   }catch (IOException e) {
    if (e.getMessage()=="IOException caught while copying." || output==null) {    
     ftpClient.disconnect();
    }
    return false;
   }
}

/**
* 重命名FTP上的文件
*
* @param oldName
* @param newName
*/
public void renameFile(String oldName,String newName) {
   try{
    /** 重命名*/
    ftpClient.rename(oldName, newName);
   }catch(Exception ex){
    ex.printStackTrace();
   }
}

/**
* 获取服务器上的文件大小
*
* @param fileName
* @return
*/
public long getFileSize(String fileName) {
   try {
    StringBuffer command = new StringBuffer("size ");
    command.append(fileName);
    ftpClient.sendCommand(command.toString());
    if (ftpClient.getReplyCode()==213)
    {
     String replyText=ftpClient.getReplyString().substring(4).trim();
     return Long.parseLong(replyText);
    }else{
     return 0;
    }
   } catch (Throwable e) {
    e.printStackTrace();
    return 0;
   }
}

/**
* 初始化FTP连接参数
*
* @param ftpInfo
*/
public void init(FtpInfo ftpInfo) {
   this.name = ftpInfo.getName();
   this.password = ftpInfo.getPassword();
   this.ip = ftpInfo.getIp();
   this.port = ftpInfo.getPort();
}

/**
* 连接到FTP服务器
*
*/
public void connectServer() {
   /** 如果当前ftpClient对象是null且没有连接就连接到FTP服务器*/
   if(this.ftpClient==null && !this.ftpClient.isConnected()) {
    try {
     /** 如果ftpClient对象为null就实例化一个新的*/
     this.ftpClient = new FTPClient();
     this.ftpClient.setDefaultPort(this.port); /** 设置默认的IP地址*/
     this.ftpClient.connect(this.ip);/** 连接到FTP服务器*/
     /** 登录到这台FTP服务器*/
     if(ftpClient.login(this.name, this.password)){
      System.out.println("在类FtpUtil中: 服务器"+this.ip+"的用户"+this.name+"登录成功!");
     }
    }catch(Exception ex){
     ex.printStackTrace();
    }
   }
}

/**
* 销毁FTP服务器连接
*
*/
public void closeConnect() {
   /** 判断当前ftpClient对象不为null和FTP已经被连接就关闭*/
   if (this.ftpClient != null && this.ftpClient.isConnected()) {
    try{
     this.ftpClient.disconnect();/** 销毁FTP连接*/
    }catch(Exception ex){
     ex.printStackTrace();
    }
   }
}
}

在来Ftp的Map连接池

package org.javawo.components.ftp;

import java.util.HashMap;

/**
* 定义一个HashMap,装载FTP连接的实例化对象,这里做FTP的连接池使用
*
* @author 杨涛
*
*/
public class FtpConnectMap {

/** K-V对应的连接池*/
@SuppressWarnings("unchecked")
private static HashMap connectMap = new HashMap();

private static FtpConnectMap ftpMap = null;

/**
* 获得FtpConnectMap的实例化对象
*
* @return
*/
public static FtpConnectMap getInstance() {
   if(ftpMap==null){
    ftpMap = new FtpConnectMap();
   }
   return ftpMap;
}

/**
* 通过Name获取存在HashMap中的FTP连接对象
*
* @param name
* @return
*/
public FtpUtil getFtpUtil(String name) {
   FtpUtil ftpUtil = (FtpUtil)connectMap.get(name);
   return ftpUtil;
}

/**
* 给HashMap添加一个新的FTP连接对象
*
* @param name
* @param ftpUtil
*/
@SuppressWarnings("unchecked")
public void addFtpUtil(String name,FtpUtil ftpUtil) {
   connectMap.put(name, ftpUtil);
}

}

最后再来一个是Ftp的创建工厂

package org.javawo.components.ftp;

/**
* FTP操作工厂类,负责FTP的实例创建和其他
*
* @author 杨涛
*
*/
public class FtpFactory {

/**
* 创建一个FtpUtil的实例化对象
*
* @param ftpInfo
* @return
*/
public FtpUtil createFtpUtil(FtpInfo ftpInfo){
   /** 获取存储对象的实例*/
   FtpConnectMap ftpMap = FtpConnectMap.getInstance();
   /** 从HashMap中获取一个连接的实例化对象*/
   FtpUtil ftpUtil = ftpMap.getFtpUtil(ftpInfo.getName());
   if(ftpUtil==null) {
    try{
     /** 该连接不存在于HashMap中,实例化一个连接将此连接添加到HashMap中*/
     ftpUtil = new FtpUtil(ftpInfo);/** 实例化一个FTP连接*/
     ftpUtil.connectServer();
     ftpMap.addFtpUtil(ftpInfo.getName(),ftpUtil);
    }catch(Exception ex){
     ex.printStackTrace();
    }
   }
   return ftpUtil;
}

}


 

 

例子:

 


import  sun.net.ftp. * ;
import  sun.net. * ;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.ByteArrayOutputStream;
import  java.util.ArrayList;
import  java.util.StringTokenizer;

/** */ /** 
   FTP远程命令列表<br>
USER    PORT    RETR    ALLO    DELE    SITE    XMKD    CDUP    FEAT<br>
PASS    PASV    STOR    REST    CWD     STAT    RMD     XCUP    OPTS<br>
ACCT    TYPE    APPE    RNFR    XCWD    HELP    XRMD    STOU    AUTH<br>
REIN    STRU    SMNT    RNTO    LIST    NOOP    PWD     SIZE    PBSZ<br>
QUIT    MODE    SYST    ABOR    NLST    MKD     XPWD    MDTM    PROT<br>
    在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上 <br>
         ftpclient.sendServer("XMKD /test/bb "); //执行服务器上的FTP命令<br>
         ftpclient.readServerResponse一定要在sendServer后调用<br>
         nameList("/test")获取指目录下的文件列表<br>
         XMKD建立目录,当目录存在的情况下再次创建目录时报错<br>
         XRMD删除目录<br>
         DELE删除文件<br>
* <p>Title: 使用JAVA操作FTP服务器(FTP客户端)</p>
* <p>Description: 上传文件的类型及文件大小都放到调用此类的方法中去检测,比如放到前台JAVASCRIPT中去检测等
* 针对FTP中的所有调用使用到文件名的地方请使用完整的路径名(绝对路径开始)。
* </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: 静靖工作室</p>
*  
@author  欧朝敬  13873195792
*  
@version  1.0
*/ 

 
public   class  FTPConnectorBK   {
    
private  FtpClient ftpclient;
    
private  String ipAddress;
    
private   int  ipPort;
    
private  String userName;
    
private  String PassWord;
    
/** */ /** 
    * 构造函数
    *  
@param  ip String 机器IP
    *  
@param  port String 机器FTP端口号
    *  
@param  username String FTP用户名
    *  
@param  password String FTP密码
    *  
@throws  Exception
     
*/ 
    
public  FTPConnectorBK(String ip,  int  port, String username, String password)  throws 
            Exception   {
        
       ipAddress  
=   new  String(ip);
       ipPort  
=  port;
//       System.out.println("1......"+ftpclient.serverIsOpen());
       ftpclient  =   new  FtpClient(ipAddress, ipPort);
//       ftpclient  =   new  FtpClient();
       System.out.println("2......"+ftpclient.serverIsOpen());
        
// ftpclient = new FtpClient(ipAddress); 
        userName  =   new  String(username);
       PassWord  
=   new  String(password);
   } 

     
/** */ /** 
    * 构造函数
    *  
@param  ip String 机器IP,默认端口为21
    *  
@param  username String FTP用户名
    *  
@param  password String FTP密码
    *  
@throws  Exception
     
*/ 
    
public  FTPConnectorBK(String ip, String username, String password)  throws 
            Exception   {
       ipAddress  
=   new  String(ip);
       ipPort  
=   21 ;
       ftpclient  
=   new  FtpClient(ipAddress, ipPort);
        
// ftpclient = new FtpClient(ipAddress); 
        userName  =   new  String(username);
       PassWord  
=   new  String(password);
   } 


     
/** */
    
/**
     * 登录FTP服务器
     * 
     * 
@throws Exception
     
*/
    
public void login() throws Exception {
        System.out.println(
"b......"+ftpclient.welcomeMsg);        
        ftpclient.login(userName, PassWord);
        ftpclient.setConnectTimeout(
30);
        System.out.println(
"getReadTimeout..."+ftpclient.getReadTimeout());        
        System.out.println(
"getConnectTimeout..."+ftpclient.getConnectTimeout());
        System.out.println(
"e......"+ftpclient.welcomeMsg);
    } 

     
/** */ /**
             * 退出FTP服务器
             * 
             * 
@throws Exception
             
*/ 
     
public   void  logout()  throws  Exception   {
        
// 用ftpclient.closeServer()断开FTP出错时用下更语句退出 
         System.out.println("10......"+ftpclient.serverIsOpen());
        ftpclient.sendServer( 
" QUIT  " );
        
int  reply  =  ftpclient.readServerResponse();  // 取得服务器的返回信息 
        System.out.println("reply...."+reply);
        System.out.println(
"11......"+ftpclient.serverIsOpen());
        ftpclient.closeServer();
        System.out.println(
"12......"+ftpclient.serverIsOpen());
        
    } 

     
/** */ /** 
    * 在FTP服务器上建立指定的目录,当目录已经存在的情下不会影响目录下的文件,这样用以判断FTP
    * 上传文件时保证目录的存在目录格式必须以"/"根目录开头
    *  
@param  pathList String
    *  
@throws  Exception
     
*/ 
     
public   void  buildList(String pathList)  throws  Exception   {
       ftpclient.ascii();
       StringTokenizer s  
=   new  StringTokenizer(pathList,  "/" );  // sign 
         int  count  =  s.countTokens();
       String pathName  
=   "" ;
        
while  (s.hasMoreElements())   {
           pathName  
=  pathName  +   "/"   +  (String) s.nextElement();
            
try    {
               ftpclient.sendServer( 
"XMKD"   +  pathName  +   " " );
           }   
catch  (Exception e)   {
               e  
=   null ;
           } 
            
int  reply  =  ftpclient.readServerResponse();
            System.out.println(reply);
       } 
       ftpclient.binary();
   } 

     
/** */ /** 
    * 取得指定目录下的所有文件名,不包括目录名称
    * 分析nameList得到的输入流中的数,得到指定目录下的所有文件名
    *  
@param  fullPath String
    *  
@return  ArrayList
    *  
@throws  Exception
     
*/ 
     
public  ArrayList fileNames(String fullPath)  throws  Exception   {
       ftpclient.ascii();  
// 注意,使用字符模式 
       ftpclient.cd("/freep");
//       ftpclient.cdUp();
//        TelnetInputStream list  =  ftpclient.nameList(fullPath);
       TelnetInputStream list  =  ftpclient.list();

        
byte [] names  =   new   byte [ 2048 ];
        
int  bufsize  =   0 ;
       bufsize  
=  list.read(names,  0 , names.length);  // 从流中读取 
        list.close();
       ArrayList namesList  
=   new  ArrayList();
        
int  i  =   0 ;
        
int  j  =   0 ;
        
while  (i  <  bufsize  /**/ /* names.length */ )   {
//            System.out.println(i);
            
// char bc = (char) names;
            
// System.out.println(i + "  " + bc + " : " + (int) names);
            
// i = i + 1; 
              if  (names[i]  ==   10 )   {  // 字符模式为10,二进制模式为13
                  System.out.println("j...."+j);
                
// 文件名在数据中开始下标为j,i-j为文件名的长度,文件名在数据中的结束下标为i-1
                
// System.out.write(names, j, i - j);
                
// System.out.println(j + "   " + i + "    " + (i - j)); 
                String tempName  =   new  String(names, j, i  -  j);
               namesList.add(tempName);
                 System.out.println(tempName);
                
//  处理代码处
                
// j = i + 2;  // 上一次位置二进制模式 
                j  =  i  +   1 ;  // 上一次位置字符模式 
            } 
           i  
=  i  +   1 ;
       } 
        
return  namesList;
   } 

     
/** */ /** 
    * 上传文件到FTP服务器,destination路径以FTP服务器的"/"开始,带文件名、
    * 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
    *  
@param  source String
    *  
@param  destination String
    *  
@throws  Exception
     
*/ 
     
public   void  upFile(String source, String destination)  throws  Exception   {
       buildList(destination.substring( 
0 , destination.lastIndexOf( "/" )));
       ftpclient.binary();  
// 此行代码必须放在buildList之后 
        TelnetOutputStream ftpOut  =  ftpclient.put(destination);
       TelnetInputStream ftpIn  
=   new  TelnetInputStream( new 
               FileInputStream(source),  
true );
        
byte [] buf  =   new   byte [ 204800 ];
        
int  bufsize  =   0 ;
        
while  ((bufsize  =  ftpIn.read(buf,  0 , buf.length))  !=   - 1 )   {
           ftpOut.write(buf,  
0 , bufsize);
       } 
       ftpIn.close();
       ftpOut.close();

   } 


     
/** */ /** 
    * JSP中的流上传到FTP服务器,
    * 上传文件只能使用二进制模式,当文件存在时再次上传则会覆盖
    * 字节数组做为文件的输入流,此方法适用于JSP中通过
    * request输入流来直接上传文件在RequestUpload类中调用了此方法,
    * destination路径以FTP服务器的"/"开始,带文件名
    *  
@param  sourceData byte[]
    *  
@param  destination String
    *  
@throws  Exception
     
*/ 
     
public   void  upFile( byte [] sourceData, String destination)  throws  Exception   {
       buildList(destination.substring( 
0 , destination.lastIndexOf( " / " )));
       ftpclient.binary();  
// 此行代码必须放在buildList之后 
        TelnetOutputStream ftpOut  =  ftpclient.put(destination);
       ftpOut.write(sourceData,  
0 , sourceData.length);
//         ftpOut.flush(); 
        ftpOut.close();
   } 

     
/** */ /** 
    * 从FTP文件服务器上下载文件SourceFileName,到本地destinationFileName
    * 所有的文件名中都要求包括完整的路径名在内
    *  
@param  SourceFileName String
    *  
@param  destinationFileName String
    *  
@throws  Exception
     
*/ 
    
public   void  downFile(String SourceFileName, String destinationFileName)  throws 
            Exception   {
       ftpclient.binary();  
// 一定要使用二进制模式 
        TelnetInputStream ftpIn  =  ftpclient.get(SourceFileName);
        
byte [] buf  =   new   byte [ 204800 ];
        
int  bufsize  =   0 ;
       FileOutputStream ftpOut  
=   new  FileOutputStream(destinationFileName);
        
while  ((bufsize  =  ftpIn.read(buf,  0 , buf.length))  !=   - 1 )   {
           ftpOut.write(buf,  
0 , bufsize);
       } 
       ftpOut.close();
       ftpIn.close();
   } 

     
/** */ /** 
    *从FTP文件服务器上下载文件,输出到字节数组中
    *  
@param  SourceFileName String
    *  
@return  byte[]
    *  
@throws  Exception
     
*/ 
    
public   byte [] downFile(String SourceFileName)  throws 
            Exception   {
       ftpclient.binary();  
// 一定要使用二进制模式 
        TelnetInputStream ftpIn  =  ftpclient.get(SourceFileName);
       ByteArrayOutputStream byteOut  
=   new  ByteArrayOutputStream();
        
byte [] buf  =   new   byte [ 204800 ];
        
int  bufsize  =   0 ;

        
while  ((bufsize  =  ftpIn.read(buf,  0 , buf.length))  !=   - 1 )   {
           byteOut.write(buf,  
0 , bufsize);
       } 
        
byte [] return_arraybyte  =  byteOut.toByteArray();
       byteOut.close();
       ftpIn.close();
        
return  return_arraybyte;
   } 

     
/** */ /** 调用示例
    * FtpUpfile fUp = new FtpUpfile("192.150.189.22", 21, "admin", "admin");
    * fUp.login();
    * fUp.buildList("/adfadsg/sfsdfd/cc");
    * String destination = "/test.zip";
    * fUp.upFile("C:/Documents and Settings/Administrator/My Documents/sample.zip",destination);
    * ArrayList filename = fUp.fileNames("/");
    * for (int i = 0; i < filename.size(); i++) {
    *     System.out.println(filename.get(i).toString());
    * }
    * fUp.logout();
    *  
@param  args String[]
    *  
@throws  Exception
     
*/ 
     
public   static   void  main(String[] args)  throws  Exception   {
//         FTPConnectorBK fUp  =   new  FTPConnectorBK( "192.168.1.230" ,  21 ,  "admin" ,  "sanco_freep" );
         FTPConnectorBK fUp  =   new  FTPConnectorBK( "192.168.1.160" ,  21 ,  "ftptest" ,  "ftptest" );
         System.out.println(
"fUp......"+fUp.ftpclient.serverIsOpen());
         fUp.login();
         
         System.out.println(
"login()......"+fUp.ftpclient.serverIsOpen());
//         fUp.fileNames("/freep");
         System.out.println("1......begin");
       fUp.upFile( 
"f:/小游戏.rar" ,"/1.rar" );
       System.out.println(
"1......end");
       System.out.println(
"2......begin");
       fUp.upFile( 
"f:/小游戏.rar" ,"/2.rar" );
       System.out.println(
"2......end");
       System.out.println(
"3......begin");
       fUp.upFile( 
"f:/小游戏.rar" ,"/小游戏.rar" );
       System.out.println(
"3......end");
       String ss
=fUp.ftpclient.pwd();
       System.out.println(
"ss......"+ss);
         System.out.println(
"option......"+fUp.ftpclient.serverIsOpen());
//         fUp.deleteFile();
         
//建立文件目录
//                 fUp.buildList("/adfadsg/sfsdfd/cc");
                 
//               String destination = "/test/SetupDJ.rar";
//               fUp.upFile(
//        "C:/Documents and Settings/Administrator/My Documents/SetupDJ.rar",
//                       destination);
//               ArrayList filename = fUp.fileNames("/");
//               for (int i = 0; i < filename.size(); i++) {
//                   System.out.println(filename.get(i).toString());
//               }
//
//               fUp.downFile("/sample.zip", "d:/sample.zip");
         
                 
//文件上传
//       FileInputStream fin  =   new  FileInputStream(
//                "d://37.jpg" );
//        byte [] data  =   new   byte [ 20480000 ];
//       fin.read(data,  0 , data.length);

       fUp.logout();
       System.out.println(
"fUp.logout()......"+fUp.ftpclient.serverIsOpen());
       System.out.println( 
" 程序运行完成! " );
        
/**/ /* FTP远程命令列表
        USER    PORT    RETR    ALLO    DELE    SITE    XMKD    CDUP    FEAT
        PASS    PASV    STOR    REST    CWD     STAT    RMD     XCUP    OPTS
        ACCT    TYPE    APPE    RNFR    XCWD    HELP    XRMD    STOU    AUTH
        REIN    STRU    SMNT    RNTO    LIST    NOOP    PWD     SIZE    PBSZ
        QUIT    MODE    SYST    ABOR    NLST    MKD     XPWD    MDTM    PROT
         
*/ 
         
/**/ /* 在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上
        ftpclient.sendServer("XMKD /test/bb "); //执行服务器上的FTP命令
        ftpclient.readServerResponse一定要在sendServer后调用
        nameList("/test")获取指目录下的文件列表
        XMKD建立目录,当目录存在的情况下再次创建目录时报错
        XRMD删除目录
        DELE删除文件
         
*/ 
   } 
     
     
public void deleteFile()throws Exception {
         ftpclient.ascii();
         ftpclient.sendServer( 
"DELE "   +  "/freep/jpg/37.jpg"  +   " " );
         
int  reply  =  ftpclient.readServerResponse();
         System.out.println(reply);
     }

 

以上的代码是我在网上搜索到的,我也有2个疑问:

1、ftpclient.setConnectTimeout(int   arg);   里面的参数应该是时间吧,但是这个时间的单位是什么呢?秒?还是毫秒?还是其他的什么?怎么我将arg的值分别设为   2     60       5000     然后每次都等了10分钟都没什么效果啊?
2、除非调用了   ftpclient.closeServer()方法   否则ftpclient.serverIsOpen()   恒为true,那么我怎么才能判断链接状态呢?   

后来我发现链接超时是由服务器端设定的,FTP服务器会在设定的时间以外自动将链接断开,这样我的程序就不好控制了。在实际项目当中,如果这个FTP上传使用的很频繁的话,要不停的打开连接和关闭连接,如果使用的不是很平凡的话,可能服务器端已经因为连接超时而把连接断开了而程序也无法判断(当然也要注意线程安全问题)。

后来我的做法是在第一次链接服务器的时候设置一个"连接超时的时间点"(即当前时间+FTP服务器端设置的连接超时时间=与FTP服务器断开连接的时间),然后在每次上传的时候根据"连接超时的时间点" 判断是否已经和FTP服务器端断开连接,如果是则重新连接后在上传,否则直接上传,最后重新设置“连接超时的时间点”。

 

原创粉丝点击