Commit 2c8f3c02 authored by neo.wang's avatar neo.wang

Merge branch 'dev_neo' into 'dev'

Dev neo

See merge request root/atms!58
parents ea74a6a8 19dafa28
......@@ -4,16 +4,19 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import pwc.taxtech.atms.exception.ServiceException;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
@Component
public class FTPClientPool {
private static final Logger LOGGER = LoggerFactory.getLogger(FTPClientPool.class);
private GenericObjectPoolConfig config;
private GenericObjectPool<FTPClient> pool;
private String ftpRootPath;
......@@ -39,9 +42,17 @@ public class FTPClientPool {
clientConfig.setPassword(ftpPwd);
ftpClientConfig = clientConfig;
pool = new GenericObjectPool<>(new FtpClientFactory(clientConfig), config);
ftpRootPath = pool.borrowObject().printWorkingDirectory();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
pool.close();
}));
FTPClient client = pool.borrowObject();
ftpRootPath = client.printWorkingDirectory();
pool.returnObject(client);
}
// this method should only be used in test
public FTPClient getClient() throws Exception {
return pool.borrowObject();
}
......@@ -56,8 +67,7 @@ public class FTPClientPool {
*/
public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath;
FTPClient client = getClient();
try {
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool)) {
if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath;
} else {
......@@ -66,12 +76,10 @@ public class FTPClientPool {
if (!StringUtils.endsWith(upPath, SYMBOL)) {
upPath = upPath + SYMBOL;
}
if (!isExist(upPath, client)) {
mkDir(upPath, client);
if (!isExist(upPath, wrapFtpClient.ftpClient)) {
mkDir(upPath, wrapFtpClient.ftpClient);
}
client.storeFile(upPath + fileName, inputStream);
} finally {
pool.returnObject(client);
wrapFtpClient.ftpClient.storeFile(upPath + fileName, inputStream);
}
}
......@@ -83,13 +91,18 @@ public class FTPClientPool {
* @throws Exception Exception
*/
public InputStream download(String filePath) throws Exception {
FTPClient client = getClient();
try {
client.changeWorkingDirectory(ftpRootPath);
return StringUtils.isBlank(filePath) ? null : client.retrieveFileStream(filePath);
} finally {
//pool.returnObject(client);
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool); OutputStream out = new ByteArrayOutputStream();) {
if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath);
InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath);
int len = 0;
byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
}
}
private void mkDir(String path, FTPClient ftpClient) throws IOException {
......@@ -128,18 +141,15 @@ public class FTPClientPool {
* @param filePath
*/
public void delete(String filePath) throws Exception {
if (StringUtils.isBlank(filePath)) {
return;
}
FTPClient client = getClient();
try {
if (!isExist(filePath, client)) {
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool);) {
if (StringUtils.isBlank(filePath)) {
return;
}
if (!isExist(filePath, wrapFtpClient.ftpClient)) {
return;
} else {
client.deleteFile(filePath);
wrapFtpClient.ftpClient.deleteFile(filePath);
}
} finally {
pool.returnObject(client);
}
}
......
......@@ -21,6 +21,9 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
this.config = config;
}
public FTPClientConfig getConfig() {
return config;
}
@Override
public PooledObject<FTPClient> makeObject() throws Exception {
......@@ -28,33 +31,32 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
if (null != config.getClientTimeout()) {
ftpClient.setConnectTimeout(config.getClientTimeout());
}
try {
ftpClient.connect(config.getHost(), config.getPort());
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
logger.warn("FTPServer refused connection");
return null;
}
boolean result = ftpClient.login(StringUtils.defaultString(config.getUsername()),
StringUtils.defaultString(config.getPassword()));
if (!result) {
throw new ServiceException("ftpClient登陆失败! userName:" + config.getUsername() + " ; password:" + config.getPassword());
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if (null != config.getTransferFileType()) {
ftpClient.setFileType(config.getTransferFileType());
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding(StringUtils.defaultString(config.getEncoding(), "UTF-8"));
ftpClient.enterLocalPassiveMode();
ftpClient.connect(config.getHost(), config.getPort());
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
logger.warn("FTPServer refused connection");
throw new ServiceException("FTPServer refused connection");
}
boolean result = ftpClient.login(StringUtils.defaultString(config.getUsername()),
StringUtils.defaultString(config.getPassword()));
if (!result) {
throw new ServiceException("ftpClient登陆失败! userName:" + config.getUsername() + " ; password:" + config.getPassword());
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
if (null != config.getTransferFileType()) {
ftpClient.setFileType(config.getTransferFileType());
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding(StringUtils.defaultString(config.getEncoding(), "UTF-8"));
ftpClient.enterLocalPassiveMode();
// if (StringUtils.equals(config.getPassiveMode(), "true")) {
// ftpClient.enterLocalPassiveMode();
// }
} catch (Exception e) {
logger.error("create ftp client error", e);
}
logger.debug("mk objec ftp client {}", ftpClient.toString());
return new DefaultPooledObject<>(ftpClient);
}
@Override
......@@ -62,13 +64,13 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
FTPClient ftpClient = pooledObject.getObject();
try {
if (ftpClient != null && ftpClient.isConnected()) {
logger.debug("destroy ftp client {}", ftpClient.toString());
ftpClient.logout();
}
} catch (IOException io) {
io.printStackTrace();
} finally {
try {
ftpClient.disconnect();
if (ftpClient != null && ftpClient.isConnected())
ftpClient.disconnect();
} catch (IOException io) {
io.printStackTrace();
}
......@@ -78,6 +80,12 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
@Override
public boolean validateObject(PooledObject<FTPClient> pooledObject) {
try {
logger.debug("validate object ftp client {} ", pooledObject.getObject().toString());
FTPClient ftpClient = pooledObject.getObject();
if (ftpClient == null || !ftpClient.isConnected()) {
return false;
}
ftpClient.changeWorkingDirectory("/");
return pooledObject.getObject().sendNoOp();
} catch (IOException e) {
throw new RuntimeException("Failed to validate client: " + e, e);
......@@ -86,12 +94,12 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
@Override
public void activateObject(PooledObject<FTPClient> pooledObject) throws Exception {
logger.debug("activateObject {}", pooledObject.getObject().toString());
}
@Override
public void passivateObject(PooledObject<FTPClient> pooledObject) throws Exception {
logger.debug("passivateObject {}", pooledObject.getObject().toString());
}
}
package pwc.taxtech.atms.common.ftp;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;
public class WrapFtpClient implements AutoCloseable {
FTPClient ftpClient;
GenericObjectPool<FTPClient> pool;
public WrapFtpClient(GenericObjectPool<FTPClient> pool) throws Exception {
this.pool = pool;
this.ftpClient = pool.borrowObject();
}
public FTPClient getFtpClient() {
return ftpClient;
}
@Override
public void close() throws Exception {
ftpClient.completePendingCommand();
pool.returnObject(ftpClient);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment