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
ed7a17d0
Commit
ed7a17d0
authored
May 25, 2018
by
frank.xa.zhang
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'dev' into dev_frank
parents
c5517275
18e37ecc
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
376 additions
and
8 deletions
+376
-8
pom.xml
atms-api/pom.xml
+10
-0
FTPClientConfig.java
...ain/java/pwc/taxtech/atms/common/ftp/FTPClientConfig.java
+103
-0
FTPClientPool.java
.../main/java/pwc/taxtech/atms/common/ftp/FTPClientPool.java
+107
-0
FtpClientFactory.java
...in/java/pwc/taxtech/atms/common/ftp/FtpClientFactory.java
+97
-0
conf.properties
atms-api/src/main/resources/conf/conf.properties
+8
-2
conf_profile_dev.properties
atms-api/src/main/resources/conf/conf_profile_dev.properties
+7
-2
conf_profile_pub.properties
atms-api/src/main/resources/conf/conf_profile_pub.properties
+7
-2
conf_profile_staging.properties
...i/src/main/resources/conf/conf_profile_staging.properties
+7
-2
FTPTest.java
atms-api/src/test/java/pwc/taxtech/atms/common/FTPTest.java
+30
-0
No files found.
atms-api/pom.xml
View file @
ed7a17d0
...
...
@@ -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>
...
...
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FTPClientConfig.java
0 → 100644
View file @
ed7a17d0
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
;
}
}
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FTPClientPool.java
0 → 100644
View file @
ed7a17d0
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
;
}
}
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FtpClientFactory.java
0 → 100644
View file @
ed7a17d0
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
{
}
}
atms-api/src/main/resources/conf/conf.properties
View file @
ed7a17d0
...
...
@@ -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
atms-api/src/main/resources/conf/conf_profile_dev.properties
View file @
ed7a17d0
...
...
@@ -9,4 +9,9 @@ mail_jdbc_password=atmsunittestSQL
web.url
=
http://localhost:8080
#web.url=*
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
atms-api/src/main/resources/conf/conf_profile_pub.properties
View file @
ed7a17d0
...
...
@@ -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
atms-api/src/main/resources/conf/conf_profile_staging.properties
View file @
ed7a17d0
...
...
@@ -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
atms-api/src/test/java/pwc/taxtech/atms/common/FTPTest.java
0 → 100644
View file @
ed7a17d0
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
();
}
}
}
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