FTP无用户名、密码使用以下方式传参登录
String username = "anonymous":
String password = null:
@Component
public class FTPExample {
@Value("${ftp.ip}")
private String ftpIp;
@Value("${ftp.port}")
private Integer ftpPort;
@Value("${ftp.username}")
private String username;
@Value("${ftp.password}")
private String password;
// @Scheduled(fixedDelay=120000)
public void ftp(){
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpIp, ftpPort);
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("连接FTP失败,用户名或密码错误。");
ftpClient.disconnect();
} else {
System.out.println("FTP连接成功!");
FTPFile[] files = ftpClient.listFiles();
printFileDetails(files,ftpClient);
}
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
public void printFileDetails(FTPFile[] files,FTPClient ftpClient) {
for (FTPFile file : files) {
String fileName = file.getName();
try {
InputStream inputStream = ftpClient.retrieveFileStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"GBK"));
String s = null;
List<String> listStr = new ArrayList<>();
while ((s = reader.readLine()) != null) {
System.out.println("===================>" + s);
listStr.add(s);
}
inputStream.close();
reader.close();
ftpClient.completePendingCommand();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(fileName);
}
}
}