尝试使用FTP将文件上载到服务器时获取Connection拒绝异常

时间:2010-12-26 15:02:22

标签: java

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());
         }
     }
 }

}

这是我的程序,当我尝试运行它时,我得到以下异常

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

为什么这个例外该怎么做才能克服......

由于

1 个答案:

答案 0 :(得分:2)

“连接拒绝”例外的正常原因是:

  • 您尝试使用的计算机/端口上当前没有运行FTP服务,或
  • 硬件或软件防火墙阻止您连接。

如果是这种情况之一,那么您将无法连接常规FTP客户端。