JAVA Use in FTPClient Upload and download

        stay JAVA In the process , Often need and FTP Dealing with , For example, to FTP Server upload file , Download files , This paper introduces how to use it jakarta
commons Medium FTPClient( stay commons-net In the bag ) Upload and download files .

One , Upload files

         The principle will not be introduced , Let's look at the code directly
/** * Description: towards FTP Server upload file * @Version1.0 Jul 27, 2008 4:31:09 PM by
Cui Hongbao (cuihongbao@d-heaven.com) establish * @param url FTP The server hostname * @param port
FTP Server port * @param username FTP Login account * @param password FTP Login password * @param path
FTP Server save directory * @param filename Upload to FTP The file name on the server * @param input Input stream * @return
Successful return true, Otherwise return false */ public static boolean uploadFile(String url,int
port,String username, String password, String path, String filename,
InputStream input) { boolean success = false; FTPClient ftp = new FTPClient();
try { int reply; ftp.connect(url, port);// connect FTP The server
// If the default port is used , have access to ftp.connect(url) The way to connect directly FTP The server ftp.login(username, password);// Sign in
reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect(); return success; } ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input); input.close(); ftp.logout(); success = true; }
catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected())
{ try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }
 

Let's write two small examples :

1. Upload local files to FTP Server , The code is as follows :
@Test public void testUpLoadFromDisk(){ try { FileInputStream in=new
FileInputStream(new File("D:/test.txt")); boolean flag =
uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", in);
System.out.println(flag); } catch (FileNotFoundException e) {
e.printStackTrace(); } }
2. stay FTP A file is generated on the server , And write a string to the file
@Test public void testUpLoadFromString(){ try { InputStream input = new
ByteArrayInputStream("test ftp".getBytes("utf-8")); boolean flag =
uploadFile("127.0.0.1", 21, "test", "test", "D:/ftp", "test.txt", input);
System.out.println(flag); } catch (UnsupportedEncodingException e) {
e.printStackTrace(); } }

Two , Download files

       from FTP The code for the server to download the file is also very simple , The reference is as follows :
/** * Description: from FTP Server download file * @Version1.0 Jul 27, 2008 5:32:36 PM by
Cui Hongbao (cuihongbao@d-heaven.com) establish * @param url FTP The server hostname * @param port
FTP Server port * @param username FTP Login account * @param password FTP Login password * @param
remotePath FTP Relative path on server * @param fileName File name to download * @param localPath
Save to local path after download * @return */ public static boolean downFile(String url, int
port,String username, String password, String remotePath,String fileName,String
localPath) { boolean success = false; FTPClient ftp = new FTPClient(); try {
int reply; ftp.connect(url, port); // If the default port is used , have access to ftp.connect(url) The way to connect directly FTP The server
ftp.login(username, password);// Sign in reply = ftp.getReplyCode(); if
(!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; }
ftp.changeWorkingDirectory(remotePath);// Transfer to 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(); success = true; } catch (IOException e) { e.printStackTrace();
} finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch
(IOException ioe) { } } } return success; }

Technology
©2019-2020 Toolsou All rights reserved,
Send love - A little romance for programmers VHDL—— Design of frequency divider Python Implementation of Hanoi Tower code It's over , Starting salary 30khtml+css+js Make a simple website home page QQ Login interface implementation Hill sorting of sorting algorithm ——c++ realization 【 Wechat applet learning 】 Netease music cloud code page implementation details Resume the 13th session python Blue Bridge Cup 2022 Solution to the 13th Blue Bridge Cup ( whole )