Realize ftp download file to local in Java (detailed):
2020-09-13: Today, I will record the function module that realizes ftp download file to local in java, and share with you what is wrong. You can point out!
1. FTP protocol:
What is FTP? FTP is one of the protocols in the TCP/IP protocol suite, which is the abbreviation of English File Transfer Protocol. This protocol is the basis of Internet file transfer. It consists of a series of specification documents. The goal is to improve file sharing, provide indirect use of remote computers, and make storage media transparent to users and transfer data reliably and efficiently. Simply put, FTP is to complete the copy between two computers, copy files from a remote computer to your own computer, which is called a "download" file. If you copy a file from your own computer to a remote computer, it is called an "upload" file.
2.SSH tools
ssh---ftp----winscp----filezilla-----xftp----
The above is a brief introduction to the ftp protocol and some tools used. In this article, I used sftp to download in java. The class used is ChannelSftp . Let’s get back to the subject and present my own demo. . . .
In this piece, we have finished the first step, first connect to the remote, the code adds the logic of retry (reconnection means), you can go down and understand some of the classes in the code, I will not be too long-winded ( my side Using ChannelSftp, you might as well try another FtpClient class (the big steps are almost the same) )
2. Batch download ( I won’t be too much here , I posted the code directly, you can search for other things, such as delete, there are many single downloads, etc., my project here uses a batch download, so I posted a paragraph Code! )
The first sentence sftp.cd() is a method that everyone knows, cd into the directory ( note: the key point, everyone pays attention: when you want to operate a certain directory file, you must cd to this directory ( windy, no one knows the same ) )
The third sentence sfpt.ls() is a method, everyone knows it, check the file. In the future, the code is to use loops to loop out the file names in the directory. I use the list collection to save them ( note: because I want to get the file at the time, if it is not a multi- level directory, if you are not a multi- level directory , go straight to your logic Just fine.)
Such a multi-level directory!
The second code
This section, as the name suggests, because my first code is to get the name of a multi-level directory, then this section is to get the files in a multi-level directory. Note: The cd method in the last paragraph of the above code. To manipulate multi-level directories, every time you enter a directory and then change to another, you have to enter a directory path under cd in the code!
Today’s ftp download and share is over, if you have anything you don’t understand, welcome to comment and I will reply in seconds! Thank you for your support too! Record life, record knowledge! I am Wang Buzheng !