完成2次成功上传文件后,FTPClient将连接超时

时间:2012-11-02 14:06:52

标签: java ftp ftp-client

我正在使用FTPClient并通过Timer每隔30秒将文件发送到FTP服务器,有趣的是,上传2次成功后会出现连接超时错误,第三次不会发生错误,错误如下,多次使用不同的设置进行测试结果

java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:171)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:285)
at com.xxxxx.PDF2VS.send2FTP(PDF2VS.java:87)
at com.xxxxx.PDF2VS$1.run(PDF2VS.java:35)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)

这是我发现发送文件的代码;

   public static boolean send2FTP ()   
       {
          FTPClient client = new FTPClient();
          FileInputStream fis = null;

    try {
            //read from the 
            Properties prop = new Properties();
            InputStream in = PDF2VS.class.getResourceAsStream("/config.properties");

            prop.load(in);
            client.setControlKeepAliveTimeout(300);
            client.connect(prop.getProperty("FTP_ADDRESS"));
            client.login(prop.getProperty("UNAME"), prop.getProperty("PWD"));
            System.out.print("Message : " + client.getReplyString());

            client.setDefaultPort(Integer.parseInt(prop.getProperty("PORT")));
            int f1 = client.getDefaultPort();
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            System.out.println("File transfer port no  " + f1);
            System.out.println("FTP server reply ." + client.getReplyString());

            String localfile = prop.getProperty("LOCAL_FILE");
            fis = new FileInputStream(localfile);

            int lastSlash = localfile.lastIndexOf('/');
            String filename = localfile.substring(lastSlash+1);

            System.out.println("Uploading: " + filename);
            System.out.println("file : "+fis);

            client.setFileTransferMode(2);
            System.out.println("Flag reply ." + client.getReplyString());

            boolean flag = client.storeFile(filename,fis);

            System.out.println("Flag reply ." + client.getReplyString());

            if (flag) {
                System.out.println("Successfully uploaded the file");

            } else {
                System.out.println("Not able to upload the file");
            }

            fis.close();
            client.logout();
            System.out.println("Logout ." + client.getReplyString());

            in.close();
    }
    catch (Exception e) 
    {
       e.printStackTrace();
    }
    finally 
    {
        if (client.isConnected()) {
            try {
                client.disconnect();
                System.out.println("Server Disconnected." + client.getReplyString());
            } catch (IOException ioe) {
                 ioe.printStackTrace();
            }
        }
    }

return true; 
} 

它确实工作2次并收到错误,我做错了什么?

0 个答案:

没有答案