這裏使用了org.apache.commons.net.ftp這個類庫,僅僅是對這個類庫稍微封裝了一下方便使用,這裏寫了一個工具類,大家可以參考一下。
介紹一個 ftp客戶端工具:iis7服務器管理工具
IIs7服務器管理工具可以批量管理ftp站點,同時具備定時上傳下載的功能。
作爲服務器集成管理器,它最優秀的功能就是批量管理windows與linux系統服務器、vps。能極大的提高站長及服務器運維人員工作效率。同時iis7服務器管理工具還是vnc客戶端,服務器真正實現了一站式管理,可謂是非常方便。
下載地址:http://yczm.iis7.com/?tscc
依賴
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons.net.version}</version>
</dependency>
工具類
public class FtpUtil {
/**
* 維護FTPClient實例
*/
private final static LinkedBlockingQueue<FTPClient> FTP_CLIENT_QUEUE = new LinkedBlockingQueue<>();
/**
* 創建目錄
*
* @param ftpConfig 配置
* @param remotePath 需要創建目錄的目錄
* @param makePath 需要創建的目錄
* @return 是否創建成功
*/
public static boolean makeDirectory(FtpConfig ftpConfig, String remotePath, String makePath) {
try {
FTPClient ftpClient = connectClient(ftpConfig);
boolean changeResult = ftpClient.changeWorkingDirectory(remotePath);
if (!changeResult) {
throw new RuntimeException("切換目錄失敗");
}
boolean result = ftpClient.makeDirectory(makePath);
// 退出FTP
ftpClient.logout();
//歸還對象
offer(ftpClient);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 移動文件
*
* @param ftpConfig 配置
* @param fromPath 待移動目錄
* @param fromName 待移動文件名
* @param toPath 移動後目錄
* @param toName 移動後文件名
* @return 是否移動成功
*/
public static boolean moveFile(FtpConfig ftpConfig, String fromPath, String fromName, String toPath, String toName) {
try {
FTPClient ftpClient = connectClient(ftpConfig);
boolean changeResult = ftpClient.changeWorkingDirectory(fromPath);
if (!changeResult) {
throw new RuntimeException("切換目錄失敗");
}
boolean result = ftpClient.rename(fromName, toPath + File.separator + toName);
// 退出FTP
ftpClient.logout();
//歸還對象
offer(ftpClient);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 刪除文件
*
* @param ftpConfig 配置
* @param remotePath 遠程目錄
* @param fileName 文件名
* @return 是否刪除成功
*/
public static boolean deleteFile(FtpConfig ftpConfig, String remotePath, String fileName) {
try {
FTPClient ftpClient = connectClient(ftpConfig);
boolean changeResult = ftpClient.changeWorkingDirectory(remotePath);
if (!changeResult) {
throw new RuntimeException("切換目錄失敗");
}
boolean result = ftpClient.deleteFile(fileName);
// 退出FTP
ftpClient.logout();
//歸還對象
offer(ftpClient);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 下載文件
*
* @param ftpConfig 配置
* @param remotePath 遠程目錄
* @param fileName 文件名
* @param localPath 本地目錄
* @param localName 本地文件名
* @return 是否下載成功
*/
public static boolean download(FtpConfig ftpConfig, String remotePath, String fileName, String localPath, String localName) {
try {
FTPClient ftpClient = connectClient(ftpConfig);
boolean changeResult = ftpClient.changeWorkingDirectory(remotePath);
if (!changeResult) {
throw new RuntimeException("切換目錄失敗");
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
File file = new File(localPath, localName);
if (!file.getParentFile().exists()) {
boolean mkdirsResult = file.getParentFile().mkdirs();
if (!mkdirsResult) {
throw new RuntimeException("創建目錄失敗");
}
}
if (!file.exists()) {
boolean createFileResult = file.createNewFile();
if (!createFileResult) {
throw new RuntimeException("創建文件失敗");
}
}
OutputStream outputStream = new FileOutputStream(file);
boolean result = ftpClient.retrieveFile(fileName, outputStream);
outputStream.flush();
outputStream.close();
// 退出FTP
ftpClient.logout();
//歸還對象
offer(ftpClient);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 上傳文件
*
* @param ftpConfig 配置
* @param remotePath 遠程目錄
* @param inputStream 待上傳文件輸入流
* @param fileName 文件名
* @return 是否上傳成功
*/
public static boolean upload(FtpConfig ftpConfig, String remotePath, InputStream inputStream, String fileName) {
try {
FTPClient ftpClient = connectClient(ftpConfig);
boolean changeResult = ftpClient.changeWorkingDirectory(remotePath);
if (!changeResult) {
throw new RuntimeException("切換目錄失敗");
}
// 設置被動模式
ftpClient.enterLocalPassiveMode();
// 設置流上傳方式
ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
// 設置二進制上傳
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//中文存在問題
// 上傳 fileName爲上傳後的文件名
boolean result = ftpClient.storeFile(fileName, inputStream);
// 關閉本地文件流
inputStream.close();
// 退出FTP
ftpClient.logout();
//歸還對象
offer(ftpClient);
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 登錄ftp
*
* @param ftpConfig 配置
* @return 是否登錄成功
* @throws IOException
*/
private static FTPClient connectClient(FtpConfig ftpConfig) throws IOException {
FTPClient ftpClient = getClient();
// 連接FTP服務器
ftpClient.connect(ftpConfig.ip, ftpConfig.port);
// 登錄FTP
ftpClient.login(ftpConfig.userName, ftpConfig.password);
// 正常返回230登陸成功
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
throw new RuntimeException("連接ftp失敗");
}
ftpClient.setControlEncoding("GBK");
return ftpClient;
}
/**
* 獲取ftpClient對象
*
* @return 獲取client對象
*/
private static FTPClient getClient() {
FTPClient ftpClient = FTP_CLIENT_QUEUE.poll();
if (ftpClient != null) {
return ftpClient;
}
return new FTPClient();
}
private static void offer(FTPClient ftpClient) {
FTP_CLIENT_QUEUE.offer(ftpClient);
}
/**
* 連接ftp配置
*/
public static class FtpConfig {
private String ip;
private int port;
private String userName;
private String password;
public FtpConfig setIp(String ip) {
this.ip = ip;
return this;
}
public FtpConfig setPort(int port) {
this.port = port;
return this;
}
public FtpConfig setUserName(String userName) {
this.userName = userName;
return this;
}
public FtpConfig setPassword(String password) {
this.password = password;
return this;
}
}
}
使用示例
public class Main {
private final static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) throws Exception {
//連接配置
FtpUtil.FtpConfig ftpConfig = new FtpUtil.FtpConfig().setUserName("[email protected]")
.setPassword("zhan123456").setIp("192.168.33.105").setPort(21);
//創建目錄
FtpUtil.makeDirectory(ftpConfig, "/", "test");
FtpUtil.makeDirectory(ftpConfig, "/", "test2");
//上傳文件
InputStream inputStream = new FileInputStream("G:\\a.txt");
FtpUtil.upload(ftpConfig, "/test", inputStream, "zhan.txt");
//下載文件
FtpUtil.download(ftpConfig, "/test", "zhan.txt", "G:", "b.txt");
//移動文件
FtpUtil.moveFile(ftpConfig, "/test", "zhan.txt", "/test2", "zhan1.txt");
//刪除文件
FtpUtil.deleteFile(ftpConfig, "/test", "zhan.txt");
FtpUtil.deleteFile(ftpConfig, "/test2", "zhan1.txt");
}
}