Commit 0bea477b authored by neo.wang's avatar neo.wang

[DEV] use wrapclient with short ftpclient connection

parent 2e61cb2f
package pwc.taxtech.atms.common.ftp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FtpConfig {
@Value("${ftp.host}")
private String ftpHost;
@Value("${ftp.port}")
private Integer ftpPort;
@Value("${ftp.user}")
private String ftpUser;
@Value("${ftp.pwd}")
private String ftpPwd;
public String getFtpHost() {
return ftpHost;
}
public void setFtpHost(String ftpHost) {
this.ftpHost = ftpHost;
}
public Integer getFtpPort() {
return ftpPort;
}
public void setFtpPort(Integer ftpPort) {
this.ftpPort = ftpPort;
}
public String getFtpUser() {
return ftpUser;
}
public void setFtpUser(String ftpUser) {
this.ftpUser = ftpUser;
}
public String getFtpPwd() {
return ftpPwd;
}
public void setFtpPwd(String ftpPwd) {
this.ftpPwd = ftpPwd;
}
}
......@@ -2,72 +2,24 @@ 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.exception.ServiceException;
import javax.annotation.PostConstruct;
import java.io.*;
@Service
public class FtpService {
static String SYMBOL = "/";
@Autowired
FtpConfig config;
@Component
public class FTPClientPool {
private static final Logger LOGGER = LoggerFactory.getLogger(FTPClientPool.class);
private GenericObjectPoolConfig config;
private GenericObjectPool<FTPClient> pool;
private String ftpRootPath;
private static final String SYMBOL = "/";
private FTPClientConfig ftpClientConfig;
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);
ftpClientConfig = clientConfig;
pool = new GenericObjectPool<>(new FtpClientFactory(clientConfig), config);
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();
}
/**
* 上传
*
* @param filePath 相对路径
* @param fileName 文件名
* @param inputStream InputStream
* @throws Exception Exception
*/
public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath;
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool)) {
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) {
if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath;
} else {
......@@ -91,7 +43,8 @@ public class FTPClientPool {
* @throws Exception Exception
*/
public InputStream download(String filePath) throws Exception {
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool); OutputStream out = new ByteArrayOutputStream();) {
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config); OutputStream out = new ByteArrayOutputStream();) {
getRootPath(wrapFtpClient);
if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath);
InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath);
......@@ -131,9 +84,6 @@ public class FTPClientPool {
}
}
public String getFtpRootPath() {
return ftpRootPath;
}
/**
* 删除ftp文件
......@@ -141,7 +91,8 @@ public class FTPClientPool {
* @param filePath
*/
public void delete(String filePath) throws Exception {
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool);) {
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config);) {
getRootPath(wrapFtpClient);
if (StringUtils.isBlank(filePath)) {
return;
}
......@@ -153,7 +104,9 @@ public class FTPClientPool {
}
}
public FTPClientConfig getFtpClientConfig() {
return ftpClientConfig;
private void getRootPath(WrapFtpClient wrapFtpClient) throws Exception {
if (ftpRootPath == null || ftpRootPath.isEmpty())
ftpRootPath = wrapFtpClient.ftpClient.printWorkingDirectory();
}
}
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.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pwc.taxtech.atms.exception.ServiceException;
import java.io.IOException;
public class WrapFtpClient implements AutoCloseable {
private static Logger logger = LoggerFactory.getLogger(WrapFtpClient.class);
FTPClient ftpClient;
GenericObjectPool<FTPClient> pool;
public WrapFtpClient(GenericObjectPool<FTPClient> pool) throws Exception {
this.pool = pool;
this.ftpClient = pool.borrowObject();
public WrapFtpClient(FtpConfig config) throws Exception {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(config.getFtpHost(), config.getFtpPort());
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.getFtpUser()),
StringUtils.defaultString(config.getFtpPwd()));
if (!result) {
throw new ServiceException("ftpClient登陆失败! userName:" + config.getFtpUser() + " ; password:" + config.getFtpPwd());
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
this.ftpClient = ftpClient;
}
public FTPClient getFtpClient() {
return ftpClient;
}
@Override
public void close() throws Exception {
ftpClient.completePendingCommand();
pool.returnObject(ftpClient);
try {
if (ftpClient != null && ftpClient.isConnected()) {
logger.debug("destroy ftp client {}", ftpClient.toString());
ftpClient.logout();
}
} finally {
try {
if (ftpClient != null && ftpClient.isConnected())
ftpClient.disconnect();
} catch (IOException io) {
io.printStackTrace();
}
}
}
}
......@@ -6,19 +6,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pwc.taxtech.atms.dto.vatdto.TemplateByGroupDto;
import org.springframework.web.bind.annotation.*;
import pwc.taxtech.atms.common.ftp.FtpService;
import pwc.taxtech.atms.common.util.MyAsserts;
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.common.ftp.FTPClientPool;
import pwc.taxtech.atms.dto.*;
import pwc.taxtech.atms.dto.vatdto.TemplateByGroupDto;
import pwc.taxtech.atms.entitiy.Template;
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.exception.BadParameterException;
import pwc.taxtech.atms.exception.NotFoundException;
import pwc.taxtech.atms.service.TemplateService;
......@@ -38,7 +32,7 @@ public class TemplateController extends BaseController {
TemplateService templateService;
@Autowired
FTPClientPool ftpClientPool;
FtpService ftpService;
@RequestMapping(value = "get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody
......@@ -74,7 +68,7 @@ public class TemplateController extends BaseController {
if (template.getIsSystemType()) {
inputStream = new BufferedInputStream(new FileInputStream(templateFile));
} else {
inputStream = ftpClientPool.download(templatePath);
inputStream = ftpService.download(templatePath);
}
//客户端保存的文件名
String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx";
......@@ -139,7 +133,7 @@ public class TemplateController extends BaseController {
OperationResultDto<String> result = templateService.deleteTemplate(param);
if (result.getResult() && StringUtils.isNotBlank(result.getData())) {
try {
ftpClientPool.delete(result.getData());
ftpService.delete(result.getData());
} catch (Exception e) {
}
}
......
......@@ -9,11 +9,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pwc.taxtech.atms.exception.ServiceException;
import pwc.taxtech.atms.common.ftp.FTPClientPool;
import pwc.taxtech.atms.common.ftp.FtpService;
import pwc.taxtech.atms.common.message.ErrorMessage;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.TemplateGroupDto;
import pwc.taxtech.atms.exception.ServiceException;
import pwc.taxtech.atms.service.TemplateGroupService;
import java.util.List;
......@@ -27,7 +27,7 @@ public class TemplateGroupController {
TemplateGroupService templateGroupService;
@Autowired
FTPClientPool ftpClientPool;
FtpService ftpService;
@ApiOperation(value = "获取所有的模板分组")
@RequestMapping(value = "getall", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
......@@ -57,17 +57,17 @@ public class TemplateGroupController {
if (result.getResult()) {
List<String> pathList = (List<String>) result.getData();
if (pathList != null && pathList.size() > 0) {
for(String path:pathList){
for (String path : pathList) {
try {
ftpClientPool.delete(path);
}
catch(Exception e){
ftpService.delete(path);
} catch (Exception e) {
}
}
}
}
return result;
}
@ResponseBody
@ApiOperation(value = "获取Sheet名称")
@RequestMapping(value = "getSheetNameList", method = RequestMethod.POST)
......
......@@ -2,7 +2,7 @@ package pwc.taxtech.atms.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.common.ftp.FTPClientPool;
import pwc.taxtech.atms.common.ftp.FtpService;
import pwc.taxtech.atms.service.FileSystemService;
import java.io.InputStream;
......@@ -12,17 +12,17 @@ public class FTPFileSystemServiceImpl implements FileSystemService {
private static final String USER_TEMPLATE_PATH = "pwc/userTemplate/";
@Autowired
private FTPClientPool ftpClientPool;
private FtpService ftpService;
@Override
public String uploadUserTemplate(String fileName, InputStream inputStream) throws Exception {
ftpClientPool.upload(USER_TEMPLATE_PATH, fileName, inputStream);
ftpService.upload(USER_TEMPLATE_PATH, fileName, inputStream);
return USER_TEMPLATE_PATH + fileName;
}
@Override
public InputStream downloadUserTemplate(String filePath) throws Exception {
return ftpClientPool.download(filePath);
return ftpService.download(filePath);
}
}
......@@ -4,23 +4,24 @@ 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 pwc.taxtech.atms.common.ftp.FtpService;
import java.io.File;
import java.io.FileInputStream;
public class FTPTest extends CommonIT {
@Autowired
FTPClientPool ftpClientPool;
FtpService ftpService;
@Test
public void test(){
public void test() {
try {
FTPClient client = ftpClientPool.getClient();
// FTPClient client = ftpClientPool.getClient();
FTPClient client = null;
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")));
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