public class ftpupload { private ftpclient client; /** * connect the FTP server and change the path of the selected FTP server * */ public void connect (FtpConfig config) throws SocketException, IOException { Client = new ftpclient (); client.connect ( Config.getserver (), config.getport ());//Set FTP server address and Port Client.login (Config.getusername (), config.getpassword ());//Set server user and password int reply = client.getreplycode (); if (! Ftpreply.ispositivecompletion (Reply)) Client.disconnect (); client.changeworkingdirectory (Config.getRemotePath ()); client.setfiletransfermode (FTP. Binary_file_type); client.setfiletype (FTP. Binary_file_type) } /** * Close connection */ public void closeconnect () { try { this.client.disconnect (); } catch (IOException e) { e.printstacktrace (); } } /** * Changing the work of FTP dir */ public void changedirector (String path) throws Exception { String Pathtree[] = path.split ("\ \" +file.separator); for (int i = 0; i < pathtree.length; i++) { if (Stringutil.nonullblank (pathtree[i)) { if (!client.changeworkingdirectory (pathtree[i)) { client.makedirectory (PathTree[i]); client.changeworkingdireCtory (Pathtree[i]); } } } } /** * upload the specified file or directory to the server */ public void upfile (File file) throws Exception { if ( File.isdirectory ()) { Client.makedirectory (File.getname ()); Client.changeworkingdirectory (File.getname ()); file[] files = file.listfiles (); for (FilE row : files) upfile (Row); } else { file infile = new file (File.getpath ()); Fileinputstream input = new fileinputstream (InFile); client.storefile (New string (Infile.getname () getBytes ("GBK"), " Iso-8859-1 "), input); input.close ( ); } }}
This article from the "Job" blog, reproduced please contact the author!
Java:ftp Uploading Files