apache FTP 实现问题解决

整合工具包,写FTP的实现,出现了问题,解决如下

apach自带的FTPExample的类

  1. /***略掉了包和 import的说明部分***/   
  2. 043     public  final  class  FTPExample  
  3. 044     {  
  4. 045       
  5. 046         public  static  final  String USAGE =  
  6. 047             "Usage: ftp  [-s] [-b] <hostname> <username> <password> <remote file> <local file>/n"  +  
  7. 048             "/nDefault behavior is to download a file and use ASCII transfer mode./n"  +  
  8. 049             "/t-s store file on server (upload)/n"  +  
  9. 050             "/t-b use binary transfer mode/n" ;  
  10. 051       
  11. 052         public  static  final  void  main(String[] args)  
  12. 053         {  
  13. 054             int  base = 0 ;  
  14. 055             boolean  storeFile  = false , binaryTransfer = false , error = false ;  
  15. 056             String server, username, password, remote, local;  
  16. 057             FTPClient ftp ;  
  17. 058       
  18. 059             for  (base = 0 ; base < args.length; base++)  
  19. 060             {  
  20. 061                 if  (args[base].startsWith("-s" ))  
  21. 062                     storeFile  = true ;  
  22. 063                 else  if  (args[base].startsWith("-b" ))  
  23. 064                     binaryTransfer = true ;  
  24. 065                 else   
  25. 066                     break ;  
  26. 067             }  
  27. 068       
  28. 069             if  ((args.length - base) != 5 )  
  29. 070             {  
  30. 071                 System.err.println(USAGE);  
  31. 072                 System.exit(1 );  
  32. 073             }  
  33. 074       
  34. 075             server = args[base++];  
  35. 076             username = args[base++];  
  36. 077             password = args[base++];  
  37. 078             remote = args[base++];  
  38. 079             local = args[base];  
  39. 080       
  40. 081             ftp  = new  FTPClient();  
  41. 082             ftp .addProtocolCommandListener(new  PrintCommandListener(  
  42. 083                                                new  PrintWriter(System.out)));  
  43. 084       
  44. 085             try   
  45. 086             {  
  46. 087                 int  reply;  
  47. 088                 ftp .connect(server);  
  48. 089                 System.out.println("Connected to "  + server + "." );  
  49. 090       
  50. 091                 // After connection attempt, you should check the reply code to verify   
  51. 092                 // success.   
  52. 093                 reply = ftp .getReplyCode();  
  53. 094       
  54. 095                 if  (!FTPReply.isPositiveCompletion(reply))  
  55. 096                 {  
  56. 097                     ftp .disconnect();  
  57. 098                     System.err.println("FTP  server refused connection." );  
  58. 099                     System.exit(1 );  
  59. 100                 }  
  60. 101             }  
  61. 102             catch  (IOException e)  
  62. 103             {  
  63. 104                 if  (ftp .isConnected())  
  64. 105                 {  
  65. 106                     try   
  66. 107                     {  
  67. 108                         ftp .disconnect();  
  68. 109                     }  
  69. 110                     catch  (IOException f)  
  70. 111                     {  
  71. 112                         // do nothing   
  72. 113                     }  
  73. 114                 }  
  74. 115                 System.err.println("Could not connect to server." );  
  75. 116                 e.printStackTrace();  
  76. 117                 System.exit(1 );  
  77. 118             }  
  78. 119       
  79. 120     __main:  
  80. 121             try   
  81. 122             {  
  82. 123                 if  (!ftp .login(username, password))  
  83. 124                 {  
  84. 125                     ftp .logout();  
  85. 126                     error = true ;  
  86. 127                     break  __main;  
  87. 128                 }  
  88. 129       
  89. 130                 System.out.println("Remote system is "  + ftp .getSystemName());  
  90. 131       
  91. 132                 if  (binaryTransfer)  
  92. 133                     ftp .setFileType(FTP .BINARY_FILE_TYPE);  
  93. 134       
  94. 135               // Use passive mode as default because most of us are   
  95. 136                 // behind firewalls these days.   
  96. 137                 ftp .enterLocalPassiveMode();  
  97. 138       
  98. 139                 if  (storeFile )  
  99. 140                 {  
  100. 141                     InputStream input;  
  101. 142       
  102. 143                     input = new  FileInputStream(local);  
  103. 144       
  104. 145                     ftp .storeFile (remote, input);  
  105. 146       
  106. 147                     input.close();  
  107. 148                 }  
  108. 149                 else   
  109. 150                 {  
  110. 151                     OutputStream output;  
  111. 152       
  112. 153                     output = new  FileOutputStream(local);  
  113. 154       
  114. 155                     ftp .retrieveFile(remote, output);  
  115. 156       
  116. 157                     output.close();  
  117. 158                 }  
  118. 159       
  119. 160                 ftp .logout();  
  120. 161             }  
  121. 162             catch  (FTPConnectionClosedException e)  
  122. 163             {  
  123. 164                 error = true ;  
  124. 165                 System.err.println("Server closed connection." );  
  125. 166                 e.printStackTrace();  
  126. 167             }  
  127. 168             catch  (IOException e)  
  128. 169             {  
  129. 170                 error = true ;  
  130. 171                 e.printStackTrace();  
  131. 172             }  
  132. 173             finally   
  133. 174             {  
  134. 175                 if  (ftp .isConnected())  
  135. 176                 {  
  136. 177                     try   
  137. 178                     {  
  138. 179                         ftp .disconnect();  
  139. 180                     }  
  140. 181                     catch  (IOException f)  
  141. 182                     {  
  142. 183                         // do nothing   
  143. 184                     }  
  144. 185                 }  
  145. 186             }  
  146. 187       
  147. 188             System.exit(error ? 1  : 0 );  
  148. 189         } // end main   
  149. 190       
  150. 191     } 

