Java Get Server (FTP) files within the specified folder

Source: Internet
Author: User
Tags ftp client

Package common.util;

Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.ArrayList;

Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import Org.apache.commons.net.PrintCommandListener;
Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPFile;
Import org.apache.commons.net.ftp.FTPReply;
Import Org.apache.log4j.Logger;

/**
* List all files under the specified directory on the FTP server
*/
public class Ftplistallfiles {
private static Logger Logger = Logger.getlogger (Ftplistallfiles.class);
Public ftpclient FTP;
Public arraylist<string> Arfiles;
private static final String far_server_url= "***.***.***.***";
private static final int server_port=21;
private static final String server_user= "* * *";
private static final String server_pwd= "* * *";
private static final String path= "/123/456/";
private static final String showpath= "";


/**
* Log in to the FTP server
* @param host Ftpserver IP address
* @param Port Ftpserver
* @param username ftpserver login Username
* @param password ftpserver login password
* @return Login is successful
* @throws IOException
*/
public boolean login (String host,int port,string username,string password) throws ioexception{
This.ftp.connect (Host,port);
if (Ftpreply.ispositivecompletion (This.ftp.getReplyCode ())) {
if (This.ftp.login (username, password)) {
This.ftp.setControlEncoding ("GBK");
return true;
}
}
if (this.ftp.isConnected ()) {
This.ftp.disconnect ();
}
return false;
}

/**
* Close Data Link
* @throws IOException
*/
public void disconnection () throws ioexception{
if (this.ftp.isConnected ()) {
This.ftp.disconnect ();
}
}

/**
* Recursive traversal of all files under the directory
* @param pathName need to traverse the directory, must start and end with "/"
* @throws IOException
*/
public string List (String pathName) throws ioexception{

StringBuffer filename=new StringBuffer ();
if (Pathname.startswith ("/") &&pathname.endswith ("/")) {
String directory = PathName;
Change directory to current directory
This.ftp.changeWorkingDirectory (directory);
Ftp.enterlocalpassivemode ();
ftpfile[] files = this.ftp.listFiles ();
if (files!=null) {
for (int i = 0; i < files.length; i++) {
if (Files[i].isfile ()) {
String N=new string (Files[i].getname (). GetBytes ("GBK"), "Utf-8");
if (i==files.length-1) {
Filename.append (n+ "," +showpath);
}else{
Filename.append (n+ ",");
}

}
}
}
}
return filename.tostring ();
}

Gets the file name within the specified folder
public static String GetFileNames () {
String names= "";
Ftplistallfiles f = new ftplistallfiles ();
try {
if (F.login (Far_server_url, Server_port, Server_user, server_pwd)) {
names= f.list (path);
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally{

try {
F.disconnection ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

return names;
}
Test
/* public static void Main (string[] args) throws IOException {
System.out.println (GetFileNames ());
} */
}

Note: Code that is marked in red is important if the FTP environment that is built by Linux does not turn on Passive mode to transfer data, the file name within the specified folder is generally not read.

After finding the relevant knowledge on the Internet, the statement has the following functions:

Call Ftpclient.enterlocalpassivemode (); This means that the FTP client tells the FTP server to open a port to transfer data before each data connection. Why do you do this, because FTP server may open different ports each time to transfer data, but on Linux, due to security restrictions, some ports may not be turned on, so there is blocking.

Java Get Server (FTP) files within the specified folder

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.