Commit 21f28b5d authored by eddie.woo's avatar eddie.woo

add ftp

parent a10fa2f6
......@@ -258,6 +258,16 @@
<artifactId>commons-text</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<dependency>
......
package pwc.taxtech.atms.common.ftp;
public class FTPClientConfig {
private String host;
private Integer port;
private String username;
private String password;
private String passiveMode;
private String encoding;
private Integer clientTimeout;
private Integer threadNum;
private Integer transferFileType;
private boolean renameUploaded;
private Integer retryTimes;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassiveMode() {
return passiveMode;
}
public void setPassiveMode(String passiveMode) {
this.passiveMode = passiveMode;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public Integer getClientTimeout() {
return clientTimeout;
}
public void setClientTimeout(Integer clientTimeout) {
this.clientTimeout = clientTimeout;
}
public Integer getThreadNum() {
return threadNum;
}
public void setThreadNum(Integer threadNum) {
this.threadNum = threadNum;
}
public Integer getTransferFileType() {
return transferFileType;
}
public void setTransferFileType(Integer transferFileType) {
this.transferFileType = transferFileType;
}
public boolean isRenameUploaded() {
return renameUploaded;
}
public void setRenameUploaded(boolean renameUploaded) {
this.renameUploaded = renameUploaded;
}
public Integer getRetryTimes() {
return retryTimes;
}
public void setRetryTimes(Integer retryTimes) {
this.retryTimes = retryTimes;
}
}
package pwc.taxtech.atms.common.ftp;
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.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
@Component
public class FTPClientPool {
private GenericObjectPoolConfig config;
private GenericObjectPool<FTPClient> pool;
private String ftpRootPath;
@Value("${ftp.host}")
private String ftpHost;
@Value("${ftp.port}")
private Integer ftpPort;
@Value("${ftp.user}")
private String ftpUser;
@Value("${ftp.pwd}")
private String ftpPwd;
@PostConstruct
public void init() throws Exception {
config = new GenericObjectPoolConfig();
FTPClientConfig clientConfig = new FTPClientConfig();
clientConfig.setHost(ftpHost);
clientConfig.setPort(ftpPort);
clientConfig.setUsername(ftpUser);
clientConfig.setPassword(ftpPwd);
pool = new GenericObjectPool<>(new FtpClientFactory(clientConfig), config);
ftpRootPath = pool.borrowObject().printWorkingDirectory();
}
public FTPClient getClient() throws Exception {
return pool.borrowObject();
}
/**
* 上传
* @param filePath 相对路径
* @param fileName 文件名
* @param inputStream InputStream
* @throws Exception Exception
*/
public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath;
if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath;
} else {
upPath = filePath;
}
FTPClient client = getClient();
if (!isExist(upPath, client)) {
mkDir(upPath, client);
}
client.storeFile(upPath + fileName, inputStream);
}
/**
* 下载
* @param filePath 相对路径 + 文件名
* @return InputStream
* @throws Exception Exception
*/
public InputStream download(String filePath) throws Exception {
FTPClient client = getClient();
client.changeWorkingDirectory(ftpRootPath);
return StringUtils.isBlank(filePath) ? null : client.retrieveFileStream(filePath);
}
private void mkDir(String path, FTPClient ftpClient) throws IOException {
if (StringUtils.isNotBlank(path)) {
ftpClient.changeWorkingDirectory(ftpRootPath);
String[] paths = path.split("/");
for (String p : paths) {
if (StringUtils.isNotBlank(p) && !StringUtils.equals(p, ".")) {
if (!ftpClient.changeWorkingDirectory(p)) {
ftpClient.makeDirectory(p);
if (!ftpClient.changeWorkingDirectory(p)){
throw new IOException("changeWorkingDirectory error.");
}
}
}
}
}
}
private boolean isExist(String path, FTPClient ftpClient) throws IOException {
String pwd = ftpClient.printWorkingDirectory();
try {
return ftpClient.changeWorkingDirectory(path);
} finally {
ftpClient.changeWorkingDirectory(pwd);
}
}
public String getFtpRootPath() {
return ftpRootPath;
}
}
package pwc.taxtech.atms.common.ftp;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pwc.taxtech.atms.common.ServiceException;
import java.io.IOException;
public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
private static final Logger logger = LoggerFactory.getLogger(FtpClientFactory.class);
private FTPClientConfig config;
public FtpClientFactory(FTPClientConfig config) {
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;
}
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);
}
return new DefaultPooledObject<>(ftpClient);
}
@Override
public void destroyObject(PooledObject<FTPClient> pooledObject) throws Exception {
FTPClient ftpClient = pooledObject.getObject();
try {
if (ftpClient != null && ftpClient.isConnected()) {
ftpClient.logout();
}
} catch (IOException io) {
io.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException io) {
io.printStackTrace();
}
}
}
@Override
public boolean validateObject(PooledObject<FTPClient> pooledObject) {
try {
return pooledObject.getObject().sendNoOp();
} catch (IOException e) {
throw new RuntimeException("Failed to validate client: " + e, e);
}
}
@Override
public void activateObject(PooledObject<FTPClient> pooledObject) throws Exception {
}
@Override
public void passivateObject(PooledObject<FTPClient> pooledObject) throws Exception {
}
}
......@@ -9,4 +9,10 @@ mail_jdbc_password=${mail_jdbc_password}
web.url=${web.url}
jwt.base64Secret=${jwt.base64Secret}
jwt.powerToken=${jwt.powerToken}
\ No newline at end of file
jwt.powerToken=${jwt.powerToken}
#FTP Config
ftp.host=${ftp.host}
ftp.port=${ftp.port}
ftp.user=${ftp.user}
ftp.pwd=${ftp.pwd}
\ No newline at end of file
......@@ -9,4 +9,9 @@ mail_jdbc_password=atmsunittestSQL
web.url=http://localhost:8080
jwt.base64Secret=TXppQjFlZFBSbnJzMHc0Tg==
jwt.powerToken=xxxx
\ No newline at end of file
jwt.powerToken=xxxx
ftp.host=cnshaappulv004.asia.pwcinternal.com
ftp.port=21
ftp.user=ftpuser
ftp.pwd=12345678
\ No newline at end of file
......@@ -9,4 +9,9 @@ mail_jdbc_password=atmsunittestSQL
web.url=http://192.168.1.102:10000
jwt.base64Secret=TXppQjFlZFBSbnJzMHc0Tg==
jwt.powerToken=
\ No newline at end of file
jwt.powerToken=
ftp.host=cnshaappulv004.asia.pwcinternal.com
ftp.port=21
ftp.user=ftpuser
ftp.pwd=12345678
\ No newline at end of file
......@@ -9,4 +9,9 @@ mail_jdbc_password=atmsunittestSQL
web.url=http://cnshaappulv004:8080
jwt.base64Secret=TXppQjFlZFBSbnJzMHc0Tg==
jwt.powerToken=xxxx
\ No newline at end of file
jwt.powerToken=xxxx
ftp.host=cnshaappulv004.asia.pwcinternal.com
ftp.port=21
ftp.user=ftpuser
ftp.pwd=12345678
\ No newline at end of file
package pwc.taxtech.atms.common;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import pwc.taxtech.atms.CommonIT;
import pwc.taxtech.atms.common.ftp.FTPClientPool;
import java.io.File;
import java.io.FileInputStream;
public class FTPTest extends CommonIT {
@Autowired
FTPClientPool ftpClientPool;
@Test
public void test(){
try {
FTPClient client = ftpClientPool.getClient();
client.listFiles("hhhhh/sss/ccc");
client.makeDirectory("hhhhh/sss/ccc");
client.changeWorkingDirectory("hhhhh/sss/ccc");
client.storeFile("./aa/bb/cc/dd/text.txt",new FileInputStream(new File("C:\\temp/Fixed.txt")));
System.out.println(client.listFiles().length);
} catch (Exception e) {
e.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