代码中第137行ftp .enterLocalPassiveMode();

然后我查了api相关的几个方法 enterLocalActiveMode,enterRemoteActiveMode,enterRemotePassiveMode。
我的理解大概是这样的
enterLocalPassiveMode:设置客户端PASV模式
static int PASSIVE_LOCAL_DATA_CONNECTION_MODE
enterLocalActiveMode:设置客户端PORT模式
static int ACTIVE_LOCAL_DATA_CONNECTION_MODE
enterRemoteActiveMode:server to server
static int ACTIVE_REMOTE_DATA_CONNECTION_MODE
requiring the one(client) connect to the other server's data port to initiate a data transfer.
enterRemotePassiveMode:server to server
static int PASSIVE_REMOTE_DATA_CONNECTION_MODE
requiring the other server to connect to the first server's data port to initiate a data transfer

FTP 协议了解的不太清楚是一个很大的原因,有时间要看看FTP 协议的内容了。

查了一些资料:
FTP 传输有两种模式:主 动模式(PORT)和被动模式(PASV)
主动模式:客户端主动连服务器端;端口用20
被动模式:服务器端连客户端;随机打开一个高端端口(端口号大于1024)

小提示有防火墙用户不能使用主动模式 ,这是因为防火墙不允许来自网外的主动连接,所以用户必须同使用被动模 式。

 

//enterLocalPassiveMode:设置客户端PASV模式 (客户端主动连服务器端;端口用20)

ftpClient.enterLocalPassiveMode();

//enterLocalActiveMode:设置客户端PORT模式  (服务器端连客户端;随机打开一个高端端口(端口号大于1024))
 ftpClient.enterLocalActiveMode();

 

 

 

 

package com.ftp.test.apache;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;

public class FtpTest {
    public static void main(String[] args) {
        //testUpload();
        testDownload();
    }

    /**
     * FTP上传单个文件测试
     */
    public static void testUpload() {
        FTPClient ftpClient = new FTPClient();
        FileInputStream fis = null;

        try {
            ftpClient.connect("服务器ip",端口);
            ftpClient.login("用户名", "密码");

            File srcFile = new File("路径+本地文件名");
            fis = new FileInputStream(srcFile);
           
            //enterLocalPassiveMode:设置客户端PASV模式 (客户端主动连服务器端;端口用20)
            //enterLocalActiveMode:设置客户端PORT模式  (服务器端连客户端;随机打开一个高端端口(端口号大于1024))
            ftpClient.enterLocalPassiveMode();
            //ftpClient.enterLocalActiveMode();
           
            //设置上传目录
            ftpClient.changeWorkingDirectory("/");
            ftpClient.setBufferSize(1024);
            ftpClient.setControlEncoding("GBK");
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.storeFile("远程文件名", fis);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fis);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP 连接发生异常!", e);
            }
        }
    }


    /**
     * FTP下载单个文件测试
     */
    public static void testDownload() {
        FTPClient ftpClient = new FTPClient();
        FileOutputStream fos = null;

        try {
            ftpClient.connect("服务器ip",端口);
            ftpClient.login("用户名", "密码");
           
            ftpClient.enterLocalPassiveMode();
           
            //远程文件名
            String remoteFileName = "/index.jsp";
           
            //本地文件
            fos = new FileOutputStream("e:/down.jsp");

            ftpClient.setBufferSize(1024);
            //设置文件类型(二进制)
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.retrieveFile(remoteFileName, fos);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错!", e);
        } finally {
            IOUtils.closeQuietly(fos);
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("关闭FTP 连接发生异常!", e);
            }
        }
    }

}

 

