FTP is the English abbreviation of File Transfer Protocol (File Transfer Protocol), and the Chinese abbreviation is called "Written Transfer Protocol". Used for bidirectional transfer of control files on the Internet. At the same time, it is also an application. There are different FTP applications based on different operating systems, and all of these applications adhere to the same protocol to transfer files. In the use of FTP, users often encounter two concepts: "Download" and "Upload". "Download" files means copying files from the remote host to your own computer; "uploading" files means copying files from your own computer to the remote host. In the Internet language, users can upload (download) files to (from) remote hosts through client programs.
First, Serv-U was downloaded to set up your computer as an FTP file server for easy operation.
1. Download the FTP file (download from the FTP server to the local machine)
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;public class FtpApche {private static FTPClient ftpClient = new FTPClient();private static String encoding = System.getProperty("file.encoding");/*** Description: Download the file from the FTP server* * @Version1.0* @param url* FTP server hostname* @param port* FTP server port* @param username* FTP login account* @param password* FTP login password* @param remotePath* Relative path on the FTP server* @param fileName* File name to download* @param localPath* Path to save to the local after downloading* @return*/public static boolean downFile(String url, int port, String username,String password, String remotePath, String fileName,String localPath) {boolean result = false;try {int reply;ftpClient.setControlEncoding(encoding);/** In order to upload and download Chinese files, some places recommend using the following two sentences instead of * new String(remotePath.getBytes(encoding),"iso-8859-1") transcoding. * After testing, it cannot be passed. */// FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);// conf.setServerLanguageCode("zh");ftpClient.connect(url, port);// If the default port is used, you can directly connect to the FTP server by ftpClient.login(username, password);// Login// Set the file transfer type to binary ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);// Get the ftp login response code reply = ftpClient.getReplyCode();// Verify that the login is successful if (!FTPReply.isPositiveCompletion(reply)) {ftpClient.disconnect();System.err.println("FTP server refused connection.");return result;}// Transfer to the FTP server directory to the specified directory ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));// Get the file list FTPFile[] fs = ftpClient.listFiles();for (FTPFile ff : fs) {if (ff.getName().equals(fileName)) {File localFile = new File(localPath + "/" + ff.getName());OutputStream is = new FileOutputStream(localFile);ftpClient.retrieveFile(ff.getName(), is);is.close();}}ftpClient.logout();result = true;} catch (IOException e) {e.printStackTrace();} finally {if (ftpClient.isConnected()) {try {ftpClient.disconnect();} catch (IOException ioe) {}}}return result;}/*** Download the file on the FTP server to the local * */public void testDownFile() {try {boolean flag = downFile("10.0.0.102", 21, "admin","123456", "/", "ip.txt", "E:/");System.out.println(flag);} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {FtpApche fa = new FtpApche();fa.testDownFile();}}2. Upload of FTP files (upload from the local machine to the FTP server)
import java.io.File;import java.io.FileInputStream;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;public class FTPTest_05 {private FTPClient ftp;/** * * @param path Which path is uploaded to the ftp server* @param addr Address* @param port Port number* @param username Username* @param password Password* @return * @throws Exception */ private boolean connect(String path,String addr,int port,String username,String password) throws Exception {boolean result = false;ftp = new FTPClient();int reply;ftp.connect(addr,port);ftp.login(username,password);ftp.setFileType(FTPClient.BINARY_FILE_TYPE);reply = ftp.getReplyCode();if (!FTPReply.isPositiveCompletion(reply)) {ftp.disconnect();return result;}ftp.changeWorkingDirectory(path);result = true;return result;}/** * * @param file Uploaded file or folder* @throws Exception */ private void upload(File file) throws Exception{if(file.isDirectory()){ftp.makeDirectory(file.getName());ftp.changeWorkingDirectory(file.getName());String[] files = file.list();for (int i = 0;i < files.length;i++) {File file1 = new File(file.getPath()+"//"+files[i] );if(file1.isDirectory()){upload(file1);ftp.changeToParentDirectory();}else{File file2 = new File(file.getPath()+"//"+files[i]);FileInputStream input = new FileInputStream(file2);ftp.storeFile(file2.getName(), input);input.close();}}}else{File file2 = new File(file.getPath());FileInputStream input = new FileInputStream(file2);ftp.storeFile(file2.getName(), input);input.close();}}public static void main(String[] args) throws Exception{FTPTest_05 t = new FTPTest_05();boolean connFlag = t.connect("/", "10.0.0.105", 21, "ls", "123456");System.out.println("connFlag : " + connFlag);File file = new File("D://test02");//The address of the file being transferred by the local machine System.out.println("file : " + file);t.upload(file);System.out.println("upload : " + "ok");}}The above is the example code for Java to implement the upload and download functions of FTP files introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!