Ftp connection

Hi for all

i created ftp class this class it connect to server and ubload files and downloading but have two problem

1.deleted method not run without compilation error

2.exsplorer method not run without compilation error

please any one tell me about this problem

import java.net.URI;

import java.net.URL;

import java.net.URLConnection;

import java.net.MalformedURLException;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.File;

import java.io.InputStreamReader;

import java.io.IOException;

import java.io.PrintStream;

import java.util.Vector;

publicclass FTPConnection

{

private URL url,urlDownload,urlUpload,urlRemove,urlExplorer;

private URLConnection connection,downloadConnection,uploadConnection;

private BufferedInputStream clintInput,downloadInput,uploadInput;

private PrintStream serverOutput,clientOutput,downloadOutput,uploadOutput;

private BufferedReader serverInput;

private Vector fileName;

private String inputLine;

private Integer counter;

private File listFiles;

public FTPConnection()

{

//No command execute

}

public FTPConnection(String urlConnection)

{

try

{

url=new URL(urlConnection);

}

catch(Exception e)

{

e.printStackTrace();

}

}

public FTPConnection(URL urlConnection,String urlFileName)

{

try

{

url=new URL(urlConnection+urlFileName);

}

catch(Exception e)

{

e.printStackTrace();

}

}

public FTPConnection(String userName,String password,String hostName)

{

try

{

url=new URL("ftp://" + userName +":" + password +"@" + hostName);

}

catch(Exception e)

{

e.printStackTrace();

}

}

publicboolean Connect()

{

try

{

connection=url.openConnection();

}

catch(Exception e)

{

returnfalse;

}

try

{

serverInput=new BufferedReader(new InputStreamReader(connection.getInputStream()));

serverOutput=new PrintStream(connection.getOutputStream());

returntrue;

}

catch(IOException e)

{

returnfalse;

}

}

publicboolean Connect(String urlConnection)

{

try{

url=new URL(urlConnection);

return Connect();

}

catch(Exception e)

{

returnfalse;

}

}

publicboolean Download(String file,String directory)

{

try

{

urlDownload=new URL(url,file);

}

catch(MalformedURLException mfue)

{

returnfalse;

}

try

{

downloadConnection=urlDownload.openConnection();

downloadInput=new BufferedInputStream(downloadConnection.getInputStream());

downloadOutput=new PrintStream(directory +"\\" + file);

int i = 0;

byte[] bytesIn =newbyte[downloadInput.available()];

while ((i = downloadInput.read(bytesIn)) >= 0){

downloadOutput.write(bytesIn, 0, i);

}

downloadOutput.close();

downloadInput.close();

returntrue;

}

catch(IOException e)

{

returnfalse;

}

}

publicboolean Upload(String file,String directory)

{

try

{

urlUpload=new URL(url,file);

}

catch(MalformedURLException mfue)

{

returnfalse;

}

try

{

uploadConnection=urlUpload.openConnection();

uploadInput=new BufferedInputStream(new FileInputStream(directory +"\\" + file));

uploadOutput=new PrintStream(uploadConnection.getOutputStream());

if (uploadInput.available()!=0){

int i = 0;

byte[] bytesIn =newbyte[uploadInput.available()];

while ((i = uploadInput.read(bytesIn)) >= 0){

uploadOutput.write(bytesIn, 0, i);

}}

uploadOutput.close();

uploadInput.close();

returntrue;

}

catch(IOException ioe)

{

returnfalse;

}

}

public Vector Explorer()

{

try//this method to return the all files in Server as vector but it no work

{

File listFiles=new File(url.getUserInfo()+"@"+url.getHost()+"/");

}

catch(Exception murl)

{

murl.printStackTrace();

returnnull;

}

System.out.println(url.getUserInfo()+"@"+url.getHost());

String listOfFiles[]=listFiles.list();

for (int i=0;i<listOfFiles.length;i++)

{

System.out.println(listOfFiles[i].toString());

fileName.add(listOfFiles[i].toString());

}

return fileName;

}

publicvoid Remove(String fileName)//it method to delete the file on server but not work

{

try

{

urlRemove=new URL(url,fileName);

}

catch(MalformedURLException murl){}

try

{

File file=new File(urlRemove.getFile());

System.out.println(urlRemove.getFile());

file.delete();

}

catch(Exception oie){oie.printStackTrace();}

}

}

thanks

[11538 byte] By [MHanafya] at [2007-11-26 18:29:00]
# 1

I'd suggest keeping your coding standards closer to the [url=http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html]SUN Code Conventions[/url], as it will help you and others to read and understand your code faster.

For example your methods Connect, Download and Remove should start with a lower case letter (connect, download and remove), otherwise it's confusing.

Additionally you've got code like that:

catch(MalformedURLException murl){}

Never, ever do that! At least print out the exception and/or the stack trace (as you do a few lines down). In the (very, very rare) event, that you want to ignore an exception, you should at least put a comment there.

Why exactly are urlRemove, urlUpload & the others member variables? It seems that you only need them in their respective methods, so you should declare them there.

What compilation errors are you getting? Read and post the message, it's usually pretty usefull.

JoachimSauera at 2007-7-9 6:03:08 > top of Java-index,Core,Core APIs...
# 2
thank you for this cautionsno compilation error but the code of remove and explorer methods no work please tell me about this problem
MHanafya at 2007-7-9 6:03:08 > top of Java-index,Core,Core APIs...
# 3

> thank you for this cautions

> no compilation error but the code of remove and

> explorer methods no work please tell me about this

> problem

Please explain in more detail what "no work" means. Does it throw an exception (make sure to log them, otherwise you will not see them). Or does it behave differently from what you expected?

Without any more details we can't help you.

Also reading [url=http://www.catb.org/~esr/faqs/smart-questions.html]How To Ask Questions The Smart Way[/url] might help understanding what kind of details we need to help you.

JoachimSauera at 2007-7-9 6:03:08 > top of Java-index,Core,Core APIs...
# 4

in more details : when call explorer method it return vector of file contain all files in srever

it method return null as no files in server but server contain alot of files

and when call delete method to delete a file in server the file no deleted

please tell me your E-mail to emaild you the hostName , ID and Password to test this class to see the effect

MHanafya at 2007-7-9 6:03:08 > top of Java-index,Core,Core APIs...
# 5
You have a fundamental misconception. The File class only works on locally visible file systems. FTP doesn't provide one of those.
ejpa at 2007-7-9 6:03:08 > top of Java-index,Core,Core APIs...