September 04, 2010, 08:32:27 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: ftp files listing  (Read 1097 times)
0 Members and 1 Guest are viewing this topic.
jazz2k8
Yo Maaan Cheer Up...
Member
*

Reputation: 0
Offline Offline
Posts: 82
Referrals: 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 :: a community about Java software development.
« on: December 24, 2008, 09:08:27 AM »

 Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

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

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
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

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

Wow, cool. I like it a lot.
Logged
Javaforums.net :: a community about Java software development.
   

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

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.44 seconds with 36 queries.

Google visited last this page August 30, 2010, 06:15:02 PM