博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apache FtpServer与Spring整合
阅读量:6420 次
发布时间:2019-06-23

本文共 11893 字,大约阅读时间需要 39 分钟。

hot3.png

首先在application.xml(spring配置文件)添加Apache Ftpserver属性

  
  
  
  
  
  
  
INSERT INTO FTP_USER (userid, userpassword, homedirectory, enableflag, writepermission, idletime, uploadrate, downloadrate,maxloginnumber,maxloginperip) VALUES ('{userid}', '{userpassword}', '{homedirectory}', '{enableflag}', '{writepermission}', {idletime}, {uploadrate}, {downloadrate},{maxloginnumber},{maxloginperip})
UPDATE FTP_USER SET userpassword='{userpassword}', homedirectory='{homedirectory}', enableflag={enableflag}, writepermission={writepermission}, idletime={idletime}, uploadrate={uploadrate}, downloadrate={downloadrate}, maxloginnumber = {maxloginnumber}, maxloginperip = {maxloginperip} WHERE userid='{userid}'
DELETE FROM FTP_USER WHERE userid = '{userid}'
SELECT userid, userpassword, homedirectory, enableflag, writepermission, idletime, uploadrate, downloadrate,maxloginnumber,maxloginperip FROM FTP_USER WHERE userid = '{userid}'
SELECT userid FROM FTP_USER ORDER BY userid
SELECT userid FROM FTP_USER WHERE userid='{userid}' AND userid='admin'
SELECT userpassword from FTP_USER WHERE userid='{userid}'

在web.xml中添加自己的容器启动关闭监听,目的是操作FTPserver

Web.xml:

com.base.listener.MyServletContextListener

MyServletContextListener.java:

