A JAVA code that implements FTP function includes the server setup module, and includes a common method for uploading files to FTP, a common method for downloading files, deleting files, uploading folders on the ftp server, detecting whether the folder exists, etc. Some of the codes may be of reference value for writing JAVA file uploads. Java FTP main file code:
package ftpDemo;import java.io.DataOutputStream;import java.io.InputStream;import java.io.OutputStream;import sun.net.TelnetInputStream;import t sun.net.TelnetOutputStream;import sun.net.ftp.FtpClient;public class ftpUtil {// Upload file to FTP general method public static void upLoadFileFtp(KmConfig kmConfig,InputStream is, String fileName){ try { String ftpHost = kmConfig.getFtpHost (); int port = kmConfig.getFtpPort(); String userName = kmConfig.getFtpUser (); String password = kmConfig.getFtpPassword(); String path = kmConfig.getFtpPath(); FtpClient ftpClient = new FtpClient(ftpHost, port);// ftp Host is the IP address of the FTP server, and port is the login port of the FTP server. ftpHost is String type and port is int type. ftpClient.login(userName, password);// userName and password are the login username and password of the FTP server respectively ftpClient.binary(); ftpClient.cd(path);// path is the path to save the uploaded file on the FTP server. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : TelnetOutputStream telnetOut = ftpClient.put(fileName);// fileName is the uploaded file name DataOutputStream dataOut = new DataOutputStream(telnetOut); byte buffer[] = new byte[ * ]; int count = ; while ((count = is.read (buffer)) != -) { dataOut.write(buffer, , count); } telnetOut.close(); dataOut.close(); ftpClient.closeServer(); } catch (Exception e) { Systeme m.out.println ("Uploading file failed! Please check the system FTP settings and confirm that the FTP service is started"); }}// Delete the file to the FTP general method public static void deleteFileFtp(KmConfig kmConfig,String fileName){ try { String ftpH ost = kmConfig.getFtpHost (); int port = kmConfig.getFtpPort(); String userName = kmConfig.getFtpUser(); String password = kmConfig.getFtpPassword(); String path = km Config.getFtpPath(); FtpClient ftpClient = new FtpClient(ftpHost, port); // ftpHost is the IP address of the FTP server, port is the login port of the FTP server, ftpHost is String type, and port is int type. ftpClient.login(userName, password);// userName and password are the login username and password of the FTP server respectively ftpClient.binary(); ftpClient.cd(path);// path is the path to save the uploaded file on the FTP server. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : try { ftpClient.sendServer("dele " + fileName + "/r/n"); } catch (Exception e) { System.out.println("Delete file failed! Please check the system FTP settings and confirm that the FTP service is started" ); } ftpClient.closeServer(); } catch (Exception e) { System.out.println("Delete file failed!"); }}// Download the ftp file public static void downloadFileFtp(KmConf ig kmConfig,String fileName, String clientFileName , OutputStream outputStream){ try { String ftpHost = kmConfig.getFtpHost(); int port = kmConfig.getFtpPort(); String userName = kmConfig.getFtp User(); String password = kmConfig.getFtpPassword(); String path = kmConfig.getFtpPath( ); FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost is the IP address of the FTP server, port is the login port of the FTP server, ftpHost is String type, and port is int type. ftpClient.login(userName, password);// userName and password are the login username and password of the FTP server respectively ftpClient.binary(); ftpClient.cd(path);// path is the path to save the uploaded file on the FTP server. : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : try { TelnetInputStream in = ftpClient.get(fileName); byte[] bytes = new byte[]; int cnt=; while ((cnt=in.read(bytes,,bytes.length)) != -) { outputStream. write(bytes, , cnt); } outputStream.close(); in.close(); } catch (Exception e) { ftpClient.closeServer(); e.printStackTrace(); } ftpClient. closeServer(); } catch ( Exception e) { System.out.println("Download file failed! Please check the system FTP settings and confirm that the FTP service is started"); }}//Upload the file folder on the ftp server public boolean createDir(String path,FtpClient ftpClient) : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : throws Exception{ //Enter into the home folder ftpClient.cd("/home"); //Create a remote folder//Remote commands include //USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT<br>// PASS PASV STOR REST CWD STAT RMD XCUP OPTS<br>// ACCT TYPE APPE RNFR XCWD HELP XRMD STOU AUTH<br>// REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ<br>// QUIT MODE SYST A BOR NLST MKD XPWD MDTM PROT< br>// Execute commands on the server. If you use sendServer to execute remote commands (local FTP commands cannot be executed), all FTP commands must be added with /r/n<br>// ftpclient.sendServer("XMKD /test /bb/r/n"); //Execute the FTP command on the server<br>// ftpclient.readServerResponse must be called after sendServer<br>// nameList("/test") to obtain the file list in the reference directory <br>// XMKD creates a directory, and when the directory exists, it reports an error when creating the directory again<br>// XRMD deletes the directory<br>// DELE deletes the file<br> // A file folder is passed through remote commands ftpClient.sendServer("MKD "+ path + "/r/n"); //This method must be called between these two methods, otherwise the command will not work.ftpClient.binary(); ftpClient.readServerResponse(); return false; } /** * Check whether the folder exists* @param dir * @param ftpClient * @return */ public boolean isDirExist(String dir, FtpClient ftpClient) { try { ftpClient.cd(dir) ; } catch (Exception e) { return false; } return true; }}The KmConfig.java code is as follows: define FTP server parameters, including logged in username and password.
package ftpDemo;public class KmConfig { //Host ip private String FtpHost = ""; //Port number private int FtpPort; //ftp username private String FtpUser = ""; //ftp Password private String FtpPassword = ""; / Directory in /ftp private String FtpPath = ""; public String getFtpHost() { return FtpHost; } public void setFtpHost(String ftpHost) { FtpHost = ftpHost; } p ublic int getFtpPort() { return FtpPort; } public void setFtpPort(int ftpPort) { FtpPort = ftpPort; } public String getFtpUser() { return FtpUser; } public void setFtpUser(String ftpUser) { FtpUser = ftpUser; } public c String getFtpPassword() { return FtpPassword; } public void setFtpPassword(String ftpPassword) { FtpPassword = ftpPassword; } public String getFtpPath() { return FtpPath; } public void setFtpPath(String ftpPath) { FtpPath = ftpPath; }}Here is the test code:
package ftpDemo;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;public c lass Test { public static void main(String[] args) { //Set ftp KmConfig km = new KmConfig(); km.setFtpHost("..."); km.setFtpPort(); km.setFtpUser("test"); km.setFtpPassword(""); km.setFtpPath( "KM") ; //Upload to ftp ftpUtil util=new ftpUtil(); File file = new File("F:/Article.jpg"); InputStream in; try { in = new FileInputStream(file); util.upLoadFi leFtp(km, in , "Changed name.jpg"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}The above code is the editor to introduce to you the method of Java to implement ftp upload and download, delete files, and upload folders on the ftp server. I hope you like it.