Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- public class FTP {
- static void listDirectory(FTPClient ftpClient, String parentDir,
- String currentDir, int level) throws IOException {
- //String dirToList = parentDir;
- // if (!currentDir.equals("")) {
- // dirToList += "/" + currentDir;
- // }
- FTPFile[] subFiles = null;
- // ftpClient.listFiles(parentDir);
- try {
- subFiles = ftpClient.listFiles(parentDir);
- }catch(Exception ex){
- ex.printStackTrace();
- }
- if (subFiles != null && subFiles.length > 0) {
- for (FTPFile aFile : subFiles) {
- String currentFileName = aFile.getName();
- if (currentFileName.equals(".")
- || currentFileName.equals("..")) {
- // skip parent directory and directory itself
- continue;
- }
- for (int i = 0; i < level; i++) {
- System.out.print("t");
- }
- if (aFile.isDirectory()) {
- System.out.println("[" + currentFileName + "]");
- listDirectory(ftpClient, parentDir, currentFileName, level + 1);
- } else {
- System.out.println(currentFileName);
- }
- }
- }
- }
- public static void main(String[] args) {
- String server = "10.xxx.xx.xxx"; // ftp server address
- int port = 21; // ftp uses default port Number 21
- String user = "123$ftp";// username of ftp server
- String pass = "123$ftp"; // password of ftp server
- FTPClient ftpClient = new FTPClient();
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- int replyCode = ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(replyCode)) {
- System.out.println("Connect failed");
- return;
- }
- boolean success = ftpClient.login(user, pass);
- if (success) {
- System.out.println("Could not login to the server");
- return;
- }
- String dirToList = "TMS$SYSDEVICE:[TMS$VMS.OPERATOR.PRINT]";
- listDirectory(ftpClient, dirToList, "", 1);
- } catch (IOException ex) {
- System.out.println("Oops! Something wrong happened");
- ex.printStackTrace();
- }catch(Exception ex){
- System.out.println("Oops! Something wrong happened");
- ex.printStackTrace();
- }
- finally {
- // logs out and disconnects from server
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement