Description
In fact, Android rarely use FTP and server interaction, especially the file upload and download operations, performance first, the entire code is very embarrassing, the basic need to use the native API to operate. There's not much encapsulation here,
Just complete the function, after all, FTP is not the mainstream of Android network requests. Gossip is not much to say, on the code.
Package com.hisign.util;
Import Android.text.TextUtils;
Import Android.util.Log;
Import com.hisign.qrcebpro.app.Constant;
Import com.hisign.qrcebpro.app.MyApplication;
Import Com.hisign.qrcebpro.utils.LogUtil;
Import Com.hisign.qrcebpro.utils.PreferenceUtil;
Import Com.hisign.util.ZipUtils.IResultListener;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import it.sauronsoftware.ftp4j.FTPAbortedException;
Import it.sauronsoftware.ftp4j.FTPClient;
Import it.sauronsoftware.ftp4j.FTPDataTransferException;
Import It.sauronsoftware.ftp4j.FTPDataTransferListener;
Import it.sauronsoftware.ftp4j.FTPException;
Import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
/** * Hongzhen yu Create at 2017/7/28/public class Ftpmanager {private static Ftpmanager instance; Private Ftpmanager () {}/** * @return * @throws * @Title: getinstance * @Description: Single case approach For object */public static FTpmanager getinstance () {if (instance = null) {synchronized (Ftpmanager.class) {if (instance = null)
{instance = new Ftpmanager ();
}} return instance; } public void Ftp4jupload (final String path, final Iresultlistener listener) {new Thread () {Pub
LIC void Run () {try {String targetName = ftp4jupload (path);
if (listener!= null) {listener.onsuccess (targetName); catch (IllegalStateException | IOException | ftpillegalreplyexception | ftpexception | ftpdatatransferexception |
Ftpabortedexception e) {e.printstacktrace ();
LOG.D ("Lixm", "Ftp4jupload Error:", e); if (listener!= null) {listener.onfilure (E.getmessage ());
}}}.start ();
/** * FTP protocol File Upload * * @param path * @throws ftpexception * @throws ftpillegalreplyexception * @throws IOException * @throws illegalstateexception * @throws ftpabortedexception * @throws ftpdatat Ransferexception */public string Ftp4jupload (string path) throws IllegalStateException, IOException, FTPILLEGALR Eplyexception, Ftpexception, ftpdatatransferexception, ftpabortedexception {//Create client final ftpclient Clie
NT = new FtpClient (); Do not specify a port, use the default port String IP = Preferenceutil.getnetworkip (myapplication.getmyapplication (). Getapplicationcontext (
));
String Rightip = "192.168.128.52"; if (!
Textutils.isempty (IP)) {string[] Iparr = Ip.split ("\.");
if (Iparr!= null && iparr.length = = 4) {rightip = IP; } int RIGHTPORT = 21;
String port = Preferenceutil.getnetworkport (Myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (port)) {Rightport = integer.valueof (Rightport);
} client.connect (Rightip, Rightport);
User Login String user = Preferenceutil.getnetworkuser (myapplication.getmyapplication (). Getapplicationcontext ());
String rightuser = "Test"; if (!
Textutils.isempty (user)) {rightuser = user;
String pwd = preferenceutil.getnetworkpwd (Myapplication.getmyapplication (). Getapplicationcontext ());
String rightpwd = "Test"; if (!
Textutils.isempty (pwd)) {rightpwd = pwd;
} client.login (Rightuser, rightpwd);
String Rightfilepath = "";
String FilePath = Preferenceutil.getnetworkftppath (Myapplication.getmyapplication (). Getapplicationcontext ()); if (! Textutils.isempty (FilePath)) {Rightfilepath = "/" + FilePath + "/";
Client.changedirectory (Rightfilepath+preferenceutil.getusercode (Myapplication.getmyapplication ()) + "/");
File File = new file (path);
Client.upload (file);
Client.rename (SrcName, targetName);
return targetName; ///download files from FTP server public void Ftpdownload (final Ftpdatatransferlistener listener) {New Thread (new RUNNABL E () {@Override public void run () {//Create client final ftpclient Clien
t = new ftpclient (); If IP is not specified, the default IP String IP = preferenceutil.getnetworkip (myapplication.getmyapplication (). Getapplicationcont
EXT ());
String Rightip = "192.168.128.52"; if (!
Textutils.isempty (IP)) {string[] Iparr = Ip.split ("\.");
if (Iparr!= null && iparr.length = = 4) {rightip = IP;
}
} Do not specify a port, then use the default port int rightport = 21;
String port = Preferenceutil.getnetworkport (Myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (port)) {Rightport = integer.valueof (Rightport);
try {client.connect (Rightip, Rightport); User logon String user = Preferenceutil.getnetworkuser (myapplication.getmyapplication (). Getapplicationcon
Text ());
String rightuser = "Test"; if (!
Textutils.isempty (user)) {rightuser = user;
String pwd = preferenceutil.getnetworkpwd (Myapplication.getmyapplication (). Getapplicationcontext ());
String rightpwd = "Test"; if (!
Textutils.isempty (pwd)) {rightpwd = pwd;
} Client.login (Rightuser, rightpwd);
String pathftp = "/" + "ZIP" + "/";
String FilePath = Preferenceutil.getnetworkftppath (Myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (FilePath)) {pathftp = FilePath + "/"; } client.changedirectory (Pathftp+preferenceutil.getusercode (Myapplication.getmyapplication ()) + "/result
");
String[] strings = Client.listnames ();
for (int i = 0; i < strings.length i++) {log.i ("ListFile", strings[i));
String name = Strings[i];
LOG.I ("file", name);
File File = new file (Constant.resuit_path + name);
Output stream OutputStream OutputStream = new FileOutputStream (file); Client.download (name, OutputStream, 0, LiStener);
Outputstream.close ();
Client.logout ();
The catch (Exception e) {}}}). Start (); /** * Create the specified directory on the FTP server * @param/public void Ftpmakeuserdir () {New Thread () (New Runnable {@Override public void run () {//Create client final ftpclient clients
= new FtpClient ();
Do not specify a port, then use the default port of String Rightip = "192.168.128.52";
String IP = preferenceutil.getnetworkip (myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (IP)) {string[] Iparr = Ip.split ("\.");
if (Iparr!= null && iparr.length = = 4) {rightip = IP;
an int rightport = 21; String Port = PreferenCeutil.getnetworkport (Myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (port)) {Rightport = integer.valueof (Rightport);
try {client.connect (Rightip, rightport);//Connect FTP server//Configure user name
String user = Preferenceutil.getnetworkuser (myapplication.getmyapplication (). Getapplicationcontext ());
String rightuser = "Test"; if (!
Textutils.isempty (user)) {rightuser = user; }//Configure password String pwd = preferenceutil.getnetworkpwd (Myapplication.getmyapplication (
). Getapplicationcontext ());
String rightpwd = "Test"; if (!
Textutils.isempty (pwd)) {rightpwd = pwd; } client.login (Rightuser, rightpwd);//Log on to FTP//Configure the FTP server root string pathftp = "/" + "ZIP" + "/";
String FilePath = Preferenceutil.getnetworkftppath (Myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (FilePath)) {pathftp = FilePath + "/";
} client.changedirectory (PATHFTP);
Create user directory String Usercode = Preferenceutil.getusercode (Myapplication.getmyapplication ());
if (!isdirexist (client, Usercode)) {client.createdirectory (usercode);
Logutil.logi ("Create directory Success");
} logutil.logi ("exist");
Client.logout ();
catch (IOException e) {e.printstacktrace ();
catch (Ftpillegalreplyexception e) {e.printstacktrace ();
catch (Ftpexception e) { E.printstacktrace ();
}}). Start (); public void Ftpmakeresultdir () {New Thread (new Runnable () {@Override public void R
Un () {//Create client final ftpclient clients = new ftpclient ();
Do not specify a port, then use the default port of String Rightip = "192.168.128.52";
String IP = preferenceutil.getnetworkip (myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (IP)) {string[] Iparr = Ip.split ("\.");
if (Iparr!= null && iparr.length = = 4) {rightip = IP;
an int rightport = 21;
String port = Preferenceutil.getnetworkport (Myapplication.getmyapplication (). Getapplicationcontext ()); if (! Textutils.isempty (port)) {Rightport = integer.valueof (Rightport);
try {client.connect (Rightip, rightport);//Connect FTP server//Configure user name
String user = Preferenceutil.getnetworkuser (myapplication.getmyapplication (). Getapplicationcontext ());
String rightuser = "Test"; if (!
Textutils.isempty (user)) {rightuser = user; }//Configure password String pwd = preferenceutil.getnetworkpwd (Myapplication.getmyapplication (
). Getapplicationcontext ());
String rightpwd = "Test"; if (!
Textutils.isempty (pwd)) {rightpwd = pwd; } client.login (Rightuser, rightpwd);//Log on to FTP//configure FTP server root STR
ing pathftp = "/" + "ZIP" + "/"; String FilePath = Preferenceutil.getnetworkftppath (Myapplication.getmyapplication (). GetappLicationcontext ()); if (!
Textutils.isempty (FilePath)) {pathftp = FilePath + "/"; //Create user directory String Usercode = Preferenceutil.getusercode (myapplication.getmyapplica
tion ());
String usrpath=pathftp+usercode+ "/";
Client.changedirectory (Usrpath); Create a result directory under User directory if (!isdirexist (client, "result") {Client.createdirectory
("result");
Logutil.logi ("Resultpath Create directory Success");
} logutil.logi ("result exists");
Client.logout ();
catch (IOException e) {e.printstacktrace ();
catch (Ftpillegalreplyexception e) {e.printstacktrace ();
catch (Ftpexception e) {e.printstacktrace ();
}
} ). Start (); /** to determine whether the FTP directory exists, there is no native to determine whether the directory exists method * * Public boolean isdirexist (ftpclient ftpclient, String dir) {try
{ftpclient.changedirectory (dir);
catch (Ftpexception e) {return false;
catch (IOException e) {return false;
catch (Ftpillegalreplyexception e) {return false;
return true; /** * Delete FTP server specified file * @param dirname/private void Ftpdeletefile (final string dir, final string dirname) {New Thread (new Runnable () {@Override public void run () {//Chuang
Build Client Final FtpClient clients = new ftpclient ();
Do not specify a port, then use the default port of String Rightip = "192.168.128.52";
String IP = preferenceutil.getnetworkip (myapplication.getmyapplication (). Getapplicationcontext ()); if (! Textutils.isempty (IP)) {String[] Iparr = Ip.split ("\;");
if (Iparr!= null && iparr.length = = 4) {rightip = IP;
an int rightport = 21;
String port = Preferenceutil.getnetworkport (Myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (port)) {Rightport = integer.valueof (Rightport);
try {client.connect (Rightip, Rightport); Configure user name String user = Preferenceutil.getnetworkuser (myapplication.getmyapplication (). Getapplicationcon
Text ());
String rightuser = "Test"; if (!
Textutils.isempty (user)) {rightuser = user; }//Configure password String pwd = preferenceutil.getnetworkpwd (Myapplication.getmyapplication (
). Getapplicationcontext ()); String rightpwd = "Test"; if (!
Textutils.isempty (pwd)) {rightpwd = pwd; } client.login (Rightuser, rightpwd);//Log on to FTP//configure FTP server root STR
ing pathftp = "/" + "ZIP" + "/";
String FilePath = Preferenceutil.getnetworkftppath (Myapplication.getmyapplication (). Getapplicationcontext ()); if (!
Textutils.isempty (FilePath)) {pathftp = FilePath + "/"; //Create user directory String Usercode = Preferenceutil.getusercode (myapplication.getmyapplica
tion ());
String resultpath=pathftp+usercode+ "/" + "result/";
Client.changedirectory (Resultpath);
Client.deletefile (dirname);
Client.logout ();
catch (IOException e) {e.printstacktrace (); catch (Ftpillegalreplyexception e) {e.printstacktrace ();
catch (Ftpexception e) {e.printstacktrace ();
}}). Start ();
}
}