I know what Los Angeles looks like at 4 a.m. — Kobe Bryant
Introduction to the
File Transfer Protocol (FTP) is a Protocol in the TCP/IP group. The FTP protocol consists of two parts: the FTP server and the FTP client. The FTP server is used to store files. Users can use the FTP client to access resources on the FTP server through FTP. In the development of websites, usually use FTP protocol to upload Web pages or programs to the Web server. In addition, FTP is usually used to transfer large files over the network because of its high transmission efficiency.
By default, FTP uses TCP ports 20 and 21. 20 is used for data transmission and 21 is used for control information transmission. However, whether 20 is used as the data transfer port depends on the transfer mode used by FTP. If active mode is used, the data transfer port is 20. If passive mode is used, the final port to be used must be negotiated between the server and client.
Setting up an FTP Server
FileZilla Server is a small FTP Server software, if you want to play a simple FTP Server, you can try this software, easy and easy to set up an FTP Server, new group configuration, upload and download speed limits, online display and remove users.
Download the installation documentation from baidu cloud disk:
Link: pan.baidu.com/s/1Kr5k-HmI… Extraction code: UYFA
After the server is installed, you can enter ftp://127.0.0.1 in the browser to check whether the setup is successful and enter your user and password:
File upload and download
upload
Upload a picture from drive D to the FTP server
/** * Upload files to the FTP server **@paramHost FTP server hostname *@paramPort FTP server port *@paramUsername FTP login account *@paramPassword FTP login password *@paramBasePath FTP server basic directory *@paramFilePath FTP server filePath. For example, save by date: /2015/01/01. The filePath is basePath+filePath *@paramFilename filename for uploading files to the FTP server *@paramInput Input stream of the file to be uploaded locally *@returnReturns true on success, false */ otherwise
public static boolean uploadFile(String host, int port, String username, String password, String basePath,
String filePath, String filename, InputStream input) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);// Connect to the FTP server
// If the default port is used, you can directly connect to the FTP server using ftp.connect(host)
ftp.login(username, password);/ / login
reply = ftp.getReplyCode();
if(! FTPReply.isPositiveCompletion(reply)) { ftp.disconnect();return result;
}
// Switch to the upload directory
if(! ftp.changeWorkingDirectory(basePath + filePath)) {// Create a directory if the directory does not exist
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) continue;
tempPath += "/" + dir;
if(! ftp.changeWorkingDirectory(tempPath)) {if(! ftp.makeDirectory(tempPath)) {return result;
} else{ ftp.changeWorkingDirectory(tempPath); }}}}// Set the upload file type to binary
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// Upload the file
if(! ftp.storeFile(filename, input)) {return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static void main(String[] args) {
try {
FileInputStream inputStream = new FileInputStream(new File("D:\\kebi.jpg"));
boolean flag = uploadFile("127.0.0.1".21."cy01"."cy2016"."/"."/images"."BlackMB.jpg", inputStream);
System.out.println(flag);
} catch(FileNotFoundException e) { e.printStackTrace(); }}Copy the code
If the message “Upload succeeded” is displayed, check whether the uploaded image exists in the FTP server.
download
Download the uploaded image to the root directory of the local disk E
/** * Download files from FTP server *@paramHost FTP server hostname *@paramPort FTP server port *@paramUsername FTP login account *@paramPassword FTP login password *@paramRemotePath Relative path on the FTP server *@paramFileName fileName to download *@paramLocalPath localPath *@return* /
public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
String fileName, String localPath) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
// If the default port is used, you can directly connect to the FTP server using ftp.connect(host)
ftp.login(username, password);/ / login
reply = ftp.getReplyCode();
if(! FTPReply.isPositiveCompletion(reply)) { ftp.disconnect();return result;
}
ftp.changeWorkingDirectory(remotePath);// Switch to the FTP server directory
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static void main(String[] args) {
boolean flag = downloadFile("127.0.0.1".21."cy01"."cy2016"."/images"."BlackMB.jpg"."E:\\");
System.out.println(flag);
}
Copy the code
Download successfully, you can also see the downloaded pictures in disk E.
conclusion
-
FTP is a client/server system. The user connects to a server program running on a remote computer through a client program. The FTP server is the computer that transfers files according to the FTP protocol, and the FTP client is the computer that connects to the FTP server and transfers files according to the FTP protocol with the server. To connect to the FTP server, users need to use the FTP client software. Usually, Windows has the “FTP” command, which is a command line FTP client program. Other common FTP client programs include FileZilla, CuteFTP, Ws_FTP, Flashfxp, and LeapFTP.
-
FTP supports two modes: Standard (PORT mode) and Passive(PASV mode). The FTP client in Standard mode sends the PORT command to the FTP server. The FTP client in Passive mode sends the PASV command to the FTP Server.
-
To log in to the FTP server, you must have an account authorized by the FTP server. That is, you must have a user ID and password to log in to the FTP server and enjoy the services provided by the FTP server.
-
There are two modes of FTP transmission: ASCII transmission mode and binary data transmission mode.