Android搭建ftp服务器/客户端
生活随笔
收集整理的這篇文章主要介紹了
Android搭建ftp服务器/客户端
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
jar包鏈接https://download.csdn.net/download/weixin_40391500/10629775
ftp服務器
不設置讀寫權限是不能上傳的文件的
private static String hostip = ""; // 本機IPpublic static final int PORT = 21;// sd卡目錄@SuppressLint("SdCardPath") // public static final String dir = Environment.getExternalStorageDirectory().getAbsolutePath();public static final String dir = "/mnt/sdcard/";public static final String dirname = dir + "face/";// ftp服務器配置文件路徑private static final String filename = dirname + "users.properties";private static FtpServer mFtpServer = null;private String properties ="ftpserver.user.admin.userpassword=E10ADC3949BA59ABBE56E057F20F883E\r\nftpserver.user.admin.homedirectory=/mnt/sdcard/ftp\r\nftpserver.user.admin.enableflag=true\r\nftpserver.user.admin.writepermission=true\r\nftpserver.user.admin.maxloginnumber=4\r\nftpserver.user.admin.maxloginperip=4\r\nftpserver.user.admin.idletime=3000\r\nftpserver.user.admin.uploadrate=4800\r\nftpserver.user.admin.downloadrate=4800\r\n";public synchronized static FTPServerUtils getIntance(){if(utils == null){utils = new FTPServerUtils();}return utils;}/*** 創建服務器配置文件*/public void creatDirsFiles(){try {File dir = new File(dirname);if (!dir.exists()) {dir.mkdirs();}FileOutputStream fos = null;File sourceFile = new File(filename);if (sourceFile.exists()){sourceFile.delete();}fos = new FileOutputStream(sourceFile);fos.write(properties.getBytes());if (fos != null) {fos.close();}}catch (Exception e){e.printStackTrace();}}/*** 開啟FTP服務器** @param hostip 本機ip*/public void startFtpServer(final String hostip) {new Thread(new Runnable() {@Overridepublic void run() { // if (!new File(filename).exists()){ // creatDirsFiles(); // }creatDirsFiles();FtpServerFactory serverFactory = new FtpServerFactory();PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();File files = new File(filename);//設置配置文件userManagerFactory.setFile(files);serverFactory.setUserManager(userManagerFactory.createUserManager());// 設置監聽IP和端口號ListenerFactory factory = new ListenerFactory();factory.setPort(PORT);factory.setServerAddress(hostip);serverFactory.addListener("default", factory.createListener());BaseUser user = new BaseUser();//設置權限List<Authority> authorities = new ArrayList<>();authorities.add(new WritePermission());user.setAuthorities(authorities);//設置用戶名密碼user.setName("admin");user.setPassword("123456");user.setEnabled(true);user.setMaxIdleTime(3000);user.setHomeDirectory(dirname);try {serverFactory.getUserManager().save(user);} catch (FtpException e) {e.printStackTrace();}if(mFtpServer != null){mFtpServer.stop();}// start the servermFtpServer = serverFactory.createServer();try {mFtpServer.start(); // FTPClientUtils.getIntance().ftpConnet();LogUtils.printLog("開啟了FTP服務器 ip = " + hostip);} catch (Exception e) {e.printStackTrace();}}}).start();}/*** 關閉FTP服務器*/public void stopFtpServer() {new Thread(new Runnable() {@Overridepublic void run() {if (mFtpServer != null) {mFtpServer.stop();mFtpServer = null;LogUtils.printLog("關閉了FTP服務器 ip = " + hostip);}}}).start();}/*** 獲取本機ip*/public String getLocalIpAddress() {try {List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());for (NetworkInterface intf : interfaces) {List<InetAddress> addrs = Collections.list(intf.getInetAddresses());for (InetAddress addr : addrs) {if (!addr.isLoopbackAddress()) {String sAddr = addr.getHostAddress().toUpperCase();boolean isIPv4 = Isipv4(sAddr);if (isIPv4) {return sAddr;}}}}} catch (Exception ex) {ex.printStackTrace();}return null;}public void onStartServer() {hostip = getLocalIpAddress();LogUtils.printLog("獲取本機IP = " + hostip);startFtpServer(hostip);}public static boolean Isipv4(String ipv4) {if (ipv4 == null || ipv4.length() == 0) {return false;//字符串為空或者空串}//因為java doc里已經說明, split的參數是reg, 即正則表達式, 如果用"|"分割, 則需使用"\\|"String[] parts = ipv4.split("\\.");if (parts.length != 4) {//分割開的數組根本就不是4個數字return false;}for (int i = 0; i < parts.length; i++) {try {int n = Integer.parseInt(parts[i]);//數字不在正確范圍內if (n < 0 || n > 255) {return false;}} catch (NumberFormatException e) {//轉換數字不正確return false;}}return true;}ftp客戶端
public void ftpConnet(){new Thread(new Runnable() {@Overridepublic void run() {try {client = new FTPClient();String host = "192.168.1.114";client.connect(host, 21);client.login("admin", "123456");} catch (Exception e) {e.printStackTrace();}}}).start();}public void ftpClose(){new Thread(new Runnable() {@Overridepublic void run() {try {if (client !=null)client.disconnect(false);} catch (Exception e) {e.printStackTrace();}}}).start();}/***下載文件***/public void download(){new Thread(new Runnable() {@Overridepublic void run() {try {if (client != null){String url = "/11.jpg";//服務器上的文件PrintlnLog.printLog("-----------------url=" + url);File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/ftp");if (!file.exists()){file.mkdirs();}File file1 = new File(file, "11.jpg");client.download(url, file1, new FTPDataTransferListener() {@Overridepublic void started() {PrintlnLog.printLog("-----------------started");}@Overridepublic void transferred(int i) {// PrintlnLog.printLog("-----------------transferred");}@Overridepublic void completed() {PrintlnLog.printLog("-----------------completed");}@Overridepublic void aborted() {PrintlnLog.printLog("-----------------aborted");}@Overridepublic void failed() {PrintlnLog.printLog("-----------------failed");}});}else {PrintlnLog.printLog("-----------------client=null"); // ftpConnet();}} catch (Exception e) {e.printStackTrace();}}}).start();}/***上傳文件***/public void upload(){new Thread(new Runnable() {@Overridepublic void run() {try {if (client != null){File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/12.jpg");if (!file.exists()){file.mkdirs();}client.createDirectory("file");client.changeDirectory("file");client.upload(file, new FTPDataTransferListener() {@Overridepublic void started() {PrintlnLog.printLog("-----------------started");}@Overridepublic void transferred(int i) {// PrintlnLog.printLog("-----------------transferred");}@Overridepublic void completed() {PrintlnLog.printLog("-----------------completed");}@Overridepublic void aborted() {PrintlnLog.printLog("-----------------aborted");}@Overridepublic void failed() {PrintlnLog.printLog("-----------------failed");}});client.changeDirectory("/");}else {PrintlnLog.printLog("-----------------client=null");}} catch (Exception e) {e.printStackTrace();}}}).start();}總結
以上是生活随笔為你收集整理的Android搭建ftp服务器/客户端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华中科技大学计算机徐永兵,ACS Nan
- 下一篇: 一些android预装应用名字对应关系