Welcome, Guest. Please login or register.
Did you miss your activation email?
Your Ad Here
Pages: [1]   Go Down
  Print  
Author Topic: ftp files listing  (Read 806 times)
0 Members and 1 Guest are viewing this topic.
jazz2k8
Yo Maaan Cheer Up...
Member
*

Reputation: 0
Offline Offline
Posts: 82
Referrals: 0
Activity
0%

WWW Awards
« on: December 24, 2008, 09:08:27 AM »

Hi

I am trying on FTP

I struck at listing the files of a directory...i can list the normal files ..here is the sample code for that:
 
Code
GeSHi (java):
  1. String ftpHost = "123.xx.xx.yyy";  //hostipadress
  2.     String ftpUserName = "abcdef";  //username
  3.     String ftpPassword = "passwoed";
  4.     FTPClient ftp = new FTPClient();
  5.    ftp.connect(host);
  6.            ftp.login(user,pwd);
  7.   String name[] = ftp.listNames();
  8.   if(name.length > 0)
  9.        {
  10.          for( int i=0; i<name.length; i++ ){
  11.  
  12.        out.println(name[i]);
  13. }
  14. }
  15.  
  16.  
Created by GeSHI 1.0.7.20

Logged

visit my blog: www.yoteam.co.cc
Javaforums.net/forum :: A community for software developers
« on: December 24, 2008, 09:08:27 AM »

 Logged
HappyFace
Javaforums.net admin
Senior Member
*

Reputation: 16
Developer @ Engineeringserver network
Online Online
Posts: 2549
Referrals: 12
Activity
12%

WWW Awards
« Reply #1 on: December 24, 2008, 10:01:07 AM »

Hi

I am trying on FTP

I struck at listing the files of a directory...i can list the normal files ..here is the sample code for that:
 
Code
GeSHi (java):
  1. String ftpHost = "123.xx.xx.yyy";  //hostipadress
  2.     String ftpUserName = "abcdef";  //username
  3.     String ftpPassword = "passwoed";
  4.     FTPClient ftp = new FTPClient();
  5.    ftp.connect(host);
  6.            ftp.login(user,pwd);
  7.   String name[] = ftp.listNames();
  8.   if(name.length > 0)
  9.        {
  10.          for( int i=0; i<name.length; i++ ){
  11.  
  12.        out.println(name[i]);
  13. }
  14. }
  15.  
  16.  
Created by GeSHI 1.0.7.20



Try the code below, it logs into the ftp server and lists the files in the directory and also saves the structure on the disk ( c:\ftpFile.txt )

Code
GeSHi (java):
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.ObjectOutputStream;
  6. import java.io.PrintStream;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.util.Scanner;
  10. import java.util.Vector;
  11.  
  12. public class FileDirectoryStructure {
  13. static int j = 0;
  14. static int k = 0;
  15. static String userDir = System.getProperty("user.dir");
  16. static String urlToSave = "c:\\ftpFile.txt";
  17. public static void main(String[] args) {
  18. copyRight();
  19. System.out.println("###############\n" +
  20. "#Example usage#\n" +
  21. "FTP username:\t\tyourusername\n"+
  22. "FTP password:\t\tyourpassword\n"+
  23. "Host:\t\t\tengineeringserver.com\n"+
  24. "Directory to scan:\tpublic_html\n"+
  25. "##############\n");
  26. FileDirectoryStructure fds = new FileDirectoryStructure();
  27.  
  28. Scanner sc = new Scanner(System.in);
  29. System.out.print("FTP username: ");
  30. String userName = sc.next();
  31.  
  32. System.out.print("FTP password: ");
  33. String userPassword = sc.next();
  34.  
  35. System.out.print("Host: ");
  36. String userHost = sc.next();
  37.  
  38.  
  39. System.out.print("Directory to scan: ");
  40. String userDirectory = sc.next();
  41.  
  42. System.out.print("Making connecting, please wait.\n\n");
  43.  
  44. fds.connectToFTPAndSaveStructure(userName,userPassword,userHost,userDirectory);
  45.  
  46. //fds.scanLocalDisk();
  47. }
  48.  
  49. public void connectToFTPAndSaveStructure(String userName, String userPassword, String userHost, String userDirectory){
  50. try {
  51. byte[] buf = new byte[4096];
  52. URL u = new URL("ftp://" + userName + ":" +userPassword + "@" +userHost +":21/" + userDirectory);
  53. URLConnection uc = u.openConnection() ;
  54. uc.setDoInput(true);
  55. InputStream in = uc.getInputStream() ;
  56.  
  57. File saveFiles = new File(urlToSave);
  58. if(!saveFiles.exists()){
  59. saveFiles.createNewFile();
  60. }
  61.  
  62. FileOutputStream saveFile = new FileOutputStream(urlToSave);
  63. for(int i = in.read(buf); i > -1; i = in.read(buf)){
  64. System.out.write(buf, 0 , i);
  65. PrintStream d = new PrintStream(OS);
  66.  
  67. d.write(buf, 0 , i);
  68. }
  69. in.close();
  70. OS.close();
  71. System.out.println("File saved!");
  72.  
  73. } catch (IOException e) {
  74. //e.printStackTrace();
  75. System.out.println("Something happened, please check for correct input!");
  76. }
  77. }
  78.  
  79. public static void copyRight(){
  80. System.out.println("*****************");
  81. System.out.println("FTP Directory Structure");
  82. System.out.println("By: HappyFace @ www.engineeringserver.com");
  83. System.out.println("This is a FREE version, but do not distribute without license!");
  84. System.out.println("Contact mail: info@engineeringserver.com");
  85. System.out.println("Ver: 02/April/2008");
  86. System.out.println("*****************");
  87. System.out.print("\n");
  88. }
  89. }
Created by GeSHI 1.0.7.20
Logged

It doesn't matter how hard you've studied; the material won't be on the exam anyway.
Admin @ www.Engineeringserver.com General Admin @ www.Javaforums.net and it's subsites Forum admin @ www.Javaforums.net Global moderator @ www.Engineeringserver.com network Java / .NET blogger @ www.Engineeringserver.com/blog
XMA performer since 2006 @ http://www.engineeringserver.com/XMA-NaN/ Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://www.javaforums.net...lery.html;sa=media;id=170
http://img222.imageshack....707/arkietomatoesmall.jpg
http://img208.imageshack.us/img208/7141/smalli.png
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
-mirkO
Junior Member
*

Reputation: 0
Offline Offline
Posts: 3
Referrals: 0
Activity
0%

Awards
« Reply #2 on: December 30, 2008, 03:38:54 PM »

Wow, cool. I like it a lot.
Logged
Javaforums.net/forum :: A community for software developers
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to: