Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Problem in doing FTP a directory by java code

Reply
Thread Tools

Problem in doing FTP a directory by java code

 
 
ankit.akg007@gmail.com
Guest
Posts: n/a
 
      02-08-2008
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[i]);
}
// 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();
}





}
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      02-08-2008
On Fri, 8 Feb 2008 05:03:09 -0800 (PST), http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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....


Instead of doing "put" with an absolute path+filename, probably you
should use fc.cd() to go to the appropriate directory first, and then
do "put" with the basename of the file to upload. You will likely need
to create the directories on the server too if they don't already
exist.

Otherwise, you might find this to be a better choice of ftp client,
since AFAIK the Sun library is undocumented and unsupported:

http://commons.apache.org/net/

/gordon

--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
having problems in changing directories doing ftp in python tedpottel@gmail.com Python 3 07-09-2007 11:29 PM
FTP : Time problem (net/ftp) Vin Raja Ruby 0 06-07-2007 07:47 AM
Problem in doing a backup of database on SQL server through Java code bhagat Java 4 10-05-2005 10:36 AM
Net::FTP problems getting files from Windows FTP server, but not Linux FTP Server. D. Buck Perl Misc 2 06-29-2004 02:05 PM



Advertisments