FTPClient连接被拒绝

时间:2014-12-03 11:17:53

标签: java ftp-client apache-commons-net

我正在尝试通过FTPClient复制文件并在我的本地系统中进行测试

我的代码是这样的,我的IPv4地址作为主机的输入

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

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

public class FTPUploader {

FTPClient ftp = null;

public FTPUploader(String host, String user, String pwd) throws Exception{
    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    int reply;
    ftp.connect(host);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftp.login(user, pwd);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();
}
public void uploadFile(String localFileFullName, String fileName, String hostDir)
        throws Exception {
    try(InputStream input = new FileInputStream(new File(localFileFullName))){
    this.ftp.storeFile(hostDir + fileName, input);
    }
}

public void disconnect(){
    if (this.ftp.isConnected()) {
        try {
            this.ftp.logout();
            this.ftp.disconnect();
        } catch (IOException f) {
             // DO NOTHING
        }
    }
}
public static void main(String[] args) {
   try{
       System.out.println("Start");
       FTPUploader ftpUploader = new FTPUploader("10.66.***.***", "username", "password");

                  ftpUploader.uploadFile("D:/Venkatesh.pptx", "65.pptx", "C:/Users/VENKATESH/Desktop");
       ftpUploader.disconnect();
       System.out.println("Done");
   }catch(Exception exception){
       exception.printStackTrace();
   }
}

}

现在,我得到以下例外

Start
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:168)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:189)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:278)
      at fileTransfer.FTPUploader.<init>(FTPUploader.java:21)
at fileTransfer.FTPUploader.main(FTPUploader.java:51)

我在做什么错误????

1 个答案:

答案 0 :(得分:2)

该计划绝对没问题。 问题出在服务器端。

这里的例外是:

java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)

错误是因为凭据错误(或)服务器未启动。 重新启动服务器后传输文件:)