2014-12-03 221 views
0

我想在我的本地系統,通過FTPClient和測試,以複製文件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) 

iam在做什麼?

+0

您是否從以下接受的答案解決了問題?請回復,我也試圖在ftp.connect(String hostName,int portNumber)時出現同樣的異常......... – 2016-06-21 10:05:36

+0

是的它已解決 – Venkatesh 2016-06-22 06:32:32

+0

如何?如答案中所述,請解釋 – 2016-06-23 04:13:07

回答

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) 

這個錯誤是因爲錯誤的憑據(或)服務器未啓動。 重新啓動服務器後,文件被傳輸:)