Commit 19dafa28 authored by neo.wang's avatar neo.wang

[DEV] ftp upload and down load file

parent d307438b
...@@ -4,21 +4,19 @@ import org.apache.commons.lang3.StringUtils; ...@@ -4,21 +4,19 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 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.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import pwc.taxtech.atms.exception.ServiceException; import pwc.taxtech.atms.exception.ServiceException;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.io.BufferedOutputStream; import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@Component @Component
public class FTPClientPool { public class FTPClientPool {
private static final Logger LOGGER = LoggerFactory.getLogger(FTPClientPool.class);
private GenericObjectPoolConfig config; private GenericObjectPoolConfig config;
private GenericObjectPool<FTPClient> pool; private GenericObjectPool<FTPClient> pool;
private String ftpRootPath; private String ftpRootPath;
...@@ -48,12 +46,15 @@ public class FTPClientPool { ...@@ -48,12 +46,15 @@ public class FTPClientPool {
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(() -> {
pool.close(); pool.close();
})); }));
ftpRootPath = pool.borrowObject().printWorkingDirectory(); FTPClient client = pool.borrowObject();
ftpRootPath = client.printWorkingDirectory();
pool.returnObject(client);
} }
// this method should only be used in test // this method should only be used in test
public FTPClient getClient() throws Exception { public FTPClient getClient() throws Exception {
return pool.borrowObject(); return pool.borrowObject();
} }
/** /**
...@@ -66,8 +67,7 @@ public class FTPClientPool { ...@@ -66,8 +67,7 @@ public class FTPClientPool {
*/ */
public void upload(String filePath, String fileName, InputStream inputStream) throws Exception { public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath; String upPath;
FTPClient client = pool.borrowObject(); try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool)) {
try {
if (StringUtils.isBlank(filePath)) { if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath; upPath = ftpRootPath;
} else { } else {
...@@ -76,12 +76,10 @@ public class FTPClientPool { ...@@ -76,12 +76,10 @@ public class FTPClientPool {
if (!StringUtils.endsWith(upPath, SYMBOL)) { if (!StringUtils.endsWith(upPath, SYMBOL)) {
upPath = upPath + SYMBOL; upPath = upPath + SYMBOL;
} }
if (!isExist(upPath, client)) { if (!isExist(upPath, wrapFtpClient.ftpClient)) {
mkDir(upPath, client); mkDir(upPath, wrapFtpClient.ftpClient);
} }
client.storeFile(upPath + fileName, inputStream); wrapFtpClient.ftpClient.storeFile(upPath + fileName, inputStream);
} finally {
pool.returnObject(client);
} }
} }
...@@ -93,19 +91,18 @@ public class FTPClientPool { ...@@ -93,19 +91,18 @@ public class FTPClientPool {
* @throws Exception Exception * @throws Exception Exception
*/ */
public InputStream download(String filePath) throws Exception { public InputStream download(String filePath) throws Exception {
FTPClient client = pool.borrowObject(); try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool); OutputStream out = new ByteArrayOutputStream();) {
if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty"); if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
client.changeWorkingDirectory(ftpRootPath); wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath);
try (InputStream in = client.retrieveFileStream(filePath); OutputStream out = new ByteArrayOutputStream();){ InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath);
int len = 0; int len = 0;
byte[] bytes = new byte[1024]; byte[] bytes = new byte[1024];
while ((len = in.read(bytes)) != -1) { while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len); out.write(bytes, 0, len);
} }
return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray()); return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
} finally {
pool.returnObject(client);
} }
} }
private void mkDir(String path, FTPClient ftpClient) throws IOException { private void mkDir(String path, FTPClient ftpClient) throws IOException {
...@@ -144,18 +141,15 @@ public class FTPClientPool { ...@@ -144,18 +141,15 @@ public class FTPClientPool {
* @param filePath * @param filePath
*/ */
public void delete(String filePath) throws Exception { public void delete(String filePath) throws Exception {
if (StringUtils.isBlank(filePath)) { try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool);) {
return; if (StringUtils.isBlank(filePath)) {
} return;
FTPClient client = pool.borrowObject(); }
try { if (!isExist(filePath, wrapFtpClient.ftpClient)) {
if (!isExist(filePath, client)) {
return; return;
} else { } else {
client.deleteFile(filePath); wrapFtpClient.ftpClient.deleteFile(filePath);
} }
} finally {
pool.returnObject(client);
} }
} }
......
...@@ -21,6 +21,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -21,6 +21,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
this.config = config; this.config = config;
} }
public FTPClientConfig getConfig() {
return config;
}
@Override @Override
public PooledObject<FTPClient> makeObject() throws Exception { public PooledObject<FTPClient> makeObject() throws Exception {
FTPClient ftpClient = new FTPClient(); FTPClient ftpClient = new FTPClient();
...@@ -77,6 +81,11 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -77,6 +81,11 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
public boolean validateObject(PooledObject<FTPClient> pooledObject) { public boolean validateObject(PooledObject<FTPClient> pooledObject) {
try { try {
logger.debug("validate object ftp client {} ", pooledObject.getObject().toString()); 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(); return pooledObject.getObject().sendNoOp();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Failed to validate client: " + e, e); throw new RuntimeException("Failed to validate client: " + e, e);
......
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