Android FTPServer监听摄像机文件上传处理

复制代码
Android 手机开启FTPServer服务,设置用户信息密码等,服务的ip地址就是本地的ip地址
摄像机在通信里面选择FTP方式,然后连接可以

implementation 'commons-net:commons-net:3.8.0'
implementation 'org.apache.ftpserver:ftpserver-core:1.1.1'
复制代码
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}
复制代码
public class FTPLogin2Aty extends AppCompatActivity {
    private EditText textName,textIp,textPwd;
    private Button btnLink;
    private FtpServer mFtpserver;
    private String filePath;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ftplogin_aty);
        initView();
        initData();
    }
    private void initView(){
        textIp = findViewById(R.id.address_ip);
        textName = findViewById(R.id.address_name);
        textPwd = findViewById(R.id.address_pwd);
        btnLink = findViewById(R.id.address_btn);
        btnLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinkServer();
            }
        });
    }

    private void initData() {
//        String albumPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutepath();
//        File internalStoragePath = getFilesDir();
//        filePath = internalStoragePath.getAbsolutePath();
        String albumPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
        filePath = albumPath;
    }

    private void LinkServer(){
        try {
            startFtpserverNew(getIpAddress());
        }catch (Exception e){

        }
    }

    private String getIpAddress(){
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for(NetworkInterface networkInterface:interfaces){
                List<InetAddress> addresses = Collections.list(networkInterface.getInetAddresses());
                for(InetAddress inetAddress : addresses){
                    if(!inetAddress.isLoopbackAddress()){
                        String strAddress = inetAddress.getHostAddress().toLowerCase();
                        boolean isIPV4 = Isipv4(strAddress);
                        if(isIPV4){
                            return strAddress;
                        }
                    }
                }
            }
        }catch (Exception e){

        }
        return null;
    }

    private boolean Isipv4(String ipv4){
        if (TextUtils.isEmpty(ipv4)){
            return false;
        }
        String [] parts = ipv4.split("\\.");
        if(parts.length!=4){
            return false;
        }
        for(String part:parts){
            try {
                int n = Integer.parseInt(part);
                if(n<0 || n>255){
                    return false;
                }
            }catch (Exception e){
                return false;
            }
        }
        return true;
    }

    private static final int PORT = 1234;
    //192.168.31.105
    private void startFtpserver(String hostip){
        try {
            textIp.setText(hostip);
            FtpServerFactory serverFactory = new FtpServerFactory();
            ListenerFactory factory = new ListenerFactory();
            serverFactory.addListener( "default", factory.createListener());
            factory.setPort(PORT);
            factory.setServerAddress(hostip);
            serverFactory.addListener( "default", factory.createListener());
            // 设置FTP服务器配置

            File fileDir = new File(filePath+"/directory"); // 指定存放文件的目录路径
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            BaseUser user = new BaseUser();
            user.setName("at");
            user.setPassword("");
            user.setEnabled(true);
            user.setMaxIdleTime(3000);
            user.setHomeDirectory(fileDir.getAbsolutePath()); // 设置默认目录为文件存放目录
            List<Authority> authorities = new ArrayList<>();
            authorities.add(new WritePermission());
            user.setAuthorities(authorities);
            serverFactory.getUserManager().save(user);
            // 配置服务器被操作的命令等回复信息,下面详细介绍

            Map<String, Ftplet> ftplets = new HashMap<>();
            ftplets.put("miaFtplet",new FTPLogin2Aty.MyFtpLet());
            serverFactory.setFtplets(ftplets);
            if(mFtpserver != null){
                mFtpserver.stop();
            }
            mFtpserver = serverFactory.createServer();
            mFtpserver.start();

            Toast.makeText(FTPLogin2Aty.this,"开启服务",Toast.LENGTH_SHORT).show();
//            FtpFile homeDirectory = serverFactory.getFileSystem().createFileSystemView(user).getHomeDirectory();
//            Toast.makeText(FTPLoginAty.this,homeDirectory.getAbsolutePath(),Toast.LENGTH_SHORT).show();
        }catch (Exception e){
            Log.e("111",e.getMessage());
        }

    }

    public class MyFtpLet extends DefaultFtplet {

        @Override
        public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException {
            return super.beforeCommand(session, request);
        }

        @Override
        public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException {
            Log.e("11111",reply.getMessage());
            return super.afterCommand(session, request, reply);
        }

        /**
         * 上传开始的状态应答,客户端开启上传过程之前的最后一次应答。
         */
        @Override
        public FtpletResult onUploadStart(FtpSession session, FtpRequest request) throws FtpException, IOException {
            return super.onUploadStart(session, request);
        }

        @Override
        public FtpletResult onUploadEnd(FtpSession session, FtpRequest request) throws FtpException, IOException {
            Log.e("11111","post end");
            return super.onUploadEnd(session, request);
        }
    }

    private FtpServer mFtpServerNew;
    private void startFtpserverNew(String hostip){
        try {
            textIp.setText(hostip);
            if (null != mFtpServerNew) {
                stop();
            }
            File fileDir = new File(filePath+"/directory"); // 指定存放文件的目录路径
            textName.setText(filePath+"/directory");
            if (!fileDir.exists()) {
                fileDir.mkdirs();
            }
            FtpServerFactory ftpServerFactory = new FtpServerFactory();
            // 配置FTP用户信息文件
            PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
            UserManager userManager = userManagerFactory.createUserManager();
            // 账户信息(也可以新增用户)
//            BaseUser user = (BaseUser) userManager.getUserByName("at");
            BaseUser user = new BaseUser();
            user.setName("at");
            user.setHomeDirectory(filePath+"/directory");
            user.setPassword("1234");
            List<Authority> authorities = new ArrayList<>();
            authorities.add(new WritePermission());
            user.setAuthorities(authorities);
            userManager.save(user); // 修改账户信息

            ListenerFactory listenerFactory = new ListenerFactory();
            // replace the default listener
            listenerFactory.setPort(PORT);
            listenerFactory.setServerAddress(hostip);
            ftpServerFactory.addListener("default", listenerFactory.createListener());
            ftpServerFactory.setUserManager(userManager);
            // 配置服务器被操作的命令等回复信息,下面详细介绍
            Map<String, Ftplet> ftplets = new HashMap<>();
            ftplets.put("miaFtplet",new FTPLogin2Aty.MyFtpLet());
            ftpServerFactory.setFtplets(ftplets);
            mFtpServerNew = ftpServerFactory.createServer();
            mFtpServerNew.start();
            Toast.makeText(FTPLogin2Aty.this,"开启服务",Toast.LENGTH_SHORT).show();
        }catch (Exception e){

        }
    }

    public void stop() {
        if (null != mFtpServerNew && !mFtpServerNew.isStopped()) {
            mFtpServerNew.stop();
        }
        mFtpServerNew = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mFtpserver!=null){
            mFtpserver.stop();
            mFtpserver = null;
        }
    }
}

