One: Download the Commons-net-1.4.1.jar package, put it under LIB, and load it into the. classpath file
Two: Write Tool class Ftputil.java as follows
- Package common;
- Import Java.io.File;
- Import java.io.FileNotFoundException;
- Import Java.io.FileOutputStream;
- Import java.io.IOException;
- Import Java.io.OutputStream;
- Import java.net.SocketException;
- Import java.util.Properties;
- Import Org.apache.log4j.Logger;
- Import Org.apache.commons.logging.Log;
- Import Org.apache.commons.logging.LogFactory;
- Import org.apache.commons.net.ftp.*;
- Public class Ftputil {
- Private final static Log logger = Logfactory.getlog (ftputil. Class);
- /**
- * Get FtpClient Object
- *
- * @param ftphost
- * FTP Host Server
- * @param FTPPassword
- * FTP Login Password
- * @param ftpusername
- * FTP Login user name
- * @param ftpport
- * FTP port defaults to 21
- * @return
- */
- public static FtpClient getftpclient (String ftphost, String ftpusername,
- String FTPPassword, int ftpport) {
- FtpClient ftpclient = new FtpClient ();
- try {
- FtpClient = new FtpClient ();
- Ftpclient.connect (Ftphost, FtpPort); //Connect FTP server
- Ftpclient.login (FtpUserName, FTPPassword); //Login FTP server
- if (! Ftpreply.ispositivecompletion (Ftpclient.getreplycode ())) {
- Logger.info ("Not connected to FTP, user name or password is incorrect. ");
- Ftpclient.disconnect ();
- } Else {
- Logger.info ("FTP connection succeeded. ");
- }
- } catch (SocketException e) {
- E.printstacktrace ();
- Logger.info ("FTP IP address may be wrong, please configure it correctly.") ");
- } catch (IOException e) {
- E.printstacktrace ();
- Logger.info ("FTP port error, please configure it correctly.") ");
- }
- return ftpclient;
- }
- /*
- * Download files from FTP server
- *
- * @param ftphost FTP IP Address
- *
- * @param ftpusername FTP user name
- *
- * @param ftppassword FTP user name password
- *
- * @param ftpport FTP port
- *
- * @param the path format of files in Ftppath FTP server: FTPTEST/AA
- *
- * @param localPath Download to local location format: h:/download
- *
- * @param filename File name
- */
- public static void Downloadftpfile (String ftphost, String ftpusername,
- String FTPPassword, int ftpport, string ftppath, String LocalPath,
- String fileName) {
- FtpClient ftpclient = null;
- try {
- FtpClient = Getftpclient (Ftphost, FtpUserName, FTPPassword, FtpPort);
- Ftpclient.setcontrolencoding ("UTF-8"); //Chinese support
- Ftpclient.setfiletype (Ftpclient.binary_file_type);
- Ftpclient.enterlocalpassivemode ();
- Ftpclient.changeworkingdirectory (Ftppath);
- File LocalFile = new File (LocalPath + File.separatorchar + fileName);
- OutputStream OS = new FileOutputStream (LocalFile);
- Ftpclient.retrievefile (fileName, OS);
- Os.close ();
- Ftpclient.logout ();
- } catch (FileNotFoundException e) {
- Logger.error ("not found" + Ftppath + "file");
- E.printstacktrace ();
- } catch (SocketException e) {
- Logger.error ("Connection to FTP failed.");
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- Logger.error ("file read error. ");
- E.printstacktrace ();
- }
- }
- }
Third, the test code
Suppose there is already an FTP server, login username and password for ZYF, Port 22, log into the FTPDir directory under the home directory, there is a 11.txt file for download test
- Package scripts.testscript;
- Import Org.testng.annotations.Test;
- Import common. Ftputil;
- Public class Ftpdownloadtestscript {
- @Test
- public Void Run () {
- String ftphost = "192.168.26.132";
- String ftpusername = "ZYF";
- String FTPPassword = "ZYF";
- int ftpport = 21;
- String Ftppath = "ftpdir/";
- String LocalPath = "H:/download";
- String fileName = "11.txt";
- Ftputil.downloadftpfile (Ftphost, FtpUserName, FTPPassword, FtpPort, Ftppath, LocalPath, fileName);
- }
- }
Java code implementation FTP single file download (ii)