本文主要是介绍FTP的文件迁移,ftpClient.rename(pathFrom, pathTo),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
FTP文件迁移
- maven
- 本质
- 代码
- 业务逻辑代码,链接FTPClient
- ftpClient 链接代码fTPConnectionOb
- ftpClient 文件迁移changeFilePath
maven
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency><groupId>commons-net</groupId><artifactId>commons-net</artifactId><version>3.1</version>
</dependency>
或者org.apache.hadoop,因为包含了commons-net包
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.3</version>
</dependency>
本质
FTPClient ftpClient = new FTPClient ();
ftpClient.rename(pathFrom, pathTo)
代码
业务逻辑代码,链接FTPClient
// An highlighted block
try {String urlString = dataSourceCfg.getAddress();String ip = urlString.split(":")[0];String port = urlString.split(":")[1];String username = dataSourceCfg.getUser();String password = dataSourceCfg.getPassword();FTPClient ftpClient = FTPUtils.fTPConnectionOb( ip, port, username, password);String[] oldPath = path(dataXParameters.getSourcePath());String newPath = dataXParameters.getChangeFilePath();FTPUtils.changeFilePath( ftpClient, oldPath, newPath);
}catch (Exception e){logger.info(e.getMessage());
}
ftpClient 链接代码fTPConnectionOb
// An highlighted block
/**
* 连接FTP服务器
*
* @ id 地址,如:127.0.0.1
* @ port 端口,如:21
* @ username 用户名,如:root
* @ password 密码,如:root
*/
public static FTPClient fTPConnectionOb(String ip, String port, String username, String password) {Boolean b = false;FTPClient ftpClient = new FTPClient();try {ftpClient.connect(ip, Integer.valueOf(port));ftpClient.login(username, password);ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);int reply = ftpClient.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {closeConnect(ftpClient);System.out.println("FTP服务器连接失败:"+"地址:"+ip+" 端口:"+port+" 用户名:"+username+" 密码:"+password);}else {b=true;}} catch (Exception e) {System.out.println("FTP登录失败"+e);}return ftpClient;
}
ftpClient 文件迁移changeFilePath
// An highlighted block
/*** 20210719-wx* 迁移ftp的文件* @param ftpClient* @param oldPaths 旧的路径* @param newPath 文件转移的新路径*/
public static void changeFilePath(FTPClient ftpClient,String[] oldPaths,String newPath){try{for(int i=0;i<oldPaths.length;i++){//路径或者是指向具体文件都是一样的String oldPath = oldPaths[i];// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {LOCAL_CHARSET = "UTF-8";}ftpClient.setControlEncoding(LOCAL_CHARSET);ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);ftpClient.enterLocalPassiveMode();String ftpPath2 = new String(oldPath.getBytes(LOCAL_CHARSET),SERVER_CHARSET);FTPFile[] fs = ftpClient.listFiles(ftpPath2);Long timeSign = Calendar.getInstance().getTimeInMillis();if(fs.length == 1 && fs[0].isFile()){//具体到文件的String pathTo = new String((newPath+fs[0].getName()+String.valueOf(timeSign)).getBytes(LOCAL_CHARSET),SERVER_CHARSET);String pathFrom = new String(oldPath.getBytes(LOCAL_CHARSET),SERVER_CHARSET);ftpClient.rename(pathFrom, pathTo);}else{for(FTPFile file: fs){if(file.isFile()){String pathFrom = new String((oldPath+file.getName()).getBytes(LOCAL_CHARSET),SERVER_CHARSET);String pathTo = new String((newPath+String.valueOf(timeSign)+file.getName()).getBytes(LOCAL_CHARSET),SERVER_CHARSET);ftpClient.rename(pathFrom, pathTo);}}}}}catch (Exception e){logger.info(e.getMessage());}
};
这篇关于FTP的文件迁移,ftpClient.rename(pathFrom, pathTo)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!