/ ** *
Title: Connect FTP Server P> *
Description: A class connecting FTP, upload download file p> * @Author yangtom * @version 1.1 * / public class ftp {ftpclient Client; / ** * FTP server ip * / private string host = ""; / ** * ftp user name * / private string username = "; / ** * ftp password * / private string password =" "; / ** * ftp port * / private int port = 21;
Public ftp () {// Properties PROPS = New Properties (); // try {// props.load (getClass (). getResourceAsStream (// "/commmmon/applicationresources.properties") ://} / / catch (IOException ex) {// ex.printStackTrace (); //} ResourceBundle rb = ResourceBundle.getBundle ( "com.common.ApplicationResources"); host = rb.getString ( "ftp_host"); username = rb.getString ("Username"); password = rb.getstring ("password"); port = integer.parseint (rb.getstring ("port"));}
/ ** * Connection server method * @throws baseException * / public void connection {try {client = new ftpclient (); client.openserver (Host, port); client.login (username, password); // Set to 2 binary transfer client.binary (); logger.write ("Success:" Host);} catch (ftploginexception e) {logger.write ("No right to connect" E.getMessage ()); Throw New ApplicationException ("Error.Sjdr.ftp.login");} catch (ioException e) {logger.write ("Connection Fail" E.GetMessage ()); throw new applicationException ("Error.Sjdr.ftp.connect ");} Catch (securityException e) {logger.write (" User name or password is wrong "); throw new applicationException (" error.sjdr.ftp.security);}} / ** * upload file * @Param HostFileName String uploaded to the ftp server file path name * @param localFilePath String uploaded file upload success in the local path of the file name * @return boolean * / public boolean upLoad (String hostFileName, String localFilePath) {try {TelnetOutputStream os = client. Put (HostFileName); java.io.file file_in = new j Ava.io.file (LocalFilePath); fileinputstream is = new fileinputstream (file_in); byte [] bytes = new byte [1024]; int C; while ((c = is.read (bytes))! = -1) { Os.Write (bytes, 0, c);} is.close (); os.close (); logger.write ("Upload file success");} catch (Exception E) {logger.write ("Upload file error " E.GetMessage ()); return false;} return true;
Path / ** * * @param hostFileName String download files download ftp server file name * @param localFilePath String downloaded to the local path to the file name * @return boolean * / public boolean download (String hostFileName, String localFilePath) {File localFile = new File (localFilePath); try {TelnetInputStream is = client.get (hostFileName); java.io.File file_in = new java.io.File (localFilePath); FileOutputStream os = new FileOutputStream (file_in); byte [] bytes = New byte [1024]; int C; while ((c = is.read (bytes))! = -1) {os.write (bytes, 0, c);} is.close (); os.close () Logger.write ("Download File Success");} Catch (Exception E) {Logger.write ("Download File Error" E.GetMessage ()); Return False;} Return true;}
/ ** * Close FTP server * @throws java.lang.exception * / public void closeeServer () throws exception {if (client! = Null) {Client.closserver (); logger.write ("FTP has been closed"); }