Java FTPClient implements file upload and download
- 2020-05-09 18:33:44
- OfStack
In the JAVA program, you often need to deal with FTP, such as uploading and downloading files to the FTP server. This article briefly describes how to make use of FTPClient in jakarta commons (in the commons-net package) to upload and download files.
The jar packages used are:
commons-net-1.4.1.jar
jakarta-oro.jar
1. Upload files
File upload source code
/**
* Description: to FTP Server upload file
* @Version1.0
* @param url FTP The server hostname
* @param port FTP Server port
* @param username FTP Login account
* @param password FTP The login password
* @param path FTP Server save directory
* @param filename Uploaded to the FTP The file name on the server
* @param input The input stream
* @return Successfully returns true Otherwise return false
*/
public static boolean uploadFile(
String url,//FTP The server hostname
int port,//FTP Server port
String username, // FTP Login account
String password, //FTP The login password
String path, //FTP Server save directory
String filename, // Uploaded to the FTP The file name on the server
InputStream input // The input stream
) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);// The connection FTP The server
// If the default port is used, it can be used ftp.connect(url) The way of direct connection FTP The server
ftp.login(username, password);// The login
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;
}
Here are the test cases for file upload:
/**
* Upload the local file to FTP On the server
*
*/
public void testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("D:/test.txt"));
boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* in FTP Generate on the server 1 File and will 1 Two strings are written to the file
*
*/
public void testUpLoadFromString(){
try {
String str = " This is the string to write! ";
InputStream input = new ByteArrayInputStream(str.getBytes("utf-8"));
boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", input);
System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
2. File download
File download the source code
/**
* Description: from FTP Server download file
* @Version1.0
* @param url FTP The server hostname
* @param port FTP Server port
* @param username FTP Login account
* @param password FTP The login password
* @param remotePath FTP The relative path on the server
* @param fileName The file name to download
* @param localPath Save to the local path after downloading
* @return
*/
public static boolean downFile(
String url, //FTP The server hostname
int port,//FTP Server port
String username, //FTP Login account
String password, //FTP The login password
String remotePath,//FTP The relative path on the server
String fileName,// The file name to download
String localPath// Save to the local path after downloading
) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// If the default port is used, it can be used ftp.connect(url) The way of direct connection FTP The server
ftp.login(username, password);// The login
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// Transferred to the 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;
}
Here are the test cases for file download:
/**
* will FTP The file on the server is downloaded locally
*
*/
public void testDownFile(){
try {
boolean flag = downFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", "D:/");
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
The above is the entire content of this article, I hope to help you with your study.