Android and Android use WIFI for FTP communication and androidwifi
I. Client Communication tools:
Import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; import java.net. socketException; import org.apache.commons.net. ftp. FTP; import org.apache.commons.net. ftp. FTPClient; import org.apache.commons.net. ftp. FTPFile; import org.apache.commons.net. ftp. FTPReply;/*** tool used for Android and FTP server interaction */public class FTPUtils {private FTPClient ftpClient = null; private static FTPUtils ftpUtilsInstance = null; private String ftpUrl; private int ftpPort; private String userName; private String userPassword; private final int connectTimeout = 5000; private FTPUtils () {ftpClient = new FTPClient () ;}/ ** get the class object instance (because only one such class object exists, use Singleton mode) */public static FTPUtils getInstance () {if (ftpUtilsInstance = null) {ftpUtilsInstance = ne W FTPUtils ();} return ftpUtilsInstance ;} /*** set the FTP server ** @ param FTPUrl * FTP Server IP Address * @ param FTPPort * FTP server port number * @ param UserName * login to the FTP server account * @ param UserPassword * FTP server password * @ return */public boolean initFTPSetting (String FTPUrl, int FTPPort, String UserName, String UserPassword) {ftpUrl = FTPUrl; ftpPort = FTPPort; userName = UserName; userPassword = UserPassword; int reply; try {// Set the connection timeout value ftpClient. setConnectTimeout (connectTimeout); // the Url of the FTP server to be connected, Port ftpClient. connect (FTPUrl, FTPPort); // log on to the FTP server ftpClient. login (UserName, UserPassword); // check whether the returned value is 230. If yes, the login is successful. reply = ftpClient. getReplyCode (); if (! FTPReply. isPositiveCompletion (reply) {// disconnect ftpClient. disconnect (); return false;} return true;} catch (SocketException e) {e. printStackTrace (); return false;} catch (IOException e) {e. printStackTrace (); return false ;}}/*** disconnect the FTP server */public boolean disconnectFTP () {try {ftpClient. disconnect (); return true;} catch (IOException e) {e. printStackTrace (); return false ;}/ *** upload a file ** @ Param FilePath * path of the SDCard where the file to be uploaded is located * @ param FileName * file name of the file to be uploaded (for example, unique Sim ID code) * @ return true indicates that the file is successfully uploaded, false: Failed */public boolean uploadFile (String FilePath, String FileName) {if (! FtpClient. isConnected () {if (! InitFTPSetting (ftpUrl, ftpPort, userName, userPassword) {return false ;}try {// set the storage path // ftpClient. makeDirectory ("/FtpFileTest"); // ftpClient. changeWorkingDirectory ("/FtpFileTest"); // you can specify the basic information required to upload a file. setBufferSize (1024); ftpClient. setControlEncoding ("UTF-8"); ftpClient. enterLocalPassiveMode (); ftpClient. setFileType (FTP. BINARY_FILE_TYPE); // upload a file ~ FileInputStream fileInputStream = new FileInputStream (FilePath); ftpClient. storeFile (FileName, fileInputStream); // close the file stream fileInputStream. close (); // log out of FTP and close the ftpCLient connection. logout (); ftpClient. disconnect ();} catch (IOException e) {e. printStackTrace (); return false;} return true ;} /***** download file ** @ param FilePath * path of the file to be stored * @ param FileName * Name of the file on the remote FTP server * @ return true indicates success, false is Failed */public boolean downLoadFile (String FilePath, String FileName) {if (! FtpClient. isConnected () {if (! InitFTPSetting (ftpUrl, ftpPort, userName, userPassword) {return false ;}try {// go to the specified download directory ftpClient. changeWorkingDirectory ("/data"); // list all files in this directory. FTPFile [] files = ftpClient. listFiles (); // traverse all files and find the specified file for (FTPFile file: files) {if (file. getName (). equals (FileName) {// initialize the File localFile = new File (FilePath) according to the absolute path; // output stream OutputStream outputStream = new FileOutputStream (localFile ); // download the ftpClient file. retrieveFile (file. getName (), outputStream); // clear the cached outputStream. flush (); // close the stream outputStream. close () ;}// log out of FTP and close the ftpCLient connection. logout (); ftpClient. disconnect ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}return true ;}}
2. Use Android devices as FTP servers. The following are FTPServer types:
Import java. io. file; import java. io. IOException; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import org. apache. ftpserver. ftpServer; import org. apache. ftpserver. ftpServerFactory; import org. apache. ftpserver. ftplet. authority; import org. apache. ftpserver. ftplet. defaultFtplet; import org. apache. ftpserver. ftplet. ftpException; import org. apache. ftpserver. ftplet. FtpRequest; import org. apache. ftpserver. ftplet. ftpSession; import org. apache. ftpserver. ftplet. ftplet; import org. apache. ftpserver. ftplet. ftpletResult; import org. apache. ftpserver. ftplet. userManager; import org. apache. ftpserver. listener. listenerFactory; import org. apache. ftpserver. usermanager. propertiesUserManagerFactory; import org. apache. ftpserver. usermanager. saltedPasswordEncryptor; import org. apache. f Tpserver. usermanager. impl. baseUser; import org. apache. ftpserver. usermanager. impl. writePermission; import android. OS. environment; public class FtpServerlet extends DefaultFtplet {private FtpServer mFtpServer; private final int mPort = 2121; private final String mDirectory = Environment. getExternalStorageDirectory (). getPath () + "/FtpFileTest"; private final String mUser = "way"; private final String mP Assword = "way"; private static FtpServerlet mInstance; public static FtpServerlet getInstance () {if (mInstance = null) {mInstance = new FtpServerlet ();} return mInstance ;} /*** FTP start ** @ throws FtpException */public void start () throws FtpException {if (null! = MFtpServer & false = mFtpServer. isStopped () {return;} File file = new File (mDirectory); if (! File. exists () {file. mkdirs ();} FtpServerFactory serverFactory = new FtpServerFactory (); ListenerFactory listenerFactory = new ListenerFactory (); // set the end number listenerFactory. setPort (mPort); // create UserManager through PropertiesUserManagerFactory and add the user PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory () to the configuration file; userManagerFactory. setPasswordEncryptor (new SaltedPasswordEncryptor (); UserManager userManager = userManagerFactory. createUserManager (); List <Authority> auths = new ArrayList <Authority> (); Authority auth = new WritePermission (); auths. add (auth); // add user BaseUser user = new BaseUser (); user. setName (mUser); user. setPassword (mPassword); user. setHomeDirectory (mDirectory); user. setAuthorities (auths); userManager. save (user); // set Ftplet Map <String, Ftplet> ftpletMap = New HashMap <String, Ftplet> (); ftpletMap. put ("Ftplet", this); serverFactory. setUserManager (userManager); serverFactory. addListener ("default", listenerFactory. createListener (); serverFactory. setFtplets (ftpletMap); // create and start FTPServer mFtpServer = serverFactory. createServer (); mFtpServer. start ();}/*** FTP stop */public void stop () {// FtpServer does not exist and FtpServer is running if (null! = MFtpServer & false = mFtpServer. isStopped () {mFtpServer. stop () ;}@ Override public FtpletResult onAppendStart (FtpSession session, FtpRequest request) throws FtpException, IOException {System. out. println ("onAppendStart"); return super. onAppendStart (session, request) ;}@ Override public FtpletResult onAppendEnd (FtpSession session, FtpRequest request) throws FtpException, IOException {System. out. println ("onAppendEnd"); return super. onAppendEnd (session, request) ;}@ Override public FtpletResult onLogin (FtpSession session, FtpRequest request) throws FtpException, IOException {System. out. println ("onLogin"); return super. onLogin (session, request) ;}@ Override public FtpletResult onConnect (FtpSession session) throws FtpException, IOException {System. out. println ("onConnect"); return super. onConnect (session) ;}@ Override public FtpletResult onDisconnect (FtpSession session) throws FtpException, IOException {System. out. println ("onDisconnect"); return super. onDisconnect (session) ;}@ Override public FtpletResult onUploadStart (FtpSession session, FtpRequest request) throws FtpException, IOException {System. out. println ("onUploadStart"); return super. onUploadStart (session, request) ;}@ Override public FtpletResult onUploadEnd (FtpSession session, FtpRequest request) throws FtpException, IOException {String FtpUploadPath = mDirectory + "/" + request. getArgument (); // delete File uploadFile = new File (FtpUploadPath) immediately after receiving the File; uploadFile. delete (); return super. onUploadEnd (session, request );}}
3. Add related permissions to AndroidManifest on the client and server
<! -- Allow the application to open the network set interface -->
<Uses-permission android: name = "android. permission. INTERNET"/>
<! -- Allow applications to access Wi-Fi network information -->
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>
<! -- Allow applications to write user external memory -->
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
<! -- Allow applications to access the Wi-Fi multicast mode -->
<Uses-permission android: name = "android. permission. CHANGE_WIFI_MULTICAST_STATE"/>
<! -- Allow applications to change the connection status of Wi-Fi -->
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"/>
<! -- Allow applications to change the network connection status -->
<Uses-permission android: name = "android. permission. CHANGE_NETWORK_STATE"/>
<! -- Allow applications to access information on the Network -->
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>
4. Required jar packages
Commons-net-3.0.1.jar
Ftpserver-core-1.0.6.jar
After searching for the attachment for half a day, I don't know how to upload the attachment. I have to ask you to download it by yourself.