so, I'm pretty new here so excuse if I make a mistake posting. I'm trying to program a FTP client that logs and uploads files (up to 5 simultaneously) to a FTP server. I'm encountering problems and need help.
this is still work in progress but I am encountering a problem. the client copies the contents of the file (for now I'm just trying to copy 1 file, hence the files[0]) but names it NULL (this is the problem).
here is the code:
Code:public class FTPClient {
public static void main (String[] args){
int i =0;
String userName="user";
String passWord="pass";
String server="127.0.0.1";
String[] files=null;
String delimiter = ";";
while (i < args.length) {
if (i==0 & args[i].equals("-u")) userName=args[++i]; else
if (args[i].equals("-p")) passWord=args[++i]; else
if (args[i].equals("-server")) server=args[++i]; else
if (args[i].equals("-files")) files=args[++i].split(delimiter,5);
i++;
}
Client f= new Client ();
f.setHost(server);
f.setUser(userName);
f.setPassword(passWord);
boolean connected=f.connect();
f.setRemoteFile("novo/u.txt");
if (connected){
if (f.uploadFile(files[0]))
// display the message of success if uploaded
System.out.println(f.getLastSuccessMessage ());
else
System.out.println(f.getLastErrorMessage ());
}
else
// Display any connection exception, if any
System.out.println(f.getLastErrorMessage ());
}
}
I thank you in advance for your help!Code:import java.net.*;
import java.io.*;
public class Client
{
/** The URL connection object */
private URLConnection m_client;
/** The FTP host/server to be connected */
private String host;
/** The FTP user */
private String user;
/** The FTP user’s password */
private String password;
/** The remote file that needs to be uploaded */
private String remoteFile;
/** The previous error message triggered after a method is called */
private String erMesg;
/** The previous success message after any method is called */
private String succMesg;
public Client(){}
/** Setter method for the FTP host/server */
public void setHost (String host)
{
this.host = host;
}
/** Setter method for the FTP user */
public void setUser (String user)
{
this.user = user;
}
/** Setter method for the FTP user’s password */
public void setPassword (String p)
{
this.password = p;
}
/** Setter method for the remote file, this must include the sub-directory path relative
to the user’s home directory, e.g you’e going to download a file that is within a sub directory
called “sdir”, and the file is named “d.txt”, so you shall include the path as “sdir/d.txt”
*/
public void setRemoteFile (String d)
{
this.remoteFile = d;
}
/** The method that returns the last message of success of any method call */
public synchronized String getLastSuccessMessage()
{
if (succMesg==null ) return ""; return succMesg;
}
/** The method that returns the last message of error resulted from any exception of any method call */
public synchronized String getLastErrorMessage()
{
if (erMesg==null ) return ""; return erMesg;
}
/** The method that handles file uploading, this method takes the absolute file path
of a local file to be uploaded to the remote FTP server, and the remote file will then
be transfered to the FTP server and saved as the relative path name specified in method setRemoteFile
@param localfilename – the local absolute file name of the file in local hard drive that needs to
FTP over
*/
public synchronized boolean uploadFile (String localfilename)
{
try{
InputStream is = new FileInputStream(localfilename);
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os =m_client.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] buffer = new byte[1024];
int readCount;
while( (readCount = bis.read(buffer)) > 0)
{
bos.write(buffer, 0, readCount);
}
bos.close();
this.succMesg = "Uploaded!";
return true;
}
catch(Exception ex)
{
StringWriter sw0= new StringWriter ();
PrintWriter p0= new PrintWriter ( sw0, true );
ex.printStackTrace ( p0 );
erMesg = sw0.getBuffer().toString ();
return false;
}
}
/** The method that connects to the remote FTP server */
public synchronized boolean connect()
{
try{
URL url = new URL("ftp://"+user+":"+password+"@"+host+"/"+remoteFile+";type=i");
m_client = url.openConnection();
return true;
}
catch(Exception ex)
{
StringWriter sw0= new StringWriter ();
PrintWriter p0= new PrintWriter ( sw0, true );
ex.printStackTrace ( p0 );
erMesg = sw0.getBuffer().toString ();
return false;
}
}
}
