当前位置 主页 > 网站技术 > 代码类 > 最大化 缩小

    springboot整合apache ftpserver详细教程(推荐)(3)

    栏目:代码类 时间:2020-01-14 18:08

    4、配置监听器,使spring容器启动时启动ftpserver,在spring容器销毁时停止ftpserver

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    @WebListener
    public class FtpServerListener implements ServletContextListener {
     private static final Logger logger = LoggerFactory.getLogger(MyFtpServer.class);
     private static final String SERVER_NAME="FTP-SERVER";
     @Autowired
     private MyFtpServer server;
     //容器关闭时调用方法stop ftpServer
     public void contextDestroyed(ServletContextEvent sce) {
    //  WebApplicationContext ctx= WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    //  MyFtpServer server=(MyFtpServer)ctx.getServletContext().getAttribute(SERVER_NAME);
      server.stop();
      sce.getServletContext().removeAttribute(SERVER_NAME);
      logger.info("Apache Ftp server is stoped!");
     }
     //容器初始化调用方法start ftpServer
     public void contextInitialized(ServletContextEvent sce) {
    //  WebApplicationContext ctx= WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
    //  MyFtpServer server=(MyFtpServer) ctx.getBean("MyFtp");
      sce.getServletContext().setAttribute(SERVER_NAME,server);
      try {
       //项目启动时已经加载好了
       server.start();
       logger.info("Apache Ftp server is started!");
      } catch (Exception e){
       e.printStackTrace();
       throw new RuntimeException("Apache Ftp server start failed!", e);
      }
     }
    }

    5、通过继承DefaultFtplet抽象类来实现一些自定义用户事件(我这里只是举例)

    import org.apache.ftpserver.ftplet.*;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import java.io.IOException;
    public class MyFtpPlet extends DefaultFtplet {
     private static final Logger logger = LoggerFactory.getLogger(MyFtpPlet.class);
     @Override
     public FtpletResult onUploadStart(FtpSession session, FtpRequest request)
       throws FtpException, IOException {
      //获取上传文件的上传路径
      String path = session.getUser().getHomeDirectory();
      //获取上传用户
      String name = session.getUser().getName();
      //获取上传文件名
      String filename = request.getArgument();
      logger.info("用户:'{}',上传文件到目录:'{}',文件名称为:'{}',状态:开始上传~", name, path, filename);
      return super.onUploadStart(session, request);
     }
     @Override
     public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)
       throws FtpException, IOException {
      //获取上传文件的上传路径
      String path = session.getUser().getHomeDirectory();
      //获取上传用户
      String name = session.getUser().getName();
      //获取上传文件名
      String filename = request.getArgument();
      logger.info("用户:'{}',上传文件到目录:'{}',文件名称为:'{},状态:成功!'", name, path, filename);
      return super.onUploadEnd(session, request);
     }
     @Override
     public FtpletResult onDownloadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
      //todo servies...
      return super.onDownloadStart(session, request);
     }
     @Override
     public FtpletResult onDownloadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
      //todo servies...
      return super.onDownloadEnd(session, request);
     }
    }
    
    下一篇:没有了