Commit 9ae926d1 authored by frank.xa.zhang's avatar frank.xa.zhang

Merge branch 'dev' into dev_frank

parents 3b1cda94 77dfceae
...@@ -21,6 +21,9 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -21,6 +21,9 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
this.config = config; this.config = config;
} }
public FTPClientConfig getConfig() {
return config;
}
@Override @Override
public PooledObject<FTPClient> makeObject() throws Exception { public PooledObject<FTPClient> makeObject() throws Exception {
...@@ -28,13 +31,12 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -28,13 +31,12 @@ public class FtpClientFactory implements PooledObjectFactory<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");
return null; throw new ServiceException("FTPServer refused connection");
} }
boolean result = ftpClient.login(StringUtils.defaultString(config.getUsername()), boolean result = ftpClient.login(StringUtils.defaultString(config.getUsername()),
StringUtils.defaultString(config.getPassword())); StringUtils.defaultString(config.getPassword()));
...@@ -51,10 +53,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -51,10 +53,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
// 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); logger.debug("mk objec ftp client {}", ftpClient.toString());
}
return new DefaultPooledObject<>(ftpClient); return new DefaultPooledObject<>(ftpClient);
} }
@Override @Override
...@@ -62,12 +64,12 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -62,12 +64,12 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
FTPClient ftpClient = pooledObject.getObject(); FTPClient ftpClient = pooledObject.getObject();
try { try {
if (ftpClient != null && ftpClient.isConnected()) { if (ftpClient != null && ftpClient.isConnected()) {
logger.debug("destroy ftp client {}", ftpClient.toString());
ftpClient.logout(); ftpClient.logout();
} }
} catch (IOException io) {
io.printStackTrace();
} finally { } finally {
try { try {
if (ftpClient != null && ftpClient.isConnected())
ftpClient.disconnect(); ftpClient.disconnect();
} catch (IOException io) { } catch (IOException io) {
io.printStackTrace(); io.printStackTrace();
...@@ -78,7 +80,9 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -78,7 +80,9 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
@Override @Override
public boolean validateObject(PooledObject<FTPClient> pooledObject) { public boolean validateObject(PooledObject<FTPClient> pooledObject) {
try { try {
return pooledObject.getObject().sendNoOp(); logger.debug("validateObject {}", pooledObject.getObject().toString());
return pooledObject.getObject().sendNoOp() && pooledObject.getObject().isAvailable()
&& pooledObject.getObject().isConnected();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Failed to validate client: " + e, e); throw new RuntimeException("Failed to validate client: " + e, e);
} }
...@@ -86,12 +90,12 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> { ...@@ -86,12 +90,12 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
@Override @Override
public void activateObject(PooledObject<FTPClient> pooledObject) throws Exception { public void activateObject(PooledObject<FTPClient> pooledObject) throws Exception {
logger.debug("activateObject {}", pooledObject.getObject().toString());
} }
@Override @Override
public void passivateObject(PooledObject<FTPClient> pooledObject) throws Exception { public void passivateObject(PooledObject<FTPClient> pooledObject) throws Exception {
logger.debug("passivateObject {}", pooledObject.getObject().toString());
} }
} }
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,62 +2,24 @@ package pwc.taxtech.atms.common.ftp; ...@@ -2,62 +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.springframework.beans.factory.annotation.Value; import pwc.taxtech.atms.exception.ServiceException;
import org.springframework.stereotype.Component;
import java.io.*;
import javax.annotation.PostConstruct; @Service
import java.io.IOException; public class FtpService {
import java.io.InputStream; static String SYMBOL = "/";
@Autowired
FtpConfig config;
@Component String ftpRootPath;
public class FTPClientPool {
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);
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 { public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
String upPath; String upPath;
FTPClient client = getClient(); try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) {
try {
if (StringUtils.isBlank(filePath)) { if (StringUtils.isBlank(filePath)) {
upPath = ftpRootPath; upPath = ftpRootPath;
} else { } else {
...@@ -66,12 +28,10 @@ public class FTPClientPool { ...@@ -66,12 +28,10 @@ public class FTPClientPool {
if (!StringUtils.endsWith(upPath, SYMBOL)) { if (!StringUtils.endsWith(upPath, SYMBOL)) {
upPath = upPath + SYMBOL; upPath = upPath + SYMBOL;
} }
if (!isExist(upPath, client)) { if (!isExist(upPath, wrapFtpClient.ftpClient)) {
mkDir(upPath, client); mkDir(upPath, wrapFtpClient.ftpClient);
} }
client.storeFile(upPath + fileName, inputStream); wrapFtpClient.ftpClient.storeFile(upPath + fileName, inputStream);
} finally {
pool.returnObject(client);
} }
} }
...@@ -83,13 +43,19 @@ public class FTPClientPool { ...@@ -83,13 +43,19 @@ 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(); try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config); OutputStream out = new ByteArrayOutputStream();) {
try { getRootPath(wrapFtpClient);
client.changeWorkingDirectory(ftpRootPath); if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
return StringUtils.isBlank(filePath) ? null : client.retrieveFileStream(filePath); wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath);
} finally { InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath);
//pool.returnObject(client); 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());
} }
} }
private void mkDir(String path, FTPClient ftpClient) throws IOException { private void mkDir(String path, FTPClient ftpClient) throws IOException {
...@@ -118,9 +84,6 @@ public class FTPClientPool { ...@@ -118,9 +84,6 @@ public class FTPClientPool {
} }
} }
public String getFtpRootPath() {
return ftpRootPath;
}
/** /**
* 删除ftp文件 * 删除ftp文件
...@@ -128,22 +91,22 @@ public class FTPClientPool { ...@@ -128,22 +91,22 @@ 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(config);) {
getRootPath(wrapFtpClient);
if (StringUtils.isBlank(filePath)) { if (StringUtils.isBlank(filePath)) {
return; return;
} }
FTPClient client = getClient(); if (!isExist(filePath, wrapFtpClient.ftpClient)) {
try {
if (!isExist(filePath, client)) {
return; return;
} else { } else {
client.deleteFile(filePath); wrapFtpClient.ftpClient.deleteFile(filePath);
} }
} finally {
pool.returnObject(client);
} }
} }
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;
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(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;
}
@Override
public void close() throws Exception {
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; ...@@ -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