Results 1 to 5 of 5
Thread: FTP client problems
- 02-20-2012, 01:10 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
FTP client problems
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:
Java 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!Java 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; } } }Last edited by dankobanana; 02-20-2012 at 01:13 AM. Reason: thanks added
- 02-20-2012, 01:30 AM #2
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: FTP client problems
I forgot to mention I'm testing this by using FileZilla Server and "-u test -p test -server 127.0.0.1 -files /var/a.txt;/var/b.txt;/var/c.txt" console input to the program. I can verify the contents of the NULL named file by opening it with notepad and seeing its contents are the same as /var/a.txt (aka files[0] in the code)
- 02-20-2012, 01:37 AM #3
Re: FTP client problems
Where does the client get the name that it is using to name the file?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
- 02-20-2012, 01:57 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: FTP client problems
line 26 in the first bit and line 112 in the 2nd one I guess.
(because I'm a n00b) I'm reusing and adapting this code that I found (I omitted the download part because I do not need it and added my void main)
http://www.ajaxapp.com/2009/02/21/a-...ad-and-upload/Last edited by dankobanana; 02-20-2012 at 02:03 AM.
- 02-20-2012, 01:59 AM #5
Similar Threads
-
Problems to build a Java Client to consume a C# WebService
By slorde in forum Advanced JavaReplies: 6Last Post: 04-04-2013, 10:36 PM -
problems trying to call an EJB's method from a Java Application Client
By ahmedbj in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 01-15-2012, 04:20 PM -
Datagram Client and Server, client timer question
By saru88 in forum NetworkingReplies: 1Last Post: 10-05-2008, 03:12 PM -
Problems with client and server
By Albert in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 06:07 AM -
Problems in running client class
By ai_2007 in forum Advanced JavaReplies: 0Last Post: 06-30-2007, 02:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks