• Post Reply Bookmark Topic Watch Topic
  • New Topic

FTP Client  RSS feed

 
Peter Phung
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm trying to write an FTP Client application but i am having some trouble with a couple of my methods.
The first problem is i need to check if a directory already exists on a FTP server.
My code is currently :
try {
URLConnection connect = url_.openConnection();
connect.getContent();
taDisplay.appendText("\nexists!!");
dex = 0;
}
catch (IOException urlex){
taDisplay.appendText("\nnot exists");
dex = 1;
}
I'm trying to check the connection to the URL but it always returns "exists!!". Does anyone know what i'm doing wrong or if there is a better way for checking existing directories?
The second problem is that i'm also trying to create a directory on the remote server. I've extended sun.net.ftp.FtpClient.class to include a make directory method but its not working. Because i'm not an expert at FTP i'm not sure if i've used the right syntax.
Here's the code :
public class FClient extends FtpClient {
public FClient(String host) throws IOException{
super(host);
}
public void mkDir(String dir) {
try {
issueCommand("MKD "+dir);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Can anyone help, Please.
Pete
 
William Sbordoni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !!!
to resolve your problem with creating directory try
ftp.sendServer("mkd "+directory+"\r\n");
 
William Sbordoni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !!!
to check if directory exist, I recommend that you create a method that can return a throw.
public void createDirectory(String name) throws YourExceptionCreated {
try {
ftpClient.sendServer("mkd "+name+"\r\n");
} catch (Throwable t) {
throw new YourExceptionCreated("Problens creating directory: " + t.getMessage());
}
}
 
Juanjo Bazan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using the sun.net.ftp package, maybe you should read this.
[ June 27, 2003: Message edited by: Juanjo Bazan ]
 
Van Glass
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option would be to try and set your remote directory to the directory you want to check exists. If the directory does not exist then an error will be returned.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!