Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
T
traffic-front
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangxiaoming
traffic-front
Commits
0bea477b
Commit
0bea477b
authored
Jul 11, 2018
by
neo.wang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DEV] use wrapclient with short ftpclient connection
parent
2e61cb2f
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
133 additions
and
102 deletions
+133
-102
FtpConfig.java
.../src/main/java/pwc/taxtech/atms/common/ftp/FtpConfig.java
+50
-0
FtpService.java
...src/main/java/pwc/taxtech/atms/common/ftp/FtpService.java
+18
-65
WrapFtpClient.java
.../main/java/pwc/taxtech/atms/common/ftp/WrapFtpClient.java
+41
-8
TemplateController.java
.../java/pwc/taxtech/atms/controller/TemplateController.java
+7
-13
TemplateGroupController.java
.../pwc/taxtech/atms/controller/TemplateGroupController.java
+7
-7
FTPFileSystemServiceImpl.java
...c/taxtech/atms/service/impl/FTPFileSystemServiceImpl.java
+4
-4
FTPTest.java
atms-api/src/test/java/pwc/taxtech/atms/common/FTPTest.java
+6
-5
No files found.
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FtpConfig.java
0 → 100644
View file @
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
;
}
}
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/F
TPClientPool
.java
→
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/F
tpService
.java
View file @
0bea477b
...
...
@@ -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
();
}
}
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/WrapFtpClient.java
View file @
0bea477b
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
();
}
}
}
}
atms-api/src/main/java/pwc/taxtech/atms/controller/TemplateController.java
View file @
0bea477b
...
...
@@ -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
F
TPClientPool
ftpClientPool
;
F
tpService
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
=
ftp
ClientPool
.
download
(
templatePath
);
inputStream
=
ftp
Service
.
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
{
ftp
ClientPool
.
delete
(
result
.
getData
());
ftp
Service
.
delete
(
result
.
getData
());
}
catch
(
Exception
e
)
{
}
}
...
...
atms-api/src/main/java/pwc/taxtech/atms/controller/TemplateGroupController.java
View file @
0bea477b
...
...
@@ -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
F
TPClientPool
ftpClientPool
;
F
tpService
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
)
...
...
atms-api/src/main/java/pwc/taxtech/atms/service/impl/FTPFileSystemServiceImpl.java
View file @
0bea477b
...
...
@@ -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.F
TPClientPool
;
import
pwc.taxtech.atms.common.ftp.F
tpService
;
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
F
TPClientPool
ftpClientPool
;
private
F
tpService
ftpService
;
@Override
public
String
uploadUserTemplate
(
String
fileName
,
InputStream
inputStream
)
throws
Exception
{
ftp
ClientPool
.
upload
(
USER_TEMPLATE_PATH
,
fileName
,
inputStream
);
ftp
Service
.
upload
(
USER_TEMPLATE_PATH
,
fileName
,
inputStream
);
return
USER_TEMPLATE_PATH
+
fileName
;
}
@Override
public
InputStream
downloadUserTemplate
(
String
filePath
)
throws
Exception
{
return
ftp
ClientPool
.
download
(
filePath
);
return
ftp
Service
.
download
(
filePath
);
}
}
atms-api/src/test/java/pwc/taxtech/atms/common/FTPTest.java
View file @
0bea477b
...
...
@@ -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.F
TPClientPool
;
import
pwc.taxtech.atms.common.ftp.F
tpService
;
import
java.io.File
;
import
java.io.FileInputStream
;
public
class
FTPTest
extends
CommonIT
{
@Autowired
F
TPClientPool
ftpClientPool
;
F
tpService
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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment