2016-04-28 113 views
0
String ftpUrl = "ftp://%s:%[email protected]%s/%s;type=i";  
String host = "10.88.195.43:22";  
String user = "ionadmin";  
String pass = "ionadmin";  
String filePath = "D:\\JARS\\beforeRunSanity.txt";  
String uploadPath = "/home/ionadmin/";  
ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);  
System.out.println("Upload URL: " + ftpUrl);  
try {  
    URL url = new URL(ftpUrl);  
    URLConnection conn = url.openConnection();  
    OutputStream outputStream = conn.getOutputStream();  
    FileInputStream inputStream = new FileInputStream(filePath);  
    byte[] buffer = new byte[BUFFER_SIZE];  
    int bytesRead = -1;  
    while ((bytesRead = inputStream.read(buffer)) != -1) {  
     outputStream.write(buffer, 0, bytesRead);  
    }  
    inputStream.close();  
    outputStream.close();  
    System.out.println("File uploaded");  
} catch (IOException ex) {  
    ex.printStackTrace();  
}  

我已經提供了所有的有效憑據,但我得到異常時:「FtpProtocolException:歡迎信息...」連接到Ubuntu的服務器使用Java

Exception in thread "main" java.io.IOException:  sun.net.ftp.FtpProtocolException: Welcome message: SSH-2.0-OpenSSH_6.6p1 Ubuntu-2ubuntu1  
    at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source)  
    at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(Unknown  Source)  

如果我刪除的端口號,其顯示我ftpConnection例外:無效的用戶名/密碼

+0

端口22似乎是錯誤的默認FTP端口是21 – Sanjeev

回答

1

您正在使用FTP協議連接到SSH/SFTP端口22。

這是行不通的。

+0

謝謝! Martin ...上面鏈接給出的例子Java:什麼是從服務器爲我工作的SFTP文件的最佳方式。 –

+0

不客氣。雖然在StackOverflow我們[感謝接受答案](http://stackoverflow.com/help/someone-answers)。 –