ftp登录命令:
windows环境下登录:文件管理资源的地址栏中输入“ftp://ip:port”,然后根据提示输入账号、密码。
linux环境下登录:ftp ip,然后根据提示输入账号、密码,linux中的ftp命令与基本命令的操作一致。
Maven依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
Java的ftp上传
@PostConstruct
public void init(){
this.ftpClient = FtpUtils.initFtpClient(hostname, port, username ,password);
this.ftpClient.setControlEncoding("UTF-8");
this.ftpClient.setConnectTimeout(20000);
this.ftpClient.setDataTimeout(600000);
this.ftpClient.enterLocalPassiveMode();
this.timeStamp = DateUtil.today();
}
public void uploadFtpFiile(){
boolean upload = false;
try {
InputStream inputStream = new FileInputStream(ftpFileTemplate);
FTPClient ftpClient = FtpUtils.getFtpClient();
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpPath = CommonConfig.get("fpt.path", "upload");
if (!ftpClient.changeWorkingDirectory(ftpPath)){
ftpClient.makeDirectory(ftpPath);
}
ftpClient.changeWorkingDirectory(ftpPath);
if (!ftpClient.changeWorkingDirectory(DateUtil.today())){
ftpClient.makeDirectory(DateUtil.today());
}
ftpClient.changeWorkingDirectory(DateUtil.today());
filename = "ftp.test.file.res";
upload = ftpClient.storeFile(new String(filename.getBytes("GBK"), StandardCharsets.ISO_8859_1), inputStream);
inputStream.close();
}catch (FTPConnectionClosedException ftpConnectionClosedException){
log.error("ftp连接异常重新连接:",ftpConnectionClosedException);
FtpUtils.init();
} catch (Exception e) {
log.error("ftp推送失败:",e);
}
log.info("ftp upload status:{}",upload);
}
Java的ftp下载
@PostConstruct
public void init(){
this.ftpClient = FtpUtils.initFtpClient(hostname, port, username ,password);
this.ftpClient.setControlEncoding("UTF-8");
this.ftpClient.setConnectTimeout(20000);
this.ftpClient.setDataTimeout(600000);
this.ftpClient.enterLocalPassiveMode();
this.timeStamp = DateUtil.today();
}
public void readFtpFiile() throws Exception {
String today = DateUtil.today().replaceAll("-", "");
ftpClient.changeWorkingDirectory(today);
ftpClient.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile ftpFile : ftpFiles) {
String name = ftpFile.getName();
log.info("文件名称:{}",name);
InputStream inputStream = ftpClient.retrieveFileStream(name);
}
}
ftp工具类
@Component
@Slf4j
public class FtpUtils {
private static FTPClient ftpClient;
public static void init(){
FtpUtils.ftpClient = FtpUtils.initFtpClient(CommonConfig.get("ftp.hostname", "172.25.241.191"),
CommonConfig.getInteger("ftp.port",21),
CommonConfig.getString("ftp.username", "ftpuser") ,
CommonConfig.getString("ftp.password", "xsZYroD3"));
FtpUtils.ftpClient.setControlEncoding("GBK");
FtpUtils.ftpClient.setConnectTimeout(20000);
FtpUtils.ftpClient.setDataTimeout(600000);
FtpUtils.ftpClient.enterLocalPassiveMode();
}
public static FTPClient initFtpClient(String hostname, int port, String username, String password) {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
try {
log.info("connecting...ftp服务器:{},{}",hostname,port);
ftpClient.connect(hostname, port);
ftpClient.login(username, password);
int replyCode = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(replyCode)){
log.info("connect failed...ftp服务器:{}/{}",hostname, port);
}
log.info("connect successful...ftp服务器:{}/{}",hostname,port);
}catch (MalformedURLException e) {
log.error("连接ftp异常MalformedURLException:",e);
}catch (IOException e) {
log.error("连接ftp异常IOException:",e);
}
return ftpClient;
}
public static FTPClient getFtpClient() {
init();
return ftpClient;
}
}