首先在application.xml(spring配置文件)添加Apache Ftpserver属性
01 |
< server xmlns = "http://mina.apache.org/ftpserver/spring/v1" |
02 |
xmlns:beans = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 |
xsi:schemaLocation=" |
05 |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd |
06 |
http://mina.apache.org/ftpserver/spring/v1 http://mina.apache.org/ftpserver/ftpserver-1.0.xsd |
07 |
" |
08 |
id = "myServer" |
09 |
max-logins = "500" |
10 |
anon-enabled = "false" |
11 |
max-login-failures = "20" |
12 |
login-failure-delay = "30" |
13 |
> |
14 |
< listeners > |
15 |
<!-- port为FTP端口默认为21 idle-timeout为登录失效时间单位s--> |
16 |
< nio-listener name = "default" port = "2121" idle-timeout = "300" > |
17 |
<!--<ssl> ssl数据传输加密 |
18 |
<keystore file="ftpserver.jks" password="password" /> |
19 |
</ssl>--> |
20 |
</ nio-listener > |
21 |
</ listeners > |
22 |
<!--FTP操作监听--> |
23 |
< ftplets > |
24 |
< ftplet name = "ftplet1" > |
25 |
< beans:bean class = "com.agnet.service.FtpService" > |
26 |
<!--属性注入--> |
27 |
<!-- <beans:property name="foo" value="123" />--> |
28 |
</ beans:bean > |
29 |
</ ftplet > |
30 |
</ ftplets > |
31 |
<!--properties文件存放用户信息 |
32 |
encrypt-passwords 默认为MD5加密,clear为明文密码,salted为加强MD5加密--> |
33 |
<!--<file-user-manager file="users.properties" encrypt-passwords="clear"/>--> |
34 |
<!-- 数据库存放用户信息 |
35 |
建表SQL |
36 |
CREATE TABLE FTP_USER ( |
37 |
userid VARCHAR(64) NOT NULL PRIMARY KEY, |
38 |
userpassword VARCHAR(64), |
39 |
homedirectory VARCHAR(128) NOT NULL, |
40 |
enableflag char(1) DEFAULT 1 check (enableflag in(0,1)), |
41 |
writepermission char(1) DEFAULT 1 check (writepermission in(0,1)), |
42 |
idletime INT DEFAULT 0, |
43 |
uploadrate INT DEFAULT 0, |
44 |
downloadrate INT DEFAULT 0, |
45 |
maxloginnumber INT DEFAULT 0, |
46 |
maxloginperip INT DEFAULT 0 |
47 |
); |
48 |
--> |
49 |
< db-user-manager encrypt-passwords = "salted" > |
50 |
< data-source > |
51 |
< beans:bean class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method = "close" > |
52 |
< beans:property name = "driverClass" value = "${common.c3p0.driver}" /> |
53 |
< beans:property name = "jdbcUrl" value = "${common.c3p0.url}" /> |
54 |
< beans:property name = "user" value = "${common.c3p0.username}" /> |
55 |
< beans:property name = "password" value = "${common.c3p0.password}" /> |
56 |
</ beans:bean > |
57 |
</ data-source > |
58 |
<!--当为数据库存放时 apache FTPserver APi 函数需要调用的sql --> |
59 |
< insert-user >INSERT INTO FTP_USER (userid, userpassword, |
60 |
homedirectory, enableflag, writepermission, idletime, uploadrate, |
61 |
downloadrate,maxloginnumber,maxloginperip) VALUES ('{userid}', '{userpassword}', '{homedirectory}', |
62 |
'{enableflag}', '{writepermission}', {idletime}, {uploadrate}, |
63 |
{downloadrate},{maxloginnumber},{maxloginperip})</ insert-user > |
64 |
< update-user > |
65 |
UPDATE FTP_USER SET |
66 |
userpassword='{userpassword}', |
67 |
homedirectory='{homedirectory}', |
68 |
enableflag={enableflag}, |
69 |
writepermission={writepermission}, |
70 |
idletime={idletime}, |
71 |
uploadrate={uploadrate}, |
72 |
downloadrate={downloadrate}, |
73 |
maxloginnumber = {maxloginnumber}, |
74 |
maxloginperip = {maxloginperip} |
75 |
WHERE userid='{userid}' |
76 |
</ update-user > |
77 |
< delete-user > |
78 |
DELETE FROM FTP_USER WHERE userid = '{userid}' |
79 |
</ delete-user > |
80 |
< select-user > |
81 |
SELECT userid, userpassword, homedirectory, |
82 |
enableflag, writepermission, idletime, uploadrate, downloadrate,maxloginnumber,maxloginperip |
83 |
FROM FTP_USER |
84 |
WHERE userid = '{userid}' |
85 |
</ select-user > |
86 |
< select-all-users > |
87 |
SELECT userid FROM FTP_USER ORDER BY userid |
88 |
</ select-all-users > |
89 |
< is-admin > |
90 |
SELECT userid |
91 |
FROM FTP_USER |
92 |
WHERE userid='{userid}' AND userid='admin' |
93 |
</ is-admin > |
94 |
< authenticate >SELECT userpassword from FTP_USER WHERE userid='{userid}'</ authenticate > |
95 |
</ db-user-manager > |
96 |
</ server > |
在web.xml中添加自己的容器启动关闭监听,目的是操作FTPserver
Web.xml:
1 |
<!-- 容器 初始化监听 --> |
2 |
< listener > |
3 |
< listener-class >com.base.listener.MyServletContextListener</ listener-class > </ listener > |
MyServletContextListener.java:
01 |
public class MyServletContextListener implements ServletContextListener { |
02 |
|
03 |
public static final String FTPSERVER_CONTEXT_NAME = "org.apache.ftpserver" ; |
04 |
|
05 |
//容器销毁 |
06 |
public void contextDestroyed(ServletContextEvent sce) { |
07 |
System.out.println( "Stopping FtpServer" ); |
08 |
//停止FTP server |
09 |
FtpServer server = (FtpServer) sce.getServletContext().getAttribute(FTPSERVER_CONTEXT_NAME); |
10 |
|
11 |
if (server != null ) { |
12 |
server.stop(); |
13 |
|
14 |
sce.getServletContext().removeAttribute(FTPSERVER_CONTEXT_NAME); |
15 |
|
16 |
System.out.println( "FtpServer stopped" ); |
17 |
} else { |
18 |
System.out.println( "No running FtpServer found" ); |
19 |
} |
20 |
|
21 |
} |
22 |
|
23 |
//容器初始化 |
24 |
public void contextInitialized(ServletContextEvent sce) { |
25 |
/*在此如不希望容器启动里就开启FTPserver而是希望在页面通过手动开启 |
26 |
System.out.println("Starting FtpServer"); |
27 |
|
28 |
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); |
29 |
|
30 |
FtpServer server = (FtpServer) ctx.getBean("myServer"); |
31 |
|
32 |
sce.getServletContext().setAttribute(FTPSERVER_CONTEXT_NAME, server); |
33 |
|
34 |
try { |
35 |
server.start(); |
36 |
System.out.println("FtpServer started"); |
37 |
} catch (Exception e) { |
38 |
throw new RuntimeException("Failed to start FtpServer", e); |
39 |
}*/ |
40 |
} |
41 |
|
42 |
} |
01 |
/** |
02 |
* 开启FTPServer |
03 |
* @param servletContext 可以从action中获得也可以ServletActionContext.getServletContext(); |
04 |
*/ |
05 |
public boolean startFtpServer(ServletContext servletContext) { |
06 |
System.out.println( "Starting FtpServer" ); |
07 |
|
08 |
boolean flg = true ; |
09 |
try { |
10 |
FtpServer server = (FtpServer) servletContext.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME); |
11 |
|
12 |
if (server != null ) { |
13 |
if (server.isSuspended()) |
14 |
server.resume(); //恢复 |
15 |
|
16 |
} else { |
17 |
|
18 |
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); |
19 |
|
20 |
server = (FtpServer) ctx.getBean( "myServer" ); |
21 |
|
22 |
servletContext.setAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME, server); |
23 |
server.start(); |
24 |
} |
25 |
System.out.println( "FtpServer started" ); |
26 |
} catch (Exception e) { |
27 |
flg = false ; |
28 |
throw new RuntimeException( "Failed to start FtpServer" , e); |
29 |
} |
30 |
return flg; |
31 |
} |
01 |
//由于apache FTPserver不提供重启功能如果需要手动关闭serve可以将其进行挂起,以方便恢复 |
02 |
/** |
03 |
* 关闭FTPServer |
04 |
* @param servletContext |
05 |
* @return |
06 |
*/ |
07 |
public boolean stopFtpServer(ServletContext servletContext) { |
08 |
System.out.println( "Stopping FtpServer" ); |
09 |
|
10 |
FtpServer server = (FtpServer) servletContext.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME); |
11 |
|
12 |
if (server != null ) { |
13 |
if (!server.isStopped()&&!server.isSuspended()) |
14 |
server.suspend(); |
15 |
|
16 |
//servletContext.removeAttribute(FtpServerListener.FTPSERVER_CONTEXT_NAME); |
17 |
|
18 |
System.out.println( "FtpServer suspend" ); |
19 |
} else { |
20 |
System.out.println( "No running FtpServer found" ); |
21 |
} |
22 |
return true ; |
23 |
} |
01 |
//对用户进行操作 |
02 |
/** |
03 |
* 申请FTP帐号 |
04 |
* @param userName |
05 |
* @param sce |
06 |
* @return |
07 |
* @throws FtpException |
08 |
*/ |
09 |
public String applyAccount(String userName, ServletContext sce) throws FtpException { |
10 |
String psw= "" ; |
11 |
//PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); |
12 |
//File f = new File(AgentFileService.class.getClassLoader().getResource("users.properties").getFile()); |
13 |
//userManagerFactory.setFile(f); |
14 |
//DbUserManagerFactory userManagerFactory = new DbUserManagerFactory(); |
15 |
//明文密码保存于配置文件中 发面时改为MD5加密 同时修改配置文件applictionContext-ftp |
16 |
//userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor()); |
17 |
//UserManager um = userManagerFactory.createUserManager(); |
18 |
DefaultFtpServer server = (DefaultFtpServer) sce.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME); |
19 |
if (server!= null ){ |
20 |
UserManager um = server.getUserManager(); |
21 |
if (um.doesExist(userName)){ |
22 |
um.delete(userName); |
23 |
} |
24 |
psw = RandomStringUtils.randomAlphanumeric( 8 ); |
25 |
UserFactory userFact = new UserFactory(); |
26 |
userFact.setName(userName); |
27 |
userFact.setPassword(psw); |
28 |
userFact.setHomeDirectory( "/home/myftpPath" ); |
29 |
userFact.setMaxIdleTime( 600000 ); //10分钟无操作自动断开连接 |
30 |
List<Authority> alist = new ArrayList<Authority>(); |
31 |
Authority a = new WritePermission(); //写权限 |
32 |
alist.add(a); |
33 |
userFact.setAuthorities(alist); |
34 |
User user = userFact.createUser(); |
35 |
um.save(user); |
36 |
} |
37 |
return psw; |
38 |
} |
39 |
|
40 |
//删除用户 |
41 |
|
42 |
DefaultFtpServer server = (DefaultFtpServer) servletContext.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME); |
43 |
if (server != null ) { |
44 |
UserManager um = server.getUserManager(); |
45 |
String[] users = um.getAllUserNames(); |
46 |
if (users != null && users.length > 0 ) { |
47 |
for (String userName : users) { |
48 |
if (!userName.equals(‘admin’) |
49 |
&& um.doesExist(userName)) { |
50 |
um.delete(userName); |
51 |
} |
52 |
} |
53 |
} |
54 |
} |
FTP操作监听
FtpService.java:
001 |
public class FtpService extends DefaultFtplet{ |
002 |
@Override |
003 |
public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) |
004 |
throws FtpException, IOException { |
005 |
|
006 |
return super .onUploadStart(session, request); |
007 |
} |
008 |
|
009 |
@Override |
010 |
public void init(FtpletContext ftpletContext) throws FtpException { |
011 |
// TODO Auto-generated method stub |
012 |
super .init(ftpletContext); |
013 |
} |
014 |
|
015 |
@Override |
016 |
public void destroy() { |
017 |
// TODO Auto-generated method stub |
018 |
super .destroy(); |
019 |
} |
020 |
|
021 |
@Override |
022 |
public FtpletResult onConnect(FtpSession session) throws FtpException, |
023 |
IOException { |
024 |
// TODO Auto-generated method stub |
025 |
return super .onConnect(session); |
026 |
} |
027 |
|
028 |
@Override |
029 |
public FtpletResult onDisconnect(FtpSession session) throws FtpException, |
030 |
IOException { |
031 |
// TODO Auto-generated method stub |
032 |
return super .onDisconnect(session); |
033 |
} |
034 |
|
035 |
@Override |
036 |
public FtpletResult beforeCommand(FtpSession session, FtpRequest request) |
037 |
throws FtpException, IOException { |
038 |
// TODO Auto-generated method stub |
039 |
return super .beforeCommand(session, request); |
040 |
} |
041 |
|
042 |
@Override |
043 |
public FtpletResult afterCommand(FtpSession session, FtpRequest request, |
044 |
FtpReply reply) throws FtpException, IOException { |
045 |
// TODO Auto-generated method stub |
046 |
return super .afterCommand(session, request, reply); |
047 |
} |
048 |
|
049 |
@Override |
050 |
public FtpletResult onLogin(FtpSession session, FtpRequest request) |
051 |
throws FtpException, IOException { |
052 |
// TODO Auto-generated method stub |
053 |
return super .onLogin(session, request); |
054 |
} |
055 |
|
056 |
@Override |
057 |
public FtpletResult onDeleteStart(FtpSession session, FtpRequest request) |
058 |
throws FtpException, IOException { |
059 |
// TODO Auto-generated method stub |
060 |
return super .onDeleteStart(session, request); |
061 |
} |
062 |
|
063 |
@Override |
064 |
public FtpletResult onDeleteEnd(FtpSession session, FtpRequest request) |
065 |
throws FtpException, IOException { |
066 |
// TODO Auto-generated method stub |
067 |
return super .onDeleteEnd(session, request); |
068 |
} |
069 |
|
070 |
@Override |
071 |
public FtpletResult onUploadStart(FtpSession session, FtpRequest request) |
072 |
throws FtpException, IOException { |
073 |
// TODO Auto-generated method stub |
074 |
return super .onUploadStart(session, request); |
075 |
} |
076 |
|
077 |
@Override |
078 |
public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) |
079 |
throws FtpException, IOException { |
080 |
// TODO Auto-generated method stub |
081 |
return super .onDownloadStart(session, request); |
082 |
} |
083 |
|
084 |
@Override |
085 |
public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) |
086 |
throws FtpException, IOException { |
087 |
// TODO Auto-generated method stub |
088 |
return super .onDownloadEnd(session, request); |
089 |
} |
090 |
|
091 |
@Override |
092 |
public FtpletResult onRmdirStart(FtpSession session, FtpRequest request) |
093 |
throws FtpException, IOException { |
094 |
// TODO Auto-generated method stub |
095 |
return super .onRmdirStart(session, request); |
096 |
} |
097 |
|
098 |
@Override |
099 |
public FtpletResult onRmdirEnd(FtpSession session, FtpRequest request) |
100 |
throws FtpException, IOException { |
101 |
// TODO Auto-generated method stub |
102 |
return super .onRmdirEnd(session, request); |
103 |
} |
104 |
|
105 |
@Override |
106 |
public FtpletResult onMkdirStart(FtpSession session, FtpRequest request) |
107 |
throws FtpException, IOException { |
108 |
// TODO Auto-generated method stub |
109 |
return super .onMkdirStart(session, request); |
110 |
} |
111 |
|
112 |
@Override |
113 |
public FtpletResult onMkdirEnd(FtpSession session, FtpRequest request) |
114 |
throws FtpException, IOException { |
115 |
// TODO Auto-generated method stub |
116 |
return super .onMkdirEnd(session, request); |
117 |
} |
118 |
|
119 |
@Override |
120 |
public FtpletResult onAppendStart(FtpSession session, FtpRequest request) |
121 |
throws FtpException, IOException { |
122 |
// TODO Auto-generated method stub |
123 |
return super .onAppendStart(session, request); |
124 |
} |
125 |
|
126 |
@Override |
127 |
public FtpletResult onAppendEnd(FtpSession session, FtpRequest request) |
128 |
throws FtpException, IOException { |
129 |
// TODO Auto-generated method stub |
130 |
return super .onAppendEnd(session, request); |
131 |
} |
132 |
|
133 |
@Override |
134 |
public FtpletResult onUploadUniqueStart(FtpSession session, |
135 |
FtpRequest request) throws FtpException, IOException { |
136 |
// TODO Auto-generated method stub |
137 |
return super .onUploadUniqueStart(session, request); |
138 |
} |
139 |
|
140 |
@Override |
141 |
public FtpletResult onUploadUniqueEnd(FtpSession session, FtpRequest request) |
142 |
throws FtpException, IOException { |
143 |
// TODO Auto-generated method stub |
144 |
return super .onUploadUniqueEnd(session, request); |
145 |
} |
146 |
|
147 |
@Override |
148 |
public FtpletResult onRenameStart(FtpSession session, FtpRequest request) |
149 |
throws FtpException, IOException { |
150 |
// TODO Auto-generated method stub |
151 |
return super .onRenameStart(session, request); |
152 |
} |
153 |
|
154 |
@Override |
155 |
public FtpletResult onRenameEnd(FtpSession session, FtpRequest request) |
156 |
throws FtpException, IOException { |
157 |
// TODO Auto-generated method stub |
158 |
return super .onRenameEnd(session, request); |
159 |
} |
160 |
|
161 |
@Override |
162 |
public FtpletResult onSite(FtpSession session, FtpRequest request) |
163 |
throws FtpException, IOException { |
164 |
// TODO Auto-generated method stub |
165 |
return super .onSite(session, request); |
166 |
} |
167 |
|
168 |
} |