//sun

package com.ftp.test;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import sun.net.*;
import sun.net.ftp.FtpClient;

public class TestFtpClient {

    public FtpClient ftpClient = null;

    /**
     * connectServer
     * 连接ftp服务器
     * @throws java.io.IOException
     * @param path 文件夹,空代表根目录
     * @param password 密码
     * @param user   登陆用户
     * @param server 服务器地址
     */
    public void connectServer(String server, String user, String password,
            String path) throws IOException {
        // server:FTP 服务器的IP地址;user:登录FTP服务器的用户名
        // password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
        ftpClient = new FtpClient();
        ftpClient.openServer(server);
        ftpClient.login(user, password);
        //path 是ftp服务下主目录的子目录
        if (path.length() != 0)
            ftpClient.cd(path);
        //用2进制上传、下载
        ftpClient.binary();
    }

    /**
     * upload
     * 上传文件
     * @throws java.lang.Exception
     * @return -1 文件不存在
     *          -2 文件内容为空
     *          >0 成功上传,返回文件的大小
     * @param newname 上传后的新文件名
     * @param filename 上传的文件
     */
    public long upload(String filename, String newname) throws Exception {
        long result = 0;
        TelnetOutputStream os = null;
        FileInputStream is = null;
        try {
            java.io.File file_in = new java.io.File(filename);
            if (!file_in.exists())
                return -1;
            if (file_in.length() == 0)
                return -2;
           
            System.out.println(newname);
           
            os = ftpClient.put(newname);
           
            result = file_in.length();
           
            is = new FileInputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
        }
        return result;
    }

    /**
     * upload
     * @throws java.lang.Exception
     * @return
     * @param filename
     */
    public long upload(String filename) throws Exception {
        String newname = "";
        if (filename.indexOf("/") > -1) {
            newname = filename.substring(filename.lastIndexOf("/") + 1);
        } else {
            newname = filename;
        }
        return upload(filename, newname);
    }

    /**
     *  download
     *  从ftp下载文件到本地
     * @throws java.lang.Exception
     * @return
     * @param newfilename 本地生成的文件名
     * @param filename 服务器上的文件名
     */
    public long download(String filename, String newfilename) throws Exception {
        long result = 0;
        TelnetInputStream is = null;
        FileOutputStream os = null;
        try {
            is = ftpClient.get(filename);
            java.io.File outfile = new java.io.File(newfilename);
            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 (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
        }
        return result;
    }

    /**
     * 取得某个目录下的所有文件列表
     *
     */
    public List getFileList(String path) {
        List list = new ArrayList();
        try {
            DataInputStream dis = new DataInputStream(ftpClient.nameList(path));
            String filename = "";
            while ((filename = dis.readLine()) != null) {
                list.add(filename);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * closeServer
     * 断开与ftp服务器的链接
     * @throws java.io.IOException
     */
    public void closeServer() throws IOException {
        try {
            if (ftpClient != null) {
                ftpClient.closeServer();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        TestFtpClient ftp = new TestFtpClient();
        try {
            //连接ftp服务器
            ftp.connectServer("服务器", "用户名", "密码", "目录");
            /**  上传文件到 info2 文件夹下 */
            //System.out.println("filesize:"+ ftp.upload("e:/info.log") + "字节");
            /** 取得info2文件夹下的所有文件列表,并下载到 E盘下 */
           
            List list = ftp.getFileList("/htdocs");
           
            System.out.println(list.size());
           
            for (int i = 0; i < list.size(); i++) {
                String filename = (String) list.get(i);
                System.out.println(filename);
                ftp.download(filename, "E:/" + filename);
            }
        } catch (Exception e) {
            //e.printStackTrace();
        } finally {
            //很重要
            ftp.closeServer();
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值