简体   繁体   中英

java: getting Connection refused when trying to upload file to FTP

When trying to upload file to FTP with java program:

public void upload(String localFile,String remoteFile) throws Exception{
       ftp = new FTPClient(); 
       ftp.setControlKeepAliveTimeout(300);
       ftp.connect(host,21);    
       ftp.enterLocalPassiveMode();     
       ftp.setUseEPSVwithIPv4(false);   
       ftp.login(user,password);    
       ftp.setFileType(FTPClient.BINARY_FILE_TYPE);      

       FileInputStream in = null;
       in = new FileInputStream(localFile);
       ftp.storeFile(remoteFile,in);
       in.close();  
       ftp.disconnect();

}

I'm getting:

java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:381)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:243)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:230)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
    at java.net.Socket.connect(Socket.java:539)
    at org.apache.commons.net.SocketClient._connect(SocketClient.java:243)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:202)

When I try to upload the same file with command line (from linux), I'm able to do it only when using EPSV:

llnx:~ ftp anonymous@9.20.1.116
Connected to 9.20.1.116.
220 Microsoft FTP Service
331 Anonymous access allowed, send identity (e-mail name) as password.
Password:
230 User logged in.
Remote system type is Windows_NT.
ftp> epsv
EPSV/EPRT on IPv4 off.
ftp> put /tmp/file1.xml /dir_1/file1.xml
local: /tmp/file1.xml remote: /dir_1/file1.xml
227 Entering Passive Mode (10,40,1,149,233,168).
125 Data connection already open; Transfer starting.
100% |*************************************|   117 KB   28.66 MB/s    --:-- ETA
226 Transfer complete.
120032 bytes sent in 00:00 (7.96 MB/s)

So, Why does my java code getting Connection refused?

Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?

*** I think the answer is how to run the EPSV command from Java program.

Thank you all. Eithan.

This is purely a guess but java.net.ConnectException: Connection refused normally happens when there is nothing listening on the target host/port. You don't specify a port in the CLI example so maybe that is the problem. Try changing ftp.connect(host,21); to ftp.connect(host); to use the default. Also confirm the the hostnames are exactly the same.

This assumes that the error is on the call to connect() . You haven't provided a big enough stack trace to indicate either way.

Connection refused means that your TCP connection request has reached the remote server (or more correctly >>a<< remote server) but the server is not expecting / listening for an incoming connection. So it "refuses" it.

Here are the things to check:

  • Check that you have the correct remote hostname or IP address for the FTP server.
  • Check that you are using the correct port for the FTP server. Port 21 is the default, but it is possible that the server is on a non-standard port.
  • Check that the FTP server is actually running.

It is also possible that the problem is due to a firewall doing something deliberately confusing. But that is unlikely for a publicly routable FTP server.


Maybe I'm not using the enterLocalPassiveMode() or setUseEPSVwithIPv4() method the right way?

That can't be the problem. The stacktrace shows that your application failed while trying to establish the initial connection to the server. You haven't gotten to the point where the you can make those calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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