简体   繁体   中英

getting Connection refused exception when trying to upload a file to the server using FTP

public class FileUpload {

 public static void main(String args[]){
  FTPClient client = new FTPClient();
     FileInputStream fis = null;
     try {
         client.connect("192.168.10.185");
         client.login("saranyas", "kspl1234");

         int reply = client.getReplyCode();
        if (!client.isConnected()) {
             System.out.println("FTP server refused connection." + reply);
             client.disconnect();
             System.exit(1);
         } else {
             System.out.println("FTP server connected." + reply);
         }
         // Create an InputStream for the file to be uploaded
         File f= new File("/home/Jyothisreea/Desktop/demo.doc");
         fis = new FileInputStream(f);
         // Store file to server
         client.storeFile(f.getName(), fis);
         client.logout();
     }  catch (IOException exp) {
         System.out.println(exp.getMessage());
     } finally {
         try {
             if (fis != null) {
                 fis.close();
             }
             client.disconnect();
         } catch (IOException exp) {
             System.out.println(exp.getMessage());
         }
     }
 }

}

This is my program when i am trying to run it iam getting the following exception

java.net.ConnectException: Connection refused
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
 at sun.net.NetworkClient.openServer(NetworkClient.java:118)
 at sun.net.ftp.FtpClient.openServer(FtpClient.java:488)
 at sun.net.ftp.FtpClient.openServer(FtpClient.java:475)
 at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:270)
 at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(FtpURLConnection.java:460)
 at components.SimpleFTPClient.uploadFile(SimpleFTPClient.java:83)
 at components.SimpleFTPClient.main(SimpleFTPClient.java:175

)

Why is this exception what should i do to overcome ....

Thanks

The normal cause of "connection refused" exceptions is that either:

  • no FTP service is currently running on the machine / port you are trying to use, or
  • a hardware or software firewall is blocking you from connecting.

If either of these is the case, then you won't be able to connect with a regular FTP client either.

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