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,
Result Encapsulation of return result information latex Custom commands in ———\newcommand Programmer and architect Hua Shan pytorch of ResNet18( Yes cifar10 The accuracy of data classification is achieved 94%)el-ui:select Get value Go language Array initialization and basic operations rk3399_android7.1 debugging USB Summary of Bluetooth module 【Python】 read txt file , Gets the specified location data in the specified row ( Essence )2020 year 6 month 26 day C# Class library File read and write operation help class Summary of artificial intelligence algorithm