参考文章:Android搭建Ftp服务器监听文件传输状态_ftpserver.user.admin.idletime-CSDN博客

Android设备FTP服务器搭建_android 搭建ftp服务器接收文件-CSDN博客

FTP错误代码列表(ftp状态码、ftp错误码)_ftp 230-CSDN博客

相关推荐
醉颜凉29 分钟前
【NOIP提高组】潜伏者
java·c语言·开发语言·c++·算法
阿维的博客日记34 分钟前
java八股-jvm入门-程序计数器,堆,元空间,虚拟机栈,本地方法栈,类加载器,双亲委派,类加载执行过程
java·jvm
qiyi.sky34 分钟前
JavaWeb——Web入门(8/9)- Tomcat:基本使用(下载与安装、目录结构介绍、启动与关闭、可能出现的问题及解决方案、总结)
java·前端·笔记·学习·tomcat
lapiii35838 分钟前
图论-代码随想录刷题记录[JAVA]
java·数据结构·算法·图论
RainbowSea41 分钟前
4. Spring Cloud Ribbon 实现“负载均衡”的详细配置说明
java·spring·spring cloud
程序员小明z41 分钟前
基于Java的药店管理系统
java·开发语言·spring boot·毕业设计·毕设
爱敲代码的小冰1 小时前
spring boot 请求
java·spring boot·后端
Lyqfor1 小时前
云原生学习
java·分布式·学习·阿里云·云原生
曾经的三心草2 小时前
Mysql之约束与事件
android·数据库·mysql·事件·约束
程序猿麦小七2 小时前
今天给在家介绍一篇基于jsp的旅游网站设计与实现
java·源码·旅游·景区·酒店