Commit 95ae2245 authored by neo's avatar neo

[DEV] ftp download transfer stream us byte array

parent 89c2475c
......@@ -6,11 +6,16 @@ import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
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.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@Component
public class FTPClientPool {
......@@ -39,9 +44,14 @@ public class FTPClientPool {
clientConfig.setPassword(ftpPwd);
ftpClientConfig = clientConfig;
pool = new GenericObjectPool<>(new FtpClientFactory(clientConfig), config);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
pool.close();
}));
ftpRootPath = pool.borrowObject().printWorkingDirectory();
}
// this method should only be used in test
public FTPClient getClient() throws Exception {
return pool.borrowObject();
}
......@@ -56,7 +66,7 @@ public class FTPClientPool {
*/
public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath;
FTPClient client = getClient();
FTPClient client = pool.borrowObject();
try {
if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath;
......@@ -83,12 +93,18 @@ public class FTPClientPool {
* @throws Exception Exception
*/
public InputStream download(String filePath) throws Exception {
FTPClient client = getClient();
try {
FTPClient client = pool.borrowObject();
if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
client.changeWorkingDirectory(ftpRootPath);
return StringUtils.isBlank(filePath) ? null : client.retrieveFileStream(filePath);
try (InputStream in = client.retrieveFileStream(filePath); OutputStream out = new ByteArrayOutputStream();){
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());
} finally {
//pool.returnObject(client);
pool.returnObject(client);
}
}
......@@ -131,7 +147,7 @@ public class FTPClientPool {
if (StringUtils.isBlank(filePath)) {
return;
}
FTPClient client = getClient();
FTPClient client = pool.borrowObject();
try {
if (!isExist(filePath, client)) {
return;
......
......@@ -21,20 +21,18 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
this.config = config;
}
@Override
public PooledObject<FTPClient> makeObject() throws Exception {
FTPClient ftpClient = new 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;
throw new ServiceException("FTPServer refused connection");
}
boolean result = ftpClient.login(StringUtils.defaultString(config.getUsername()),
StringUtils.defaultString(config.getPassword()));
......@@ -51,10 +49,8 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
// if (StringUtils.equals(config.getPassiveMode(), "true")) {
// ftpClient.enterLocalPassiveMode();
// }
} catch (Exception e) {
logger.error("create ftp client error", e);
}
return new DefaultPooledObject<>(ftpClient);
}
@Override
......@@ -64,10 +60,9 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();
}
} catch (IOException io) {
io.printStackTrace();
} finally {
try {
if (ftpClient != null && ftpClient.isConnected())
ftpClient.disconnect();
} catch (IOException io) {
io.printStackTrace();
......
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