简体   繁体   English

在Java中使用FTP程序

[英]using FTP program in java

I have been using a tutorial for listing directories from FTP server from here .But it is giving exception as java.net.SocketException: Software caused connection abort: socket write error The image is attached as well 我一直在使用教程从此处列出FTP服务器上的目录,但是它给出java.net.SocketException: Software caused connection abort: socket write error异常java.net.SocketException: Software caused connection abort: socket write error图像也被附加 在此处输入图片说明 I used ftp4j library and sample code and also got some error, Can anybody tell the reason as I cant see any reason behind this, May be I have to enable some security restriction for JVM? 我使用了ftp4j库和示例代码,并且也出现了一些错误,由于我看不到任何背后的原因,因此有人可以说出原因吗,可能是我必须为JVM启用一些安全性限制吗? (just a thought) Thank you in meekness (只是一个想法)谢谢你的温柔

Assuming that you tried to connect to this FTP server with any FTP client like FileZilla , here is the code to list files and directories based on Apache Commons FTP (define SERVER_NAME, USER_NAME and PASSWORD). 假设您尝试使用FileZilla类的任何FTP客户端连接到此FTP服务器,下面的代码将列出基于Apache Commons FTP(定义SERVER_NAME,USER_NAME和PASSWORD)的文件和目录。

        FTPClient ftp = new FTPClient();
        DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

        FTPClientConfig config = new FTPClientConfig();
        //optional - set timezone of the server 
        config.setServerTimeZoneId("America/New_York");

        ftp.configure(config );

        try {

            int reply;

            ftp.connect(SERVER_NAME);
            ftp.login(USER_NAME, PASSWORD);
            System.out.println("Connected to " + SERVER_NAME + ".");
            System.out.println(ftp.getReplyString());

            //After connection attempt, check the reply code to verify success.
            reply = ftp.getReplyCode();

            if(!FTPReply.isPositiveCompletion(reply)) {
              ftp.disconnect();
              System.out.println("Failed to connect to FTP server");
              System.exit(1);
            }

             //use binary mode
             ftp.setFileType(FTP.BINARY_FILE_TYPE);
             //use passive mode for firewalls
             ftp.enterLocalPassiveMode();

             FTPListParseEngine engine = ftp.initiateListParsing(".");

             while (engine.hasNext()) {
                FTPFile[] files = engine.getNext(25);  
                for (FTPFile file : files) {
                    String details = file.getName();
                    if (file.isDirectory()) {
                        details = "[" + details + "]";
                    }
                    details += "\t\t" + file.getSize();
                    details += "\t\t" + dateFormater.format(file.getTimestamp().getTime());
                    System.out.println(details);
                }

             }

          ftp.logout();

        } catch(IOException e) {
               e.printStackTrace();
        } finally {
          if(ftp.isConnected()) {
            try {
              ftp.disconnect();
            } catch(IOException ioe) {
              // do nothing
            }
          }
        }

Some points which are not handled in the tutorial: 本教程中未处理的一些要点:

  1. Passive mode to deal with firewalls 被动模式应对防火墙
  2. Pagination of FTP files FTP文件分页
  3. Checking replyCode 检查replyCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM