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博客