我正在尝试将文件从本地计算机上传到ftp服务器,当这个代码首先启动它在ftp服务器上成功存储6个文件夹数据但是从第7个文件夹到第25个文件夹我得到连接拒绝错误...没有限制从客户端进行连接,只有我使用这些凭据进行测试。任何人都可以帮助我吗? 我是因为代码中的镜像问题而得到此问题,但我无法解决此问题。请帮帮我
package com.epath.smoketest.tests;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPSClient;
public class FTPUploadDirectoryTest {
public static void main(String[] args) {
try {
System.out.println("\n"
+ "---------------------------------------------------------------------------------" + "\r\n");
System.out.println("Read FTP Login details from PROPERTIES file" + "\r\n");
System.out.println(
"---------------------------------------------------------------------------------" + "\r\n");
Properties prop = new Properties();
InputStream input = null;
String inputFS = getAutomationInputDataPath() + "//Validation.properties";
input = new FileInputStream(inputFS);
prop.load(input);
// -Input file with test data
System.out.println("Uploading file on ftp server");
String ftp_port = prop.getProperty("ftp_port");
int ftp_host = Integer.parseInt(prop.getProperty("ftp_host"));
String ftp_username = prop.getProperty("ftp_username");
String ftp_password = prop.getProperty("ftp_password");
String server = ftp_port;
int port = ftp_host;
String user = ftp_username;
String pass = ftp_password;
FTPSClient ftpClient = new FTPSClient();
// connect and login to the server
ftpClient.connect(server, port);
ftpClient.login(user, pass);
// use local passive mode to pass firewall
ftpClient.enterLocalPassiveMode();
System.out.println("Connected");
String remoteDirPath = "/www/ngage/screenshots";
String localDirPath = folderPathForUploadingOnFTP;
uploadDirectory(ftpClient, remoteDirPath, localDirPath, "");
// log out and disconnect from the server
ftpClient.logout();
ftpClient.disconnect();
System.out.println("Disconnected");
} catch (IOException ex) {
System.err.println("Error occured....." + ex.getMessage());
ex.printStackTrace();
}
}
public static void uploadDirectory(FTPSClient ftpClient, String remoteDirPath, String localParentDir,
String remoteParentDir) throws IOException {
File localDir = new File(localParentDir);
File[] subFiles = localDir.listFiles();
if (subFiles != null && subFiles.length > 0) {
for (File item : subFiles) {
String remoteFilePath = remoteDirPath + "/" + remoteParentDir + "/" + item.getName();
if (remoteParentDir.equals("")) {
remoteFilePath = remoteDirPath + "/" + item.getName();
}
if (item.isFile()) {
if (!checkFileExists(remoteFilePath, ftpClient)) {
// upload the file
String localFilePath = item.getAbsolutePath();
boolean uploaded = uploadSingleFile(ftpClient, localFilePath, remoteFilePath);
if (uploaded) {
System.out.println("UPLOADED a file to: " + remoteFilePath);
} else {
System.out.println("COULD NOT upload the file: " + localFilePath);
}
} else {
System.out.println("This file alerady exist on ftp server ");
}
} else {
// create directory on the server
boolean created = ftpClient.makeDirectory(remoteFilePath);
if (created) {
System.out.println("CREATED the directory: " + remoteFilePath);
} else {
System.out.println("COULD NOT create the directory: " + remoteFilePath);
}
// upload the sub directory
String parent = remoteParentDir + "/" + item.getName();
if (remoteParentDir.equals("")) {
parent = item.getName();
}
localParentDir = item.getAbsolutePath();
uploadDirectory(ftpClient, remoteDirPath, localParentDir, parent);
}
}
}
ftpClient.makeDirectory(remoteDirPath);
}
public static boolean uploadSingleFile(FTPSClient ftpClient, String localFilePath, String remoteFilePath)
throws IOException {
File localFile = new File(localFilePath);
InputStream inputStream = new FileInputStream(localFile);
try {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
return ftpClient.storeFile(remoteFilePath, inputStream);
} finally {
inputStream.close();
}
}
public static Boolean checkFileExists(String filePath, FTPSClient ftpClient) throws IOException {
InputStream inputStream = ftpClient.retrieveFileStream(filePath);
int returnCode = ftpClient.getReplyCode();
if (inputStream == null || returnCode == 550) {
return false;
}
inputStream.close();
return true;
}
}
Error coming .
COULD NOT upload the file: \\192.168.10.21\volume1\ngage_dev\engineering\ngage\testing\automated\validation\Build_1.19.1\051\2018-04-23_07-54-39_AM\TC_UA_PSWD_0001_006_1.png
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.ftp.FTPClient._openDataConnection_(FTPClient.java:920)
at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:627)
at org.apache.commons.net.ftp.FTPClient._retrieveFileStream(FTPClient.java:1980)
at org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1967)
at com.epath.smoketest.tests.FTPUploadDirectoryTest.checkFileExists(FTPUploadDirectoryTest.java:124)