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
95ae2245
Commit
95ae2245
authored
Jul 10, 2018
by
neo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DEV] ftp download transfer stream us byte array
parent
89c2475c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
35 deletions
+46
-35
FTPClientPool.java
.../main/java/pwc/taxtech/atms/common/ftp/FTPClientPool.java
+24
-8
FtpClientFactory.java
...in/java/pwc/taxtech/atms/common/ftp/FtpClientFactory.java
+22
-27
No files found.
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FTPClientPool.java
View file @
95ae2245
...
@@ -6,11 +6,16 @@ import org.apache.commons.pool2.impl.GenericObjectPool;
...
@@ -6,11 +6,16 @@ import org.apache.commons.pool2.impl.GenericObjectPool;
import
org.apache.commons.pool2.impl.GenericObjectPoolConfig
;
import
org.apache.commons.pool2.impl.GenericObjectPoolConfig
;
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
javax.annotation.PostConstruct
;
import
javax.annotation.PostConstruct
;
import
java.io.BufferedOutputStream
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
@Component
@Component
public
class
FTPClientPool
{
public
class
FTPClientPool
{
...
@@ -39,11 +44,16 @@ public class FTPClientPool {
...
@@ -39,11 +44,16 @@ public class FTPClientPool {
clientConfig
.
setPassword
(
ftpPwd
);
clientConfig
.
setPassword
(
ftpPwd
);
ftpClientConfig
=
clientConfig
;
ftpClientConfig
=
clientConfig
;
pool
=
new
GenericObjectPool
<>(
new
FtpClientFactory
(
clientConfig
),
config
);
pool
=
new
GenericObjectPool
<>(
new
FtpClientFactory
(
clientConfig
),
config
);
Runtime
.
getRuntime
().
addShutdownHook
(
new
Thread
(()
->
{
pool
.
close
();
}));
ftpRootPath
=
pool
.
borrowObject
().
printWorkingDirectory
();
ftpRootPath
=
pool
.
borrowObject
().
printWorkingDirectory
();
}
}
// this method should only be used in test
public
FTPClient
getClient
()
throws
Exception
{
public
FTPClient
getClient
()
throws
Exception
{
return
pool
.
borrowObject
();
return
pool
.
borrowObject
();
}
}
/**
/**
...
@@ -56,7 +66,7 @@ public class FTPClientPool {
...
@@ -56,7 +66,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
=
getClien
t
();
FTPClient
client
=
pool
.
borrowObjec
t
();
try
{
try
{
if
(
StringUtils
.
isBlank
(
filePath
))
{
if
(
StringUtils
.
isBlank
(
filePath
))
{
upPath
=
ftpRootPath
;
upPath
=
ftpRootPath
;
...
@@ -83,12 +93,18 @@ public class FTPClientPool {
...
@@ -83,12 +93,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
=
getClient
();
FTPClient
client
=
pool
.
borrowObject
();
try
{
if
(
StringUtils
.
isBlank
(
filePath
))
throw
new
ServiceException
(
"file path should not empty"
);
client
.
changeWorkingDirectory
(
ftpRootPath
);
client
.
changeWorkingDirectory
(
ftpRootPath
);
return
StringUtils
.
isBlank
(
filePath
)
?
null
:
client
.
retrieveFileStream
(
filePath
);
try
(
InputStream
in
=
client
.
retrieveFileStream
(
filePath
);
OutputStream
out
=
new
ByteArrayOutputStream
();){
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
());
}
finally
{
}
finally
{
//
pool.returnObject(client);
pool
.
returnObject
(
client
);
}
}
}
}
...
@@ -131,7 +147,7 @@ public class FTPClientPool {
...
@@ -131,7 +147,7 @@ public class FTPClientPool {
if
(
StringUtils
.
isBlank
(
filePath
))
{
if
(
StringUtils
.
isBlank
(
filePath
))
{
return
;
return
;
}
}
FTPClient
client
=
getClien
t
();
FTPClient
client
=
pool
.
borrowObjec
t
();
try
{
try
{
if
(!
isExist
(
filePath
,
client
))
{
if
(!
isExist
(
filePath
,
client
))
{
return
;
return
;
...
...
atms-api/src/main/java/pwc/taxtech/atms/common/ftp/FtpClientFactory.java
View file @
95ae2245
...
@@ -21,40 +21,36 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
...
@@ -21,40 +21,36 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
this
.
config
=
config
;
this
.
config
=
config
;
}
}
@Override
@Override
public
PooledObject
<
FTPClient
>
makeObject
()
throws
Exception
{
public
PooledObject
<
FTPClient
>
makeObject
()
throws
Exception
{
FTPClient
ftpClient
=
new
FTPClient
();
FTPClient
ftpClient
=
new
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"
);
throw
new
ServiceException
(
"FTPServer refused connection"
);
return
null
;
}
}
boolean
result
=
ftpClient
.
login
(
StringUtils
.
defaultString
(
config
.
getUsername
()),
boolean
result
=
ftpClient
.
login
(
StringUtils
.
defaultString
(
config
.
getUsername
()),
StringUtils
.
defaultString
(
config
.
getPassword
()));
StringUtils
.
defaultString
(
config
.
getPassword
()));
if
(!
result
)
{
if
(!
result
)
{
throw
new
ServiceException
(
"ftpClient登陆失败! userName:"
+
config
.
getUsername
()
+
" ; password:"
+
config
.
getPassword
());
throw
new
ServiceException
(
"ftpClient登陆失败! userName:"
+
config
.
getUsername
()
+
" ; password:"
+
config
.
getPassword
());
}
}
ftpClient
.
setFileType
(
FTP
.
BINARY_FILE_TYPE
);
ftpClient
.
setFileType
(
FTP
.
BINARY_FILE_TYPE
);
if
(
null
!=
config
.
getTransferFileType
())
{
if
(
null
!=
config
.
getTransferFileType
())
{
ftpClient
.
setFileType
(
config
.
getTransferFileType
());
ftpClient
.
setFileType
(
config
.
getTransferFileType
());
}
}
ftpClient
.
setBufferSize
(
1024
);
ftpClient
.
setBufferSize
(
1024
);
ftpClient
.
setControlEncoding
(
StringUtils
.
defaultString
(
config
.
getEncoding
(),
"UTF-8"
));
ftpClient
.
setControlEncoding
(
StringUtils
.
defaultString
(
config
.
getEncoding
(),
"UTF-8"
));
ftpClient
.
enterLocalPassiveMode
();
ftpClient
.
enterLocalPassiveMode
();
// 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
);
}
return
new
DefaultPooledObject
<>(
ftpClient
);
return
new
DefaultPooledObject
<>(
ftpClient
);
}
}
@Override
@Override
...
@@ -64,11 +60,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
...
@@ -64,11 +60,10 @@ public class FtpClientFactory implements PooledObjectFactory<FTPClient> {
if
(
ftpClient
!=
null
&&
ftpClient
.
isConnected
())
{
if
(
ftpClient
!=
null
&&
ftpClient
.
isConnected
())
{
ftpClient
.
logout
();
ftpClient
.
logout
();
}
}
}
catch
(
IOException
io
)
{
io
.
printStackTrace
();
}
finally
{
}
finally
{
try
{
try
{
ftpClient
.
disconnect
();
if
(
ftpClient
!=
null
&&
ftpClient
.
isConnected
())
ftpClient
.
disconnect
();
}
catch
(
IOException
io
)
{
}
catch
(
IOException
io
)
{
io
.
printStackTrace
();
io
.
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