A
ankit.akg007
Hello Frnds
I was trying to do ftp using sun.net.ftp.*; but while doing ftp its
not upload directories in proper structural way inspite of it it just
uploading the files which are in nested directory altogether
means i am not getting same directory structure as on client side....
Please Help me...
Here is the code attached
import java.io.*;
import sun.net.ftp.*;
class FTPServer{
private String path="/C/remote";
//private String path="C:\\a_h\\cmaf2\\repository\\masterRepository\
\srcRepository" ;
private String userName="superuser";
private String passWd="nopasswd";
public static int BUFFER_SIZE = 10240;
FtpClient fc;
public void connect()
{
try{
fc=new FtpClient("192.168.61.84");
fc.login(userName,passWd);
System.out.println("Connected");
fc.cd(path);
System.out.println("Path Changed");
fc.binary();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void disconnect()
{
if(null!=fc)
{
try{
fc.cd("/");
fc.closeServer();
System.out.println("DisConnected");
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void putFile(String file_name)
{
try {
byte[] buffer = new byte[BUFFER_SIZE];
File file=new File(file_name);
FileInputStream in = new
FileInputStream(file_name);
String
file_separator=System.getProperty("file.separator");
int
last_index=file_name.lastIndexOf(file_separator);
OutputStream out =
fc.put(file_name.substring(last_index+1));
int counter = 0;
while (true) {
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
System.out.println(counter);
}
out.flush();
out.close();
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void visitAllDirsAndFiles(File dir) {
if (dir.isDirectory()) {
//putFile(dir.getAbsolutePath());
File[] children = dir.listFiles();
for (int i=0; i<children.length; i++) {
visitAllDirsAndFiles(children);
}
// exiting from the directory
}else
{
String fileName=dir.getAbsolutePath();
putFile(fileName);
//System.out.println(fileName);
}
}
public static void main(String args[]){
FTPServer myserver=new FTPServer();
myserver.connect();
myserver.visitAllDirsAndFiles(new File("C:\\ULDC"));
myserver.disconnect();
}
}
I was trying to do ftp using sun.net.ftp.*; but while doing ftp its
not upload directories in proper structural way inspite of it it just
uploading the files which are in nested directory altogether
means i am not getting same directory structure as on client side....
Please Help me...
Here is the code attached
import java.io.*;
import sun.net.ftp.*;
class FTPServer{
private String path="/C/remote";
//private String path="C:\\a_h\\cmaf2\\repository\\masterRepository\
\srcRepository" ;
private String userName="superuser";
private String passWd="nopasswd";
public static int BUFFER_SIZE = 10240;
FtpClient fc;
public void connect()
{
try{
fc=new FtpClient("192.168.61.84");
fc.login(userName,passWd);
System.out.println("Connected");
fc.cd(path);
System.out.println("Path Changed");
fc.binary();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void disconnect()
{
if(null!=fc)
{
try{
fc.cd("/");
fc.closeServer();
System.out.println("DisConnected");
}
catch(Exception e){
e.printStackTrace();
}
}
}
public void putFile(String file_name)
{
try {
byte[] buffer = new byte[BUFFER_SIZE];
File file=new File(file_name);
FileInputStream in = new
FileInputStream(file_name);
String
file_separator=System.getProperty("file.separator");
int
last_index=file_name.lastIndexOf(file_separator);
OutputStream out =
fc.put(file_name.substring(last_index+1));
int counter = 0;
while (true) {
int bytes = in.read(buffer);
if (bytes < 0)
break;
out.write(buffer, 0, bytes);
counter += bytes;
System.out.println(counter);
}
out.flush();
out.close();
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void visitAllDirsAndFiles(File dir) {
if (dir.isDirectory()) {
//putFile(dir.getAbsolutePath());
File[] children = dir.listFiles();
for (int i=0; i<children.length; i++) {
visitAllDirsAndFiles(children);
}
// exiting from the directory
}else
{
String fileName=dir.getAbsolutePath();
putFile(fileName);
//System.out.println(fileName);
}
}
public static void main(String args[]){
FTPServer myserver=new FTPServer();
myserver.connect();
myserver.visitAllDirsAndFiles(new File("C:\\ULDC"));
myserver.disconnect();
}
}