Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
ftp_proxy
Overview
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李辅翼
ftp_proxy
Commits
dd1618c1
Commit
dd1618c1
authored
5 years ago
by
李辅翼
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
v1
parent
146a0547
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
164 additions
and
0 deletions
+164
-0
src/main/java/com/hikcreate/ftp/proxy/modules/FtpProxyWebApi.java
+0
-0
src/main/java/com/hikcreate/ftp/proxy/utils/FtpUtil.java
+164
-0
No files found.
src/main/java/com/hikcreate/ftp/proxy/modules/FtpProxyWebApi.java
View file @
dd1618c1
This diff is collapsed.
Click to expand it.
src/main/java/com/hikcreate/ftp/proxy/utils/FtpUtil.java
0 → 100644
View file @
dd1618c1
package
com
.
hikcreate
.
ftp
.
proxy
.
utils
;
import
org.apache.commons.net.ftp.FTP
;
import
org.apache.commons.net.ftp.FTPClient
;
import
org.apache.commons.net.ftp.FTPClientConfig
;
import
org.apache.commons.net.ftp.FTPReply
;
import
org.apache.log4j.Logger
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
/**
* FTP工具类,用于获取ftp连接,上传文件,删除文件等对ftp的操作
* @author hulei
*/
public
class
FtpUtil
{
private
static
Logger
logger
=
Logger
.
getLogger
(
FtpUtil
.
class
);
/**
* ftp 根目录
*/
private
static
final
String
ROOTPATH
=
"/"
;
/**
* ftp 字符集
*/
private
static
final
String
FTP_ENCODE
=
"UTF-8"
;
/**
* ftp 连接默认超时时间
*/
private
static
final
int
DEFAULT_TIMEOUT
=
30
*
1000
;
/**
* FTP客户端
*/
FTPClient
ftpClient
=
null
;
/**
* 获取ftpUtil实例
* @param ftpIp ftp Ip
* @param port ftp 端口
* @param userName ftp用户名
* @param passWord ftp 用户密码
* @return FTPClient
* @throws IOException
*/
public
FtpUtil
(
String
ftpIp
,
int
port
,
String
userName
,
String
passWord
)
{
try
{
ftpClient
=
new
FTPClient
();
ftpClient
.
connect
(
ftpIp
);
ftpClient
.
setDefaultPort
(
port
);
ftpClient
.
setDataTimeout
(
DEFAULT_TIMEOUT
);
ftpClient
.
setControlEncoding
(
FTP_ENCODE
);
boolean
isConnect
=
ftpClient
.
login
(
userName
,
passWord
);
if
(!
isConnect
)
{
logger
.
error
(
"Connection the FtpServer Is Failed!"
);
ftpClient
.
disconnect
();
}
}
catch
(
IOException
e
)
{
logger
.
error
(
"Connection the FtpServer Is Failed! "
+
e
.
toString
());
}
}
/**
* ftp文件上传
* @param ins 文件输入流
* @param uploadPath 上传路径
* @param fileName 文件名
* @return boolean true/false
*/
public
boolean
uploadFile
(
InputStream
ins
,
String
uploadPath
,
String
fileName
)
{
boolean
flag
=
false
;
if
(
ftpClient
!=
null
)
{
try
{
//设置上传文件类型,默认为文本,图片需要设置成二进制,否则图片失真
ftpClient
.
setFileType
(
FTPClient
.
BINARY_FILE_TYPE
);
//切换到上传目录
boolean
change
=
ftpClient
.
changeWorkingDirectory
(
uploadPath
);
if
(
change
)
{
flag
=
ftpClient
.
storeFile
(
new
String
(
fileName
.
getBytes
(
"GBK"
),
"iso-8859-1"
),
ins
);
}
else
{
ftpClient
.
makeDirectory
(
uploadPath
);
ftpClient
.
changeWorkingDirectory
(
uploadPath
);
flag
=
ftpClient
.
storeFile
(
fileName
,
ins
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
return
flag
;
}
/**
* 切换回上级菜单
*/
public
void
changeToParentDirectory
()
{
try
{
ftpClient
.
changeToParentDirectory
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
/**
* 关闭ftp连接
*/
public
void
closeFtp
()
{
try
{
ftpClient
.
logout
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
ftpClient
.
isConnected
())
{
try
{
ftpClient
.
disconnect
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
/**
* 下载文件
* @param remotePath
* @param fileName
* @param os
* @return
*/
public
boolean
downloadPicFile
(
String
remotePath
,
String
fileName
,
OutputStream
os
)
{
boolean
flag
=
false
;
try
{
ftpClient
.
setFileType
(
FTPClient
.
BINARY_FILE_TYPE
);
ftpClient
.
enterLocalPassiveMode
();
flag
=
ftpClient
.
changeWorkingDirectory
(
new
String
(
remotePath
.
getBytes
(),
"ISO-8859-1"
));
if
(
flag
)
{
ftpClient
.
retrieveFile
(
fileName
,
os
);
os
.
flush
();
os
.
close
();
flag
=
true
;
}
else
{
flag
=
false
;
}
ftpClient
.
logout
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
ftpClient
.
isConnected
())
{
try
{
ftpClient
.
disconnect
();
}
catch
(
IOException
ioe
)
{
}
}
}
return
flag
;
}
}
This diff is collapsed.
Click to expand it.
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