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
19dafa28
Commit
19dafa28
authored
Jul 11, 2018
by
neo.wang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DEV] ftp upload and down load file
parent
d307438b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
31 deletions
+58
-31
FTPClientPool.java
.../main/java/pwc/taxtech/atms/common/ftp/FTPClientPool.java
+25
-31
FtpClientFactory.java
...in/java/pwc/taxtech/atms/common/ftp/FtpClientFactory.java
+9
-0
WrapFtpClient.java
.../main/java/pwc/taxtech/atms/common/ftp/WrapFtpClient.java
+24
-0
No files found.
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FTPClientPool.java
View file @
19dafa28
...
@@ -4,21 +4,19 @@ import org.apache.commons.lang3.StringUtils;
...
@@ -4,21 +4,19 @@ 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.apache.commons.pool2.impl.GenericObjectPool
;
import
org.apache.commons.pool2.impl.GenericObjectPoolConfig
;
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.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
pwc.taxtech.atms.exception.ServiceException
;
import
pwc.taxtech.atms.exception.ServiceException
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.PostConstruct
;
import
java.io.BufferedOutputStream
;
import
java.io.*
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
@Component
@Component
public
class
FTPClientPool
{
public
class
FTPClientPool
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
FTPClientPool
.
class
);
private
GenericObjectPoolConfig
config
;
private
GenericObjectPoolConfig
config
;
private
GenericObjectPool
<
FTPClient
>
pool
;
private
GenericObjectPool
<
FTPClient
>
pool
;
private
String
ftpRootPath
;
private
String
ftpRootPath
;
...
@@ -48,12 +46,15 @@ public class FTPClientPool {
...
@@ -48,12 +46,15 @@ public class FTPClientPool {
Runtime
.
getRuntime
().
addShutdownHook
(
new
Thread
(()
->
{
Runtime
.
getRuntime
().
addShutdownHook
(
new
Thread
(()
->
{
pool
.
close
();
pool
.
close
();
}));
}));
ftpRootPath
=
pool
.
borrowObject
().
printWorkingDirectory
();
FTPClient
client
=
pool
.
borrowObject
();
ftpRootPath
=
client
.
printWorkingDirectory
();
pool
.
returnObject
(
client
);
}
}
// this method should only be used in test
// this method should only be used in test
public
FTPClient
getClient
()
throws
Exception
{
public
FTPClient
getClient
()
throws
Exception
{
return
pool
.
borrowObject
();
return
pool
.
borrowObject
();
}
}
/**
/**
...
@@ -66,8 +67,7 @@ public class FTPClientPool {
...
@@ -66,8 +67,7 @@ public class FTPClientPool {
*/
*/
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
=
pool
.
borrowObject
();
try
(
WrapFtpClient
wrapFtpClient
=
new
WrapFtpClient
(
pool
))
{
try
{
if
(
StringUtils
.
isBlank
(
filePath
))
{
if
(
StringUtils
.
isBlank
(
filePath
))
{
upPath
=
ftpRootPath
;
upPath
=
ftpRootPath
;
}
else
{
}
else
{
...
@@ -76,12 +76,10 @@ public class FTPClientPool {
...
@@ -76,12 +76,10 @@ public class FTPClientPool {
if
(!
StringUtils
.
endsWith
(
upPath
,
SYMBOL
))
{
if
(!
StringUtils
.
endsWith
(
upPath
,
SYMBOL
))
{
upPath
=
upPath
+
SYMBOL
;
upPath
=
upPath
+
SYMBOL
;
}
}
if
(!
isExist
(
upPath
,
c
lient
))
{
if
(!
isExist
(
upPath
,
wrapFtpClient
.
ftpC
lient
))
{
mkDir
(
upPath
,
c
lient
);
mkDir
(
upPath
,
wrapFtpClient
.
ftpC
lient
);
}
}
client
.
storeFile
(
upPath
+
fileName
,
inputStream
);
wrapFtpClient
.
ftpClient
.
storeFile
(
upPath
+
fileName
,
inputStream
);
}
finally
{
pool
.
returnObject
(
client
);
}
}
}
}
...
@@ -93,19 +91,18 @@ public class FTPClientPool {
...
@@ -93,19 +91,18 @@ 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
=
pool
.
borrowObject
();
try
(
WrapFtpClient
wrapFtpClient
=
new
WrapFtpClient
(
pool
);
OutputStream
out
=
new
ByteArrayOutputStream
();)
{
if
(
StringUtils
.
isBlank
(
filePath
))
throw
new
ServiceException
(
"file path should not empty"
);
if
(
StringUtils
.
isBlank
(
filePath
))
throw
new
ServiceException
(
"file path should not empty"
);
c
lient
.
changeWorkingDirectory
(
ftpRootPath
);
wrapFtpClient
.
ftpC
lient
.
changeWorkingDirectory
(
ftpRootPath
);
try
(
InputStream
in
=
client
.
retrieveFileStream
(
filePath
);
OutputStream
out
=
new
ByteArrayOutputStream
();){
InputStream
in
=
wrapFtpClient
.
ftpClient
.
retrieveFileStream
(
filePath
);
int
len
=
0
;
int
len
=
0
;
byte
[]
bytes
=
new
byte
[
1024
];
byte
[]
bytes
=
new
byte
[
1024
];
while
((
len
=
in
.
read
(
bytes
))
!=
-
1
)
{
while
((
len
=
in
.
read
(
bytes
))
!=
-
1
)
{
out
.
write
(
bytes
,
0
,
len
);
out
.
write
(
bytes
,
0
,
len
);
}
}
return
new
ByteArrayInputStream
(((
ByteArrayOutputStream
)
out
).
toByteArray
());
return
new
ByteArrayInputStream
(((
ByteArrayOutputStream
)
out
).
toByteArray
());
}
finally
{
pool
.
returnObject
(
client
);
}
}
}
}
private
void
mkDir
(
String
path
,
FTPClient
ftpClient
)
throws
IOException
{
private
void
mkDir
(
String
path
,
FTPClient
ftpClient
)
throws
IOException
{
...
@@ -144,18 +141,15 @@ public class FTPClientPool {
...
@@ -144,18 +141,15 @@ public class FTPClientPool {
* @param filePath
* @param filePath
*/
*/
public
void
delete
(
String
filePath
)
throws
Exception
{
public
void
delete
(
String
filePath
)
throws
Exception
{
if
(
StringUtils
.
isBlank
(
filePath
))
{
try
(
WrapFtpClient
wrapFtpClient
=
new
WrapFtpClient
(
pool
);)
{
return
;
if
(
StringUtils
.
isBlank
(
filePath
))
{
}
return
;
FTPClient
client
=
pool
.
borrowObject
();
}
try
{
if
(!
isExist
(
filePath
,
wrapFtpClient
.
ftpClient
))
{
if
(!
isExist
(
filePath
,
client
))
{
return
;
return
;
}
else
{
}
else
{
c
lient
.
deleteFile
(
filePath
);
wrapFtpClient
.
ftpC
lient
.
deleteFile
(
filePath
);
}
}
}
finally
{
pool
.
returnObject
(
client
);
}
}
}
}
...
...
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FtpClientFactory.java
View file @
19dafa28
...
@@ -21,6 +21,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
...
@@ -21,6 +21,10 @@ 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
{
FTPClient
ftpClient
=
new
FTPClient
();
FTPClient
ftpClient
=
new
FTPClient
();
...
@@ -77,6 +81,11 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
...
@@ -77,6 +81,11 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
public
boolean
validateObject
(
PooledObject
<
FTPClient
>
pooledObject
)
{
public
boolean
validateObject
(
PooledObject
<
FTPClient
>
pooledObject
)
{
try
{
try
{
logger
.
debug
(
"validate object ftp client {} "
,
pooledObject
.
getObject
().
toString
());
logger
.
debug
(
"validate object ftp client {} "
,
pooledObject
.
getObject
().
toString
());
FTPClient
ftpClient
=
pooledObject
.
getObject
();
if
(
ftpClient
==
null
||
!
ftpClient
.
isConnected
())
{
return
false
;
}
ftpClient
.
changeWorkingDirectory
(
"/"
);
return
pooledObject
.
getObject
().
sendNoOp
();
return
pooledObject
.
getObject
().
sendNoOp
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Failed to validate client: "
+
e
,
e
);
throw
new
RuntimeException
(
"Failed to validate client: "
+
e
,
e
);
...
...
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/WrapFtpClient.java
0 → 100644
View file @
19dafa28
package
pwc
.
taxtech
.
atms
.
common
.
ftp
;
import
org.apache.commons.net.ftp.FTPClient
;
import
org.apache.commons.pool2.impl.GenericObjectPool
;
public
class
WrapFtpClient
implements
AutoCloseable
{
FTPClient
ftpClient
;
GenericObjectPool
<
FTPClient
>
pool
;
public
WrapFtpClient
(
GenericObjectPool
<
FTPClient
>
pool
)
throws
Exception
{
this
.
pool
=
pool
;
this
.
ftpClient
=
pool
.
borrowObject
();
}
public
FTPClient
getFtpClient
()
{
return
ftpClient
;
}
@Override
public
void
close
()
throws
Exception
{
ftpClient
.
completePendingCommand
();
pool
.
returnObject
(
ftpClient
);
}
}
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