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; ...@@ -6,11 +6,16 @@ import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
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 javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
@Component @Component
public class FTPClientPool { public class FTPClientPool {
...@@ -39,11 +44,16 @@ public class FTPClientPool { ...@@ -39,11 +44,16 @@ public class FTPClientPool {
clientConfig.setPassword(ftpPwd); clientConfig.setPassword(ftpPwd);
ftpClientConfig = clientConfig; ftpClientConfig = clientConfig;
pool = new GenericObjectPool<>(new FtpClientFactory(clientConfig), config); pool = new GenericObjectPool<>(new FtpClientFactory(clientConfig), config);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
pool.close();
}));
ftpRootPath = pool.borrowObject().printWorkingDirectory(); ftpRootPath = pool.borrowObject().printWorkingDirectory();
} }
// this method should only be used in test
public FTPClient getClient() throws Exception { public FTPClient getClient() throws Exception {
return pool.borrowObject(); return pool.borrowObject();
} }
/** /**
...@@ -56,7 +66,7 @@ public class FTPClientPool { ...@@ -56,7 +66,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 = getClient(); FTPClient client = pool.borrowObject();
try { try {
if (StringUtils.isBlank(filePath)) { if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath; upPath = ftpRootPath;
...@@ -83,12 +93,18 @@ public class FTPClientPool { ...@@ -83,12 +93,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 = getClient(); FTPClient client = pool.borrowObject();
try { if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
client.changeWorkingDirectory(ftpRootPath); 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 { } finally {
//pool.returnObject(client); pool.returnObject(client);
} }
} }
...@@ -131,7 +147,7 @@ public class FTPClientPool { ...@@ -131,7 +147,7 @@ public class FTPClientPool {
if (StringUtils.isBlank(filePath)) { if (StringUtils.isBlank(filePath)) {
return; return;
} }
FTPClient client = getClient(); FTPClient client = pool.borrowObject();
try { try {
if (!isExist(filePath, client)) { if (!isExist(filePath, client)) {
return; return;
......
...@@ -21,40 +21,36 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -21,40 +21,36 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
this.config = config; this.config = config;
} }
@Override @Override
public PooledObject<FTPClient> makeObject() throws Exception { public PooledObject<FTPClient> makeObject() throws Exception {
FTPClient ftpClient = new FTPClient(); FTPClient ftpClient = new FTPClient();
if (null != config.getClientTimeout()) { if (null != config.getClientTimeout()) {
ftpClient.setConnectTimeout(config.getClientTimeout()); ftpClient.setConnectTimeout(config.getClientTimeout());
} }
try { ftpClient.connect(config.getHost(), config.getPort());
ftpClient.connect(config.getHost(), config.getPort()); int reply = ftpClient.getReplyCode();
int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) {
if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect();
ftpClient.disconnect(); logger.warn("FTPServer refused connection");
logger.warn("FTPServer refused connection"); throw new ServiceException("FTPServer refused connection");
return null; }
} boolean result = ftpClient.login(StringUtils.defaultString(config.getUsername()),
boolean result = ftpClient.login(StringUtils.defaultString(config.getUsername()), StringUtils.defaultString(config.getPassword()));
StringUtils.defaultString(config.getPassword())); if (!result) {
if (!result) { throw new ServiceException("ftpClient登陆失败! userName:" + config.getUsername() + " ; password:" + config.getPassword());
throw new ServiceException("ftpClient登陆失败! userName:" + config.getUsername() + " ; password:" + config.getPassword()); }
} ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); if (null != config.getTransferFileType()) {
if (null != config.getTransferFileType()) { ftpClient.setFileType(config.getTransferFileType());
ftpClient.setFileType(config.getTransferFileType()); }
} ftpClient.setBufferSize(1024);
ftpClient.setBufferSize(1024); ftpClient.setControlEncoding(StringUtils.defaultString(config.getEncoding(), "UTF-8"));
ftpClient.setControlEncoding(StringUtils.defaultString(config.getEncoding(), "UTF-8")); ftpClient.enterLocalPassiveMode();
ftpClient.enterLocalPassiveMode();
// if (StringUtils.equals(config.getPassiveMode(), "true")) { // if (StringUtils.equals(config.getPassiveMode(), "true")) {
// ftpClient.enterLocalPassiveMode(); // ftpClient.enterLocalPassiveMode();
// } // }
} catch (Exception e) {
logger.error("create ftp client error", e);
}
return new DefaultPooledObject<>(ftpClient); return new DefaultPooledObject<>(ftpClient);
} }
@Override @Override
...@@ -64,11 +60,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -64,11 +60,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
if (ftpClient != null && ftpClient.isConnected()) { if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout(); ftpClient.logout();
} }
} catch (IOException io) {
io.printStackTrace();
} finally { } finally {
try { try {
ftpClient.disconnect(); if (ftpClient != null && ftpClient.isConnected())
ftpClient.disconnect();
} catch (IOException io) { } catch (IOException io) {
io.printStackTrace(); 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