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:
GeSHi (java):
String ftpHost =
"123.xx.xx.yyy";
//hostipadress String ftpUserName =
"abcdef";
//username String ftpPassword =
"passwoed";
FTPClient ftp = new FTPClient();
ftp.connect(host);
ftp.login(user,pwd);
String name
[] = ftp.
listNames();
if(name.length > 0)
{
for( int i=0; i<name.length; i++ ){
out.println(name[i]);
}
}
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 )
GeSHi (java):
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.util.Vector;
public class FileDirectoryStructure {
static int j = 0;
static int k = 0;
static String urlToSave =
"c:\\ftpFile.txt";
public static void main
(String[] args
) { copyRight();
System.
out.
println("###############\n" +
"#Example usage#\n" +
"FTP username:\t\tyourusername\n"+
"FTP password:\t\tyourpassword\n"+
"Host:\t\t\tengineeringserver.com\n"+
"Directory to scan:\tpublic_html\n"+
"##############\n");
FileDirectoryStructure fds = new FileDirectoryStructure();
Scanner sc =
new Scanner
(System.
in);
System.
out.
print("FTP username: ");
System.
out.
print("FTP password: ");
String userPassword = sc.
next();
System.
out.
print("Directory to scan: ");
String userDirectory = sc.
next();
System.
out.
print("Making connecting, please wait.\n\n");
fds.connectToFTPAndSaveStructure(userName,userPassword,userHost,userDirectory);
//fds.scanLocalDisk();
}
try {
byte[] buf = new byte[4096];
URL u =
new URL("ftp://" + userName +
":" +userPassword +
"@" +userHost +
":21/" + userDirectory
);
uc.setDoInput(true);
if(!saveFiles.exists()){
saveFiles.createNewFile();
}
for(int i = in.read(buf); i > -1; i = in.read(buf)){
d.write(buf, 0 , i);
}
in.close();
OS.close();
System.
out.
println("File saved!");
//e.printStackTrace();
System.
out.
println("Something happened, please check for correct input!");
}
}
public static void copyRight(){
System.
out.
println("*****************");
System.
out.
println("FTP Directory Structure");
System.
out.
println("By: HappyFace @ www.engineeringserver.com");
System.
out.
println("This is a FREE version, but do not distribute without license!");
System.
out.
println("Contact mail: info@engineeringserver.com");
System.
out.
println("Ver: 02/April/2008");
System.
out.
println("*****************");
}
}
Created by GeSHI 1.0.7.20