String ftpUrl = "ftp://%s:%s@%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();
}
我已提供所有有效凭据,但出现异常:
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 异常:无效的用户名/密码
您正在使用 FTP 协议连接到 SSH/SFTP 端口 22。
那行不通。
int bytesRead = 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 9999999999999999999999999999999999999999999999999099000000000000000000000009099999999899999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 99999999999999999999999999999999999999999999999999999999999999999999999999989988899887845566778888999997765443223467788990 933456677888754444677899964332234677899997654432233455677754333455677643334556677776678777777;