In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
            
                    
                
2025-11-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Java how to achieve FTP upload and download function", the content of the article 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 upload and download function" bar!
For JAVA to operate a FTP server, you only need to create a FTPClient, and all operations are encapsulated in FTPClient. JDK comes with FTPClient (sun.net.ftp.FtpClient) or a third-party FTPClient. Generally, apache's FTPClient (org.apache.commons.net.ftp.FTPClient) is used. In this article, the FTPClient,API of apache is more or less the same.
Key dependency: commons-net
Encapsulate common operations (upload and download) into tool classes
Package com.day0322;import org.apache.commons.io.IOUtils;import org.apache.commons.lang3.StringUtils;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPReply;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream / * FTP tool class * File upload * File download * / public class FTPUtil {private static final Logger log = LoggerFactory.getLogger (FTPUtil.class); / * set buffer size 4m * / private static final int BUFFER_SIZE = 1024 * 1024 * 4; / * Local character Encoding * / private static String LOCAL_CHARSET = "GBK" / * UTF-8 character encoding * * / private static final String CHARSET_UTF8 = "UTF-8"; / * OPTS UTF8 string constant * * / private static final String OPTS_UTF8 = "OPTS UTF8"; / * FTP protocol specifies that the file name is encoded as iso-8859-1 * / private static final String SERVER_CHARSET = "ISO-8859-1" Private static FTPClient ftpClient = null; / * Connect to FTP server * / private static void login (OaFtp oaFtp) {ftpClient = new FTPClient (); try {ftpClient.connect (oaFtp.getIp (), Integer.valueOf (oaFtp.getPort (); ftpClient.login (oaFtp.getName (), oaFtp.getPwd ()); ftpClient.setBufferSize (BUFFER_SIZE) FtpClient.setFileType (FTPClient.BINARY_FILE_TYPE); int reply = ftpClient.getReplyCode (); if (! FTPReply.isPositiveCompletion (reply)) {closeConnect ();}} catch (Exception e) {log.error (", e); throw new RuntimeException (e) }} / * close FTP connection * / private static void closeConnect () {if (ftpClient! = null & & ftpClient.isConnected ()) {try {ftpClient.logout (); ftpClient.disconnect ();} catch (IOException e) {log.error ("", e) } / * FTP server path transcoding * * @ param ftpPath FTP server path * @ return String * / private static String changeEncoding (String ftpPath) {String directory = null Try {if (FTPReply.isPositiveCompletion (ftpClient.sendCommand (OPTS_UTF8, "ON")) {LOCAL_CHARSET = CHARSET_UTF8;} directory = new String (ftpPath.getBytes (LOCAL_CHARSET), SERVER_CHARSET);} catch (Exception e) {log.error (", e);} return directory } / * change the working directory * if not, create the working directory * @ param path * / private static void changeAndMakeWorkingDir (String path) {try {ftpClient.changeWorkingDirectory ("/"); path = path.replaceAll ("\", "/"); String [] path_array = path.split ("/") For (String s: path_array) {boolean b = ftpClient.changeWorkingDirectory (s); if (! b) {ftpClient.makeDirectory (s); ftpClient.changeWorkingDirectory (s);} catch (IOException e) {log.error ("", e) Throw new RuntimeException (e);}} / * * upload * @ param oaFtp * @ param filename * @ param in * @ return * / public static boolean upload (OaFtp oaFtp, String filename, String dirPath, InputStream in) {login (oaFtp); if (! ftpClient.isConnected ()) {return false } boolean isSuccess = false; if (ftpClient! = null) {try {if (FTPReply.isPositiveCompletion (ftpClient.sendCommand (OPTS_UTF8, "ON") {LOCAL_CHARSET = CHARSET_UTF8;} ftpClient.setControlEncoding (LOCAL_CHARSET); String path = changeEncoding (dirPath) ChangeAndMakeWorkingDir (path); isSuccess = ftpClient.storeFile (new String (filename.getBytes (), SERVER_CHARSET), in);} catch (Exception e) {log.error (", e);} finally {closeConnect ();}} return isSuccess Download * @ param oaFtp * @ param filename * @ param dirPath * @ param out * @ return * / public static void download (OaFtp oaFtp, String filename, String dirPath, FileOutputStream out) {/ / log in to login (oaFtp); if (ftpClient! = null) {try {String path = changeEncoding (dirPath) ChangeAndMakeWorkingDir (path); String [] fileNames = ftpClient.listNames (); if (fileNames = = null | | fileNames.length = = 0) {return;} for (String fileName: fileNames) {String ftpName = new String (fileName.getBytes (SERVER_CHARSET), LOCAL_CHARSET) If (StringUtils.equals (ftpName,filename)) {InputStream in = ftpClient.retrieveFileStream (fileName); IOUtils.copy (in,out);} catch (IOException e) {log.error ("", e) } finally {closeConnect ();} Test
1. Upload
two。 download
Thank you for your reading, the above is the content of "how to achieve the upload and download function of FTP by Java". After the study of this article, I believe you have a deeper understanding of how to achieve the upload and download function of FTP 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





            
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.