In the Java program development, FTP with a lot of, often dealing with, for example, upload files to ftp server, download files, this article to introduce how to use the Jakarta Commons in the FtpClient (in the Commons-net package) to achieve upload download files.
First, upload files
The principle is not introduced, we look at the code directly
/** * Description: Uploading files to FTP server * @Version1.0, 2008 4:31:09 PM by Tri Hongbao (cuihongbao@d-heaven.com) create * @param URL FT P Server hostname * @param port FTP server ports * @param username FTP login account * @param password FTP login password * @param path FTP server save directory * @p Aram FileName uploaded to the FTP server file name * @param input stream * @return successfully returns TRUE, otherwise return false/Publicstaticboolean UploadFile (String Url,int port,string Username, string password, string path, string filename, InputStream input) {Boolean success = False
;
ftpclient ftp = new FtpClient ();
try {int reply;
Ftp.connect (URL, port);//Connect FTP server//If the default port is used, you can connect the FTP server directly using the Ftp.connect (URL) ftp.login (username, password);/Login
Reply = Ftp.getreplycode (); if (!
Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
return success;
} ftp.changeworkingdirectory (path);
Ftp.storefile (filename, input);
Input.close ();
Ftp.logout ();
Success = true;
catch (IOException e) {e.printstacktrace (); finally {if (ftp.isconnected ()) {try {Ftp.disconnecT ();
catch (IOException IoE) {}} return success; }<pre></pre>/** * Description: Uploading files to FTP server * @Version1.0, 2008 4:31:09 PM by Tri Hongbao (Cuihongbao@d-heaven. COM) creates * @param URL FTP server hostname * @param port FTP Server port * @param username FTP login account * @param password FTP login password * @param pat H FTP Server Save directory * @param filename uploaded to the FTP server file name * @param input stream * @return successfully returns TRUE, otherwise returns false */public static Boolean UPL Oadfile (String Url,int port,string Username, string password, string path, string filename, InputStream input) {Boolean s
Uccess = false;
ftpclient ftp = new FtpClient (); try {int reply; Ftp.connect (URL, port);//Connect FTP server//If the default port is used, you can connect to the FTP server directly using the Ftp.connect (URL) ftp.login (username,
password);/Login reply = Ftp.getreplycode (); if (!
Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect (); return success;} ftp.changeworkingdirectory (path);
Ftp.storefile (filename, input);
Input.close ();
Ftp.logout ();
Success = true; catch (IOException e) {e.printstacktrace ();}finally {if (ftp.isconnected ()) {try {ftp.disconnect ();} catch (IOException IoE) {}} return success; }
Below we write two small examples:
1. Upload the local file to the FTP server, the code is as follows:
@Test
publicvoid Testuploadfromdisk () {
try {
FileInputStream in=new fileinputstream (New File ("d:/ Test.txt "));
Boolean flag = UploadFile ("127.0.0.1", "Test", "Test", "D:/ftp", "test.txt", in);
SYSTEM.OUT.PRINTLN (flag);
} catch (FileNotFoundException e) {
e.printstacktrace ();
}
} <pre></pre>
@Test public
void Testuploadfromdisk () {
try {
FileInputStream in=new FileInputStream (New File ("D:/test.txt"));
Boolean flag = UploadFile ("127.0.0.1", "Test", "Test", "D:/ftp", "test.txt", in);
SYSTEM.OUT.PRINTLN (flag);
} catch (FileNotFoundException e) {
e.printstacktrace ();
}
2. Generate a file on the FTP server and write a string to the file
@Test
publicvoid testuploadfromstring () {
try {
InputStream input = new Bytearrayinputstream ("Test ftp"). GetBytes ("Utf-8"));
Boolean flag = UploadFile ("127.0.0.1", "Test", "Test", "D:/ftp", "test.txt", input);
SYSTEM.OUT.PRINTLN (flag);
} catch (Unsupportedencodingexception e) {
e.printstacktrace ();
}
} <pre></pre>
@Test public
void Testuploadfromstring () {
try {
InputStream input = new Bytearrayinputstream ("Test ftp". GetBytes ("Utf-8"));
Boolean flag = UploadFile ("127.0.0.1", "Test", "Test", "D:/ftp", "test.txt", input);
SYSTEM.OUT.PRINTLN (flag);
} catch (Unsupportedencodingexception e) {
e.printstacktrace ();
}
Second, download the file
The code for downloading files from an FTP server is also simple, as follows:
/** * Description: Download files from FTP server * @Version. June:: PM by Tri Hongbao (cuihongbao@d-heaven.com) create * @param URL FTP server hostname * @param port FTP Server port * @param username FTP Login Record account * @param password FTP login password * @param remotepath FTP Server relative path * @param filename to download * @param localpath saved to local path after download Diameter * @return/Publicstaticboolean downfile (string url, int port,string username, string password, string remotepath,st
Ring filename,string LocalPath) {Boolean success = false;
ftpclient ftp = new FtpClient ();
try {int reply;
Ftp.connect (URL, port);
If you use the default port, you can use the Ftp.connect (URL) way to connect directly to the FTP server Ftp.login (username, password);/Login reply = Ftp.getreplycode (); if (!
Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
return success;
} ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles ();
for (Ftpfile ff:fs) {if (Ff.getname (). Equals (FileName)) {file LocalFile = new File (localpath+ "/" +ff.getname ());
OutputStream is = new FileOutputStream (localfile); FTp.retrievefile (Ff.getname (), is);
Is.close ();
} ftp.logout ();
Success = true;
catch (IOException e) {e.printstacktrace ();
finally {if (ftp.isconnected ()) {try {ftp.disconnect ();
catch (IOException IoE) {}} return success; }<pre></pre>