In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Java how to achieve FTP server file upload and download", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java how to achieve FTP server file upload and download" bar!
Preface
Due to business requirements, it is necessary to implement the file upload and download functions of ftp server. So I made use of commons-net, a toolkit of apache. It is easy to use, but some details are rather awkward, so I would like to explain.
Use
The core tool class of the third-party package is FTPClient, which needs to be connected and logged in, whether it is file upload or file download, which will be reflected in the code.
Preparatory work
Introduce maven dependency
Commons-net commons-net 3.6
Connection information of the ftp server
Hostnam
Port
User name
Password
If you need to build and configure your own file server, please build your own Baidu
Common commands rm-rf directory name mkdir directory name create directory touch file name new file cd directory name switch directory kill-9 process ID kill process chmod configuration permissions file upload
File upload is actually putting our local files in the directory specified by the file server. Imagine what we would do if we created a new folder and file on a linux server at this time.
Create folder mkdir folder name
Enter the cd folder name in the folder
Create a new file touch file name
In fact, the code used by FTPClient to upload files means the same thing, and the main methods used are:
MakeDirectory (path)
ChangeWorkingDirectory (path)
StoreFile (remoteFilename, inputStream)
Correspond to the above three steps respectively.
The following is the complete code, but there are a few details to explain:
Make sure that the logged-in user has sufficient permissions, such as reading and writing files.
The makeDirectory method cannot create multi-level directories, only one layer at a time.
The failure to create the file may be because the user does not have permissions, or the directory already exists.
The problem of Chinese garbled code. Look at the code.
Public static void main (String [] args) {FTPClient ftpClient = new FTPClient (); try {/ / 1. Connect to login ftpClient.connect ("hostname", port); boolean loginSuccessful = ftpClient.login ("user name", "password!"); System.out.println (loginSuccessful? "login success": "login failed"); String remotePath = "Chinese directory"; / / the directory where the uploaded file resides String remotePathAfterTransferCode = new String (remotePath.getBytes ("utf-8"), "iso-8859-1"); / / prevent Chinese garbled String localFile = "/ Users/zzz/Desktop/ftp-server/perfect.txt"; / / Local files to be uploaded String remoteFilename = "remoteFile.txt" / / the file name of the uploaded ftp server / / 2. Create a folder on ftp server: boolean makeDirSuccessful = ftpClient.makeDirectory (remotePathAfterTransferCode); System.out.println ("was the folder created successfully?" + makeDirSuccessful); / / 3. Change the directory boolean changeWorkingDirectorySuccessful = ftpClient.changeWorkingDirectory (remotePathAfterTransferCode); System.out.println (changeWorkingDirectorySuccessful? "change directory successfully": "failed to change directory"); / / 4. Upload local files to ftp server boolean uploadSuccessful = ftpClient.storeFile (remoteFilename, new FileInputStream (new File (localFile); System.out.println (uploadSuccessful? "upload successful": "upload failed"); / / 5. Log in to ftpClient.logout ();} catch (Exception e) {e.printStackTrace ();}}
File download
File downloading also requires the user to connect and log in before downloading.
The core method is retrieveFile ("server file address", output stream)
The specific code is as follows:
Public static void main (String [] args) {FTPClient ftpClient = new FTPClient (); / / specify the local download directory String localPath = "/ Users/zzz/Desktop/ftp-server"; try {ftpClient.connect ("hostname", 21); boolean loginSuccess = ftpClient.login ("username", "password!"); System.out.println (loginSuccess? "login success": "login failed"); / / File download File localFile = new File (localPath + File.separatorChar + "ttt.txt"); OutputStream os = new FileOutputStream (localFile); boolean downloadSuccessful = ftpClient.retrieveFile ("test.txt", os); os.close (); System.out.println (downloadSuccessful? "download successfully!" : "download failed!") ; ftpClient.logout ();} catch (Exception e) {e.printStackTrace ();}} Summary
FTPClient to achieve file upload and download method is very easy to understand, but some details need to pay attention to (common mentioned earlier), of course, the above code is only demo version, the specific requirements need to be optimized, such as exception handling, how to create multi-level folders and so on, and finally, you need to customize the configuration of FTPClient, such as coding, specific API to check the information.
Thank you for your reading, the above is the content of "Java how to upload and download files on FTP server". After the study of this article, I believe you have a deeper understanding of how to achieve file upload and download on FTP server by Java. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.