Commit 77dfceae authored by neo.wang's avatar neo.wang

Merge branch 'dev_neo' into 'dev'

[DEV] use wrapclient with short ftpclient connection

See merge request root/atms!60
parents e8293302 0bea477b
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; ...@@ -2,72 +2,24 @@ package pwc.taxtech.atms.common.ftp;
import org.apache.commons.lang3.StringUtils; 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.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import pwc.taxtech.atms.exception.ServiceException; import pwc.taxtech.atms.exception.ServiceException;
import javax.annotation.PostConstruct;
import java.io.*; import java.io.*;
@Service
public class FtpService {
static String SYMBOL = "/";
@Autowired
FtpConfig config;
@Component String ftpRootPath;
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;
@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 { public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath; String upPath;
try (WrapFtpClient wrapFtpClient = new WrapFtpClient(pool)) { try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) {
if (StringUtils.isBlank(filePath)) { if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath; upPath = ftpRootPath;
} else { } else {
...@@ -91,7 +43,8 @@ public class FTPClientPool { ...@@ -91,7 +43,8 @@ public class FTPClientPool {
* @throws Exception Exception * @throws Exception Exception
*/ */
public InputStream download(String filePath) throws 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"); if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath); wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath);
InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath); InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath);
...@@ -131,9 +84,6 @@ public class FTPClientPool { ...@@ -131,9 +84,6 @@ public class FTPClientPool {
} }
} }
public String getFtpRootPath() {
return ftpRootPath;
}
/** /**
* 删除ftp文件 * 删除ftp文件
...@@ -141,7 +91,8 @@ public class FTPClientPool { ...@@ -141,7 +91,8 @@ public class FTPClientPool {
* @param filePath * @param filePath
*/ */
public void delete(String filePath) throws Exception { 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)) { if (StringUtils.isBlank(filePath)) {
return; return;
} }
...@@ -153,7 +104,9 @@ public class FTPClientPool { ...@@ -153,7 +104,9 @@ public class FTPClientPool {
} }
} }
public FTPClientConfig getFtpClientConfig() { private void getRootPath(WrapFtpClient wrapFtpClient) throws Exception {
return ftpClientConfig; if (ftpRootPath == null || ftpRootPath.isEmpty())
ftpRootPath = wrapFtpClient.ftpClient.printWorkingDirectory();
} }
} }
package pwc.taxtech.atms.common.ftp; 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.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.pool2.impl.GenericObjectPool; 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 { public class WrapFtpClient implements AutoCloseable {
private static Logger logger = LoggerFactory.getLogger(WrapFtpClient.class);
FTPClient ftpClient; FTPClient ftpClient;
GenericObjectPool<FTPClient> pool; GenericObjectPool<FTPClient> pool;
public WrapFtpClient(GenericObjectPool<FTPClient> pool) throws Exception { public WrapFtpClient(FtpConfig config) throws Exception {
this.pool = pool; FTPClient ftpClient = new FTPClient();
this.ftpClient = pool.borrowObject(); 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 @Override
public void close() throws Exception { public void close() throws Exception {
ftpClient.completePendingCommand(); try {
pool.returnObject(ftpClient); 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; ...@@ -6,19 +6,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody; import pwc.taxtech.atms.common.ftp.FtpService;
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 pwc.taxtech.atms.common.util.MyAsserts; 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.*;
import pwc.taxtech.atms.dto.vatdto.TemplateByGroupDto;
import pwc.taxtech.atms.entitiy.Template; import pwc.taxtech.atms.entitiy.Template;
import pwc.taxtech.atms.exception.ApplicationException;
import pwc.taxtech.atms.exception.BadParameterException; import pwc.taxtech.atms.exception.BadParameterException;
import pwc.taxtech.atms.exception.NotFoundException; import pwc.taxtech.atms.exception.NotFoundException;
import pwc.taxtech.atms.service.TemplateService; import pwc.taxtech.atms.service.TemplateService;
...@@ -38,7 +32,7 @@ public class TemplateController extends BaseController { ...@@ -38,7 +32,7 @@ public class TemplateController extends BaseController {
TemplateService templateService; TemplateService templateService;
@Autowired @Autowired
FTPClientPool ftpClientPool; FtpService ftpService;
@RequestMapping(value = "get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(value = "get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public @ResponseBody public @ResponseBody
...@@ -74,7 +68,7 @@ public class TemplateController extends BaseController { ...@@ -74,7 +68,7 @@ public class TemplateController extends BaseController {
if (template.getIsSystemType()) { if (template.getIsSystemType()) {
inputStream = new BufferedInputStream(new FileInputStream(templateFile)); inputStream = new BufferedInputStream(new FileInputStream(templateFile));
} else { } else {
inputStream = ftpClientPool.download(templatePath); inputStream = ftpService.download(templatePath);
} }
//客户端保存的文件名 //客户端保存的文件名
String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx"; String customFileName = "template_" + DateTime.now().toString("yyyyMMddHHmmss") + ".xlsx";
...@@ -139,7 +133,7 @@ public class TemplateController extends BaseController { ...@@ -139,7 +133,7 @@ public class TemplateController extends BaseController {
OperationResultDto<String> result = templateService.deleteTemplate(param); OperationResultDto<String> result = templateService.deleteTemplate(param);
if (result.getResult() && StringUtils.isNotBlank(result.getData())) { if (result.getResult() && StringUtils.isNotBlank(result.getData())) {
try { try {
ftpClientPool.delete(result.getData()); ftpService.delete(result.getData());
} catch (Exception e) { } catch (Exception e) {
} }
} }
......
...@@ -9,11 +9,11 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -9,11 +9,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import pwc.taxtech.atms.exception.ServiceException; import pwc.taxtech.atms.common.ftp.FtpService;
import pwc.taxtech.atms.common.ftp.FTPClientPool;
import pwc.taxtech.atms.common.message.ErrorMessage; import pwc.taxtech.atms.common.message.ErrorMessage;
import pwc.taxtech.atms.dto.OperationResultDto; import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.TemplateGroupDto; import pwc.taxtech.atms.dto.TemplateGroupDto;
import pwc.taxtech.atms.exception.ServiceException;
import pwc.taxtech.atms.service.TemplateGroupService; import pwc.taxtech.atms.service.TemplateGroupService;
import java.util.List; import java.util.List;
...@@ -27,7 +27,7 @@ public class TemplateGroupController { ...@@ -27,7 +27,7 @@ public class TemplateGroupController {
TemplateGroupService templateGroupService; TemplateGroupService templateGroupService;
@Autowired @Autowired
FTPClientPool ftpClientPool; FtpService ftpService;
@ApiOperation(value = "获取所有的模板分组") @ApiOperation(value = "获取所有的模板分组")
@RequestMapping(value = "getall", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @RequestMapping(value = "getall", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
...@@ -57,17 +57,17 @@ public class TemplateGroupController { ...@@ -57,17 +57,17 @@ public class TemplateGroupController {
if (result.getResult()) { if (result.getResult()) {
List<String> pathList = (List<String>) result.getData(); List<String> pathList = (List<String>) result.getData();
if (pathList != null && pathList.size() > 0) { if (pathList != null && pathList.size() > 0) {
for(String path:pathList){ for (String path : pathList) {
try { try {
ftpClientPool.delete(path); ftpService.delete(path);
} } catch (Exception e) {
catch(Exception e){
} }
} }
} }
} }
return result; return result;
} }
@ResponseBody @ResponseBody
@ApiOperation(value = "获取Sheet名称") @ApiOperation(value = "获取Sheet名称")
@RequestMapping(value = "getSheetNameList", method = RequestMethod.POST) @RequestMapping(value = "getSheetNameList", method = RequestMethod.POST)
......
...@@ -2,7 +2,7 @@ package pwc.taxtech.atms.service.impl; ...@@ -2,7 +2,7 @@ package pwc.taxtech.atms.service.impl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; 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 pwc.taxtech.atms.service.FileSystemService;
import java.io.InputStream; import java.io.InputStream;
...@@ -12,17 +12,17 @@ public class FTPFileSystemServiceImpl implements FileSystemService { ...@@ -12,17 +12,17 @@ public class FTPFileSystemServiceImpl implements FileSystemService {
private static final String USER_TEMPLATE_PATH = "pwc/userTemplate/"; private static final String USER_TEMPLATE_PATH = "pwc/userTemplate/";
@Autowired @Autowired
private FTPClientPool ftpClientPool; private FtpService ftpService;
@Override @Override
public String uploadUserTemplate(String fileName, InputStream inputStream) throws Exception { 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; return USER_TEMPLATE_PATH + fileName;
} }
@Override @Override
public InputStream downloadUserTemplate(String filePath) throws Exception { 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; ...@@ -4,23 +4,24 @@ import org.apache.commons.net.ftp.FTPClient;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import pwc.taxtech.atms.CommonIT; 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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
public class FTPTest extends CommonIT { public class FTPTest extends CommonIT {
@Autowired @Autowired
FTPClientPool ftpClientPool; FtpService ftpService;
@Test @Test
public void test(){ public void test() {
try { try {
FTPClient client = ftpClientPool.getClient(); // FTPClient client = ftpClientPool.getClient();
FTPClient client = null;
client.listFiles("hhhhh/sss/ccc"); client.listFiles("hhhhh/sss/ccc");
client.makeDirectory("hhhhh/sss/ccc"); client.makeDirectory("hhhhh/sss/ccc");
client.changeWorkingDirectory("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); System.out.println(client.listFiles().length);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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