天天看點

java ftp連接配接池_ftp連接配接池用戶端(示例代碼)

package com.scenetec.isv.utils.ftp.core;

import com.scenetec.isv.utils.ftp.config.FtpClientProperties;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

import org.apache.commons.pool2.BasePooledObjectFactory;

import org.apache.commons.pool2.PooledObject;

import org.apache.commons.pool2.impl.DefaultPooledObject;

import org.springframework.stereotype.Component;

@Slf4j

@Component

public class FtpClientFactory extends BasePooledObjectFactory {

private FtpClientProperties config;

public FtpClientFactory(FtpClientProperties config) {

this.config = config;

}

@Override

public FTPClient create() {

FTPClient ftpClient = new FTPClient();

ftpClient.setControlEncoding(config.getEncoding());

if (config.getConnectTimeout() != null) {

ftpClient.setConnectTimeout(config.getConnectTimeout());

}

try {

ftpClient.connect(config.getHost(), config.getPort());

int replyCode = ftpClient.getReplyCode();

if (!FTPReply.isPositiveCompletion(replyCode)) {

ftpClient.disconnect();

log.warn("FTPServer refused connection,replyCode:{}", replyCode);

return null;

}

if (!ftpClient.login(config.getUsername(), config.getPassword())) {

log.warn("FTPClient login failed... username is {}; password: {}", config.getUsername(), config.getPassword());

}

ftpClient.setBufferSize(config.getBufferSize());

ftpClient.setFileType(config.getTransferFileType());

if (config.isPassiveMode()) {

ftpClient.enterLocalPassiveMode();

}

} catch (Exception ex) {

ex.printStackTrace();

log.error("Failed to create FTP connection");

}

return ftpClient;

}

void destroyObject(FTPClient client) {

destroyObject(wrap(client));

}

@Override

public PooledObject wrap(FTPClient ftpClient) {

return new DefaultPooledObject<>(ftpClient);

}

@Override

public void destroyObject(PooledObject ftpPooled) {

if (ftpPooled == null) {

return;

}

FTPClient ftpClient = ftpPooled.getObject();

try {

if (ftpClient.isConnected()) {

ftpClient.logout();

}

} catch (Exception ex) {

ex.printStackTrace();

log.error("Failure to destroy FTP connection pool.");

} finally {

try {

ftpClient.disconnect();

} catch (Exception ex) {

ex.printStackTrace();

log.error("Failed to close FTP connection pool.");

}

}

}

@Override

public boolean validateObject(PooledObject ftpPooled) {

try {

FTPClient ftpClient = ftpPooled.getObject();

if (ftpClient == null) {

return false;

}

if (!ftpClient.isConnected()) {

return false;

}

return ftpClient.sendNoOp();

} catch (Exception ex) {

ex.printStackTrace();

}

return false;

}

}

package com.scenetec.isv.utils.ftp.core;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.pool2.BaseObjectPool;

import org.springframework.util.ObjectUtils;

import java.util.NoSuchElementException;

import java.util.concurrent.ArrayBlockingQueue;

import java.util.concurrent.BlockingQueue;

import java.util.concurrent.TimeUnit;

@Slf4j

@Deprecated

public class FtpClientPool extends BaseObjectPool {

private static final int DEFAULT_POOL_SIZE = 5;

private final BlockingQueue pool;

private final FtpClientFactory factory;

public FtpClientPool(FtpClientFactory factory) {

this(DEFAULT_POOL_SIZE, factory);

}

public FtpClientPool(int poolSize, FtpClientFactory factory) {

this.factory = factory;

this.pool = new ArrayBlockingQueue<>(poolSize * 2);

initPool(poolSize);

}

private void initPool(int maxPoolSize) {

try {

for (int i = 0; i < maxPoolSize; i++) {

addObject();

}

} catch (Exception ex) {

ex.printStackTrace();

throw new RuntimeException("Failed to initialize FTP thread pool.");

}

}

@Override

public FTPClient borrowObject() throws Exception, NoSuchElementException, IllegalStateException {

FTPClient client = pool.take();

if (ObjectUtils.isEmpty(client)) {

// 建立新的連接配接

client = factory.create();

// 返換對象

returnObject(client);

}

// 驗證對象是否有效

else if (!factory.validateObject(factory.wrap(client))) {

// 對無效的對象進行處理

invalidateObject(client);

// 建立新的對象

client = factory.create();

// 将新的對象放入連接配接池

returnObject(client);

}

// 傳回ftp對象

return client;

}

@Override

public void returnObject(FTPClient client) {

try {

long timeout = 3L;

if (client != null) {

if (client.isConnected()) {

if (pool.size() < DEFAULT_POOL_SIZE) {

if (!pool.offer(client, timeout, TimeUnit.SECONDS)) {

factory.destroyObject(client);

}

} else {

factory.destroyObject(client);

}

} else {

factory.destroyObject(client);

}

}

} catch (Exception ex) {

ex.printStackTrace();

log.error("Failed to return FTP connection object.");

}

}

@Override

public void invalidateObject(FTPClient client) {

try {

// 移除無效對象

pool.remove(client);

// 登出對象

factory.destroyObject(client);

} catch (Exception ex) {

ex.printStackTrace();

log.error("Failed to remove invalid FTP object.");

}

}

@Override

public void addObject() throws Exception, IllegalStateException, UnsupportedOperationException {

pool.offer(factory.create(), 3, TimeUnit.SECONDS);

}

@Override

public void close() {

try {

while (pool.iterator().hasNext()) {

FTPClient client = pool.take();

factory.destroyObject(client);

}

} catch (Exception ex) {

ex.printStackTrace();

log.error("Failed to close FTP object.");

}

}

}