public class MyServletContextListener implements ServletContextListener {    public static final String FTPSERVER_CONTEXT_NAME = "org.apache.ftpserver";        //容器销毁    public void contextDestroyed(ServletContextEvent sce) {        System.out.println("Stopping FtpServer");        //停止FTP server        FtpServer server = (FtpServer) sce.getServletContext().getAttribute(FTPSERVER_CONTEXT_NAME);                if(server != null) {            server.stop();                        sce.getServletContext().removeAttribute(FTPSERVER_CONTEXT_NAME);                        System.out.println("FtpServer stopped");        } else {            System.out.println("No running FtpServer found");        }            }    //容器初始化    public void contextInitialized(ServletContextEvent sce) {        /*在此如不希望容器启动里就开启FTPserver而是希望在页面通过手动开启System.out.println("Starting FtpServer");           WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());                FtpServer server = (FtpServer) ctx.getBean("myServer");                sce.getServletContext().setAttribute(FTPSERVER_CONTEXT_NAME, server);                try {            server.start();            System.out.println("FtpServer started");        } catch (Exception e) {            throw new RuntimeException("Failed to start FtpServer", e);        }*/    	    }}
/**	 * 开启FTPServer	 * @param servletContext 可以从action中获得也可以ServletActionContext.getServletContext();	 */	public boolean startFtpServer(ServletContext servletContext) {		System.out.println("Starting FtpServer");           boolean flg = true;        try {        	FtpServer server = (FtpServer) servletContext.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME);	        	        if(server != null) {	        	if(server.isSuspended())	        		server.resume();//恢复	        		        }else{	        		        	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);	        		        	server = (FtpServer) ctx.getBean("myServer");	        		        	servletContext.setAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME, server);	        	server.start();	        }            System.out.println("FtpServer started");        } catch (Exception e) {        	flg =  false;            throw new RuntimeException("Failed to start FtpServer", e);        }		return flg;	}
//由于apache FTPserver不提供重启功能如果需要手动关闭serve可以将其进行挂起,以方便恢复/**	 * 关闭FTPServer	 * @param servletContext	 * @return	 */	public boolean stopFtpServer(ServletContext servletContext) {		 System.out.println("Stopping FtpServer");	        	        FtpServer server = (FtpServer) servletContext.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME);	        	        if(server != null) {	        	if(!server.isStopped()&&!server.isSuspended())	        		server.suspend();	            	            //servletContext.removeAttribute(FtpServerListener.FTPSERVER_CONTEXT_NAME);	            	            System.out.println("FtpServer suspend");	        } else {	            System.out.println("No running FtpServer found");	        }		return true;	}
//对用户进行操作/**	 * 申请FTP帐号	 * @param userName	 * @param sce 	 * @return	 * @throws FtpException 	 */	public String applyAccount(String userName, ServletContext sce) throws FtpException {		String psw="";		//PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();		//File f = new File(AgentFileService.class.getClassLoader().getResource("users.properties").getFile());		//userManagerFactory.setFile(f);		//DbUserManagerFactory userManagerFactory = new DbUserManagerFactory();        //明文密码保存于配置文件中 发面时改为MD5加密 同时修改配置文件applictionContext-ftp        //userManagerFactory.setPasswordEncryptor(new ClearTextPasswordEncryptor());        //UserManager um = userManagerFactory.createUserManager();        DefaultFtpServer server = (DefaultFtpServer) sce.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME);        if(server!=null){        	UserManager um = server.getUserManager();        	if(um.doesExist(userName)){        		um.delete(userName);        	}        	psw = RandomStringUtils.randomAlphanumeric(8);        	UserFactory userFact = new UserFactory();        	userFact.setName(userName);        	userFact.setPassword(psw);        	userFact.setHomeDirectory("/home/myftpPath");        	userFact.setMaxIdleTime(600000);//10分钟无操作自动断开连接        	List
alist = new ArrayList
(); Authority a = new WritePermission();//写权限 alist.add(a); userFact.setAuthorities(alist); User user = userFact.createUser(); um.save(user); } return psw; }//删除用户DefaultFtpServer server = (DefaultFtpServer) servletContext.getAttribute(MyServletContextListener.FTPSERVER_CONTEXT_NAME); if (server != null) { UserManager um = server.getUserManager(); String[] users = um.getAllUserNames(); if (users != null && users.length > 0) { for (String userName : users) { if (!userName.equals(‘admin’) && um.doesExist(userName)) { um.delete(userName); } } } }

FTP操作监听

FtpService.java:

public class FtpService extends DefaultFtplet{	@Override	public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {				return super.onUploadStart(session, request);	}@Override	public void init(FtpletContext ftpletContext) throws FtpException {		// TODO Auto-generated method stub		super.init(ftpletContext);	}	@Override	public void destroy() {		// TODO Auto-generated method stub		super.destroy();	}	@Override	public FtpletResult onConnect(FtpSession session) throws FtpException,			IOException {		// TODO Auto-generated method stub		return super.onConnect(session);	}	@Override	public FtpletResult onDisconnect(FtpSession session) throws FtpException,			IOException {		// TODO Auto-generated method stub		return super.onDisconnect(session);	}	@Override	public FtpletResult beforeCommand(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.beforeCommand(session, request);	}	@Override	public FtpletResult afterCommand(FtpSession session, FtpRequest request,			FtpReply reply) throws FtpException, IOException {		// TODO Auto-generated method stub		return super.afterCommand(session, request, reply);	}	@Override	public FtpletResult onLogin(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onLogin(session, request);	}	@Override	public FtpletResult onDeleteStart(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onDeleteStart(session, request);	}	@Override	public FtpletResult onDeleteEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onDeleteEnd(session, request);	}	@Override	public FtpletResult onUploadStart(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onUploadStart(session, request);	}	@Override	public FtpletResult onDownloadStart(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onDownloadStart(session, request);	}	@Override	public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onDownloadEnd(session, request);	}	@Override	public FtpletResult onRmdirStart(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onRmdirStart(session, request);	}	@Override	public FtpletResult onRmdirEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onRmdirEnd(session, request);	}	@Override	public FtpletResult onMkdirStart(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onMkdirStart(session, request);	}	@Override	public FtpletResult onMkdirEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onMkdirEnd(session, request);	}	@Override	public FtpletResult onAppendStart(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onAppendStart(session, request);	}	@Override	public FtpletResult onAppendEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onAppendEnd(session, request);	}	@Override	public FtpletResult onUploadUniqueStart(FtpSession session,			FtpRequest request) throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onUploadUniqueStart(session, request);	}	@Override	public FtpletResult onUploadUniqueEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onUploadUniqueEnd(session, request);	}	@Override	public FtpletResult onRenameStart(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onRenameStart(session, request);	}	@Override	public FtpletResult onRenameEnd(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onRenameEnd(session, request);	}	@Override	public FtpletResult onSite(FtpSession session, FtpRequest request)			throws FtpException, IOException {		// TODO Auto-generated method stub		return super.onSite(session, request);	}  	}

转载于:https://my.oschina.net/laigous/blog/120771

你可能感兴趣的文章
Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)
查看>>
人工智能将如何变革视频监控行业?
查看>>
MyBatis Review——开发Dao的方法
查看>>
阿里云容器宣布开放支持Kubernetes托管服务
查看>>
只在UnitTest和WebHost中的出现的关于LogicalCallContext的严重问题
查看>>
Linux_FTP服务器
查看>>
Django里自定义用户登陆及登陆后跳转到登陆前页面的实现
查看>>
技术研发国产化进程加快 看传感器企业如何展示十八般武艺
查看>>
技术助力第三次革命
查看>>
《HTML与CSS入门经典(第8版)》——2.6 总结
查看>>
新手指南:在 Ubuntu 和 Fedora 上安装软件包
查看>>
在 CentOS7.0 上搭建 Chroot 的 Bind DNS 服务器
查看>>
《Python高性能编程》——2.2 Julia集合的介绍
查看>>
大型网站的 HTTPS 实践(二):HTTPS 对性能的影响
查看>>
《Swift 权威指南》——第6章,第6.10节嵌套函数
查看>>
《自己动手做交互系统》——1.3 本章小结
查看>>
Mobile devices bundled with malware?
查看>>
《Node应用程序构建——使用MongoDB和Backbone》一导读
查看>>
《JavaScript面向对象精要》——1.5 访问属性
查看>>
《Python数据可视化编程实战》—— 第 1 章 准备工作环境
查看>>