package com.scenetec.isv.utils.ftp.core;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

import org.apache.commons.pool2.impl.GenericObjectPool;

import org.springframework.stereotype.Component;

import java.io.*;

@Slf4j

@Component

public class FtpClientTemplate {

private GenericObjectPool ftpClientPool;

public FtpClientTemplate(FtpClientFactory ftpClientFactory) {

this.ftpClientPool = new GenericObjectPool<>(ftpClientFactory);

}

public boolean uploadFile(String localPath, String remotePath) {

if (StringUtils.isBlank(localPath)) {

log.error("本地檔案路徑為空");

return false;

}

return uploadFile(new File(localPath), remotePath);

}

public boolean uploadFile(File localFile, String remotePath) {

if (!localFile.exists()) {

log.error("本地檔案不存在");

return false;

}

if (!localFile.isFile()) {

log.error("上傳類型不是檔案");

}

if (StringUtils.isBlank(remotePath)) {

remotePath = "/";

}

FileInputStream fis = null;

BufferedInputStream bis = null;

try {

fis = new FileInputStream(localFile);

bis = new BufferedInputStream(fis);

// 上傳

return uploadFile(bis, remotePath);

} catch (FileNotFoundException fex) {

fex.printStackTrace();

log.error("系統找不到指定的檔案:{}", localFile);

} catch (Exception ex) {

ex.printStackTrace();

log.error("上傳檔案異常。");

} finally {

try {

if (bis != null) {

bis.close();

}

} catch (Exception ex) {

ex.printStackTrace();

}

try {

if (fis != null) {

fis.close();

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

return false;

}

public boolean uploadFile(byte[] fileContent, String remotePath) {

if (fileContent == null || fileContent.length <= 0) {

log.error("上傳檔案内容為空。");

return false;

}

InputStream is = null;

try {

is = new ByteArrayInputStream(fileContent);

// 上傳

return uploadFile(is, remotePath);

} catch (Exception ex) {

ex.printStackTrace();

log.error("上傳檔案異常。原因:【{}】", ex.getMessage());

} finally {

try {

if (is != null) {

is.close();

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

return false;

}

public boolean uploadFile(InputStream inputStream, String remotePath) {

// 上傳

FTPClient client = null;

try {

// 擷取遠端檔案路徑

String remoteFilePath = getRemoteFilePath(remotePath);

// 擷取遠端檔案名

String remoteFileName = getRemoteFileName(remotePath);

// 從池中擷取對象

client = getFtpClient();

// 驗證連接配接是否成功

boolean bool = FTPReply.isPositiveCompletion(client.getReplyCode());

if (!bool) {

log.error("連接配接伺服器失敗,{}", client.getReplyString());

return false;

}

// 切換工作路徑

bool = changeDirectory(client, remoteFilePath);

if (!bool) {

log.error("切換工作路徑失敗,{}", client.getReplyString());

return false;

}

// 設定重試次數

final int retryTime = 3;

boolean retryResult = false;

for (int i = 0; i <= retryTime; i++) {

boolean success = client.storeFile(remoteFileName, inputStream);

if (success) {

log.info("檔案【{}】上傳成功。", remotePath);

retryResult = true;

break;

} else {

log.error("檔案上傳失敗。{}", client.getReplyString());

}

log.warn("檔案【{}】上傳失敗,重試上傳...嘗試{}次", remotePath, i);

}

return retryResult;

} catch (Exception ex) {

ex.printStackTrace();

log.error("上傳檔案異常。");

} finally {

// 将對象放回池中

if (client != null) {

ftpClientPool.returnObject(client);

}

}

return false;

}

public boolean downloadFile(String remotePath, String localPath) {

if (StringUtils.isBlank(remotePath)) {

remotePath = "/";

}

if (StringUtils.isBlank(localPath)) {

log.error("本地檔案路徑為空");

return false;

}

// 建立本地檔案路徑

File localFile = new File(localPath);

if (!localFile.exists()) {

File parentFile = localFile.getParentFile();

if (!parentFile.exists()) {

boolean bool = parentFile.mkdirs();

if (!bool) {

log.error("建立本地路徑失敗");

return false;

}

}

}

// 下載下傳

FTPClient client = null;

OutputStream os = null;

try {

os = new FileOutputStream(localPath);

// 擷取遠端檔案路徑

String remoteFilePath = getRemoteFilePath(remotePath);

// 擷取遠端檔案名

String remoteFileName = getRemoteFileName(remotePath);

// 從池中擷取對象

client = getFtpClient();

// 驗證連接配接是否成功

boolean bool = FTPReply.isPositiveCompletion(client.getReplyCode());

if (!bool) {

log.error("連接配接伺服器失敗,{}", client.getReplyString());

return false;

}

// 切換工作路徑

bool = client.changeWorkingDirectory(remoteFilePath);

if (!bool) {

log.error("切換工作路徑失敗,{}", client.getReplyString());

return false;

}

// 設定重試次數

final int retryTime = 3;

boolean retryResult = false;

for (int i = 0; i <= retryTime; i++) {

boolean success = client.retrieveFile(remoteFileName, os);

if (success) {

log.info("檔案【{}】下載下傳成功。", localPath);

retryResult = true;

break;

} else {

log.error("檔案下載下傳失敗。 {}", client.getReplyString());

}

log.warn("檔案【{}】下載下傳失敗,重試下載下傳...嘗試{}次", localPath, i);

}

// 傳回結果

return retryResult;

} catch (Exception ex) {

ex.printStackTrace();

log.error("下載下傳檔案異常。");

} finally {

if (client != null) {

ftpClientPool.returnObject(client);

}

try {

if (os != null) {

os.close();

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

return false;

}

public boolean deleteFile(String remotePath) {

FTPClient client = null;

try {

// 從池中擷取對象

client = getFtpClient();

// 驗證連接配接是否成功

boolean bool = FTPReply.isPositiveCompletion(client.getReplyCode());

if (!bool) {

log.error("連接配接伺服器失敗,{}", client.getReplyString());

return false;

}

// 删除檔案

bool = client.deleteFile(remotePath);

if (!bool) {

log.error("删除檔案失敗,{}", client.getReplyString());

}

return bool;

} catch (Exception ex) {

ex.printStackTrace();

log.error("删除檔案異常。");

} finally {

if (client != null) {

ftpClientPool.returnObject(client);

}

}

return false;

}

private String getRemoteFilePath(String remotePath) {

if (StringUtils.isNotBlank(remotePath)) {

return remotePath.substring(0, remotePath.lastIndexOf("/") + 1);

}

return "/";

}

private String getRemoteFileName(String remotePath) {

if (StringUtils.isNotBlank(remotePath)) {

return remotePath.substring(remotePath.lastIndexOf("/") + 1);

}

return "";

}

private FTPClient getFtpClient () {

FTPClient client = null;

try {

while (true) {

// 擷取用戶端

client = ftpClientPool.borrowObject();

// 驗證用戶端

if (client == null) {

continue;

} else {

if (!client.isConnected() || !FTPReply.isPositiveCompletion(client.getReplyCode())) {

ftpClientPool.invalidateObject(client);

} else {

break;

}

}

}

} catch (Exception ex) {

ex.printStackTrace();

}

return client;

}

private boolean changeDirectory(FTPClient client, String dir) {

try {

if (client == null || StringUtils.isBlank(dir)) {

return false;

}

String fileBackslashSeparator = "\";

String fileSeparator = "/";

if (StringUtils.contains(dir, fileBackslashSeparator)) {

dir = StringUtils.replaceAll(dir, fileBackslashSeparator, fileSeparator);

}

String[] dirArray = StringUtils.split(dir, fileSeparator);

String tmp = "";

for (String aDirArray : dirArray) {

tmp += fileSeparator + aDirArray;

if (!client.changeWorkingDirectory(tmp)) {

// 建立工作目錄

client.makeDirectory(tmp);

// 切換工作目錄

client.changeWorkingDirectory(tmp);

}

}

return true;

} catch (Exception ex) {

ex.printStackTrace();

log.error("切換工作目錄失敗。");

}

return false;

}

}