Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

FTP Client

Options
  • 13-04-2007 1:19pm
    #1
    Banned (with Prison Access) Posts: 121 ✭✭


    im looking for a simple ftp client that connects and can upload and download a file. i want it for work so that i can customise it. (they like stuff like that lol)

    ive written a bit of stuff but have had no joy. id love to hook it up to a GUI too.

    Cheers
    public static void main (String[] args)
    	{
    		String serverName;
    		FTPConnection ftp = null;
    		
    		try
    		{
    			if (args.length == 0)
    			{
    				serverName = getStringFromUser("Enter the server you would like to connect to: ");
    				if (serverName.length() == 0)  {  return;  }
    			}  else  {
    				serverName = args[0];
    			}
    			
    			// set the FTPConnection parameter to true if you want to
    			// see debug output in your console window
    			ftp = new FTPConnection(false);
    			System.out.println("Trying to connect anonymously to " + serverName);
    			ftp.connect(serverName);
    			
    			
    			if (ftp.login("username", "PW"))
    			{
    				System.out.println("Successfully logged in!");
    				System.out.println("System type is: " + ftp.getSystemType());
    				System.out.println("Current directory is: " + ftp.getCurrentDirectory());
    				String files = ftp.listFiles();
    				String subDirs = ftp.listSubdirectories();
    				System.out.println("Files in Directory:\n" + files);
    				System.out.println("Subdirectories:\n" + subDirs);
    				
    				// try to change to the first subdirectory
    				StringTokenizer st = new StringTokenizer(subDirs, ftp.lineTerm);
    				String sdName = "";
    				if (st.hasMoreTokens())  { sdName = st.nextToken(); }
    				
    				if (sdName.length() > 0)
    				{
    					System.out.println("Changing to directory " + sdName);
    					if (ftp.changeDirectory(sdName))
    					{
    						// just for kicks, try to download the first 3 files in the directory
    						files = ftp.listFiles();
    						st = new StringTokenizer(files, ftp.lineTerm);
    						
    						String fileName;
    						int count = 1;
    						while ((st.hasMoreTokens()) && (count < 3)) {
    							fileName = st.nextToken();
    							System.out.println("Downloading " + fileName + " to C:\\");
    							try
    							{
    								if (ftp.downloadFile(fileName, "C:\\" + fileName))
    								{
    									System.out.println("Download successful!");
    								}  else  {
    									System.out.println("Error downloading " + fileName);
    								}
    							}  catch(Exception de)  {
    								System.out.println("ERROR: " + de.getMessage());
    							}
    							
    							count++;
    						}
    					}
    					
    				}  else  {
    					System.out.println("There are no Subdirectories!");
    				}
    				
    				ftp.logout();
    				ftp.disconnect();
    				System.out.println("Disconnected and Logged Out.");
    			}  else  {
    				System.out.println("Sorry. Could not connect.");
    			}
    		}  catch(Exception e)  {
    			e.printStackTrace();
    			try { ftp.disconnect(); }  catch(Exception e2)  {}
    		}
    	}
    
    	private static String getStringFromUser(String prompt) throws IOException
    	{
    		System.out.print(prompt);
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		return br.readLine();
    	}
    
    }
    


Comments

  • Banned (with Prison Access) Posts: 121 ✭✭irishpal25


    here is a gui i made. quick question. how would i connect the ftp program to the pane ive left. like run the whole program inside there?
    /****************************************************************/
    /*                      FTPCLIENT	                            */
    /*                                                              */
    /****************************************************************/
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    /**
     * Summary description for FTPCLIENT
     *
     */
    public class FTPCLIENT extends JFrame
    {
    	// Variables declaration
    	private JLabel log;
    	private JLabel info;
    	private JTextField serveradd;
    	private JTextField username;
    	private JTextField password;
    	private JList jList1;
    	private JScrollPane jScrollPane1;
    	private JButton Connect;
    	private JButton disconnect;
    	private JPanel contentPane;
    	//-----
    	private JPanel jPanel1;
    	//-----
    	// End of variables declaration
    
    
    	public FTPCLIENT()
    	{
    		super();
    		initializeComponent();
    		//
    		// TODO: Add any constructor code after initializeComponent call
    		//
    
    		this.setVisible(true);
    	}
    
    	
    	private void initializeComponent()
    	{
    		log = new JLabel();
    		info = new JLabel();
    		serveradd = new JTextField();
    		username = new JTextField();
    		password = new JTextField();
    		jList1 = new JList();
    		jScrollPane1 = new JScrollPane();
    		Connect = new JButton();
    		disconnect = new JButton();
    		contentPane = (JPanel)this.getContentPane();
    		//-----
    		jPanel1 = new JPanel();
    		//-----
    
    		//
    		// log
    		//
    		log.setText("Activity Log");
    		//
    		// info
    		//
    		info.setText("FTP Connection Info");
    		//
    		// serveradd
    		//
    		serveradd.setText("Server Address");
    		serveradd.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				serveradd_actionPerformed(e);
    			}
    
    		});
    		//
    		// username
    		//
    		username.setText("Username");
    		username.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				username_actionPerformed(e);
    			}
    
    		});
    		//
    		// password
    		//
    		password.setText("Password");
    		password.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				password_actionPerformed(e);
    			}
    
    		});
    		//
    		// jList1
    		//
    		jList1.addListSelectionListener(new ListSelectionListener() {
    			public void valueChanged(ListSelectionEvent e)
    			{
    				jList1_valueChanged(e);
    			}
    
    		});
    		//
    		// jScrollPane1
    		//
    		jScrollPane1.setViewportView(jList1);
    		//
    		// Connect
    		//
    		Connect.setText("Connect");
    		Connect.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				Connect_actionPerformed(e);
    			}
    
    		});
    		//
    		// disconnect
    		//
    		disconnect.setText("Disconnect");
    		disconnect.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e)
    			{
    				disconnect_actionPerformed(e);
    			}
    
    		});
    		//
    		// contentPane
    		//
    		contentPane.setLayout(null);
    		addComponent(contentPane, log, 8,174,60,18);
    		addComponent(contentPane, info, 158,77,116,30);
    		addComponent(contentPane, serveradd, 11,40,100,22);
    		addComponent(contentPane, username, 10,94,100,22);
    		addComponent(contentPane, password, 9,123,100,22);
    		addComponent(contentPane, jScrollPane1, 3,198,120,159);
    		addComponent(contentPane, Connect, 237,58,83,28);
    		addComponent(contentPane, disconnect, 334,57,88,28);
    		addComponent(contentPane, jPanel1, 155,101,262,221);
    		//
    		// jPanel1
    		//
    		jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
    		jPanel1.setBorder(BorderFactory.createEtchedBorder());
    		//
    		// FTPCLIENT
    		//
    		this.setTitle("FTP CLIENT");
    		this.setLocation(new Point(-1, -1));
    		this.setSize(new Dimension(474, 405));
    	}
    
    	/** Add Component Without a Layout Manager (Absolute Positioning) */
    	private void addComponent(Container container,Component c,int x,int y,int width,int height)
    	{
    		c.setBounds(x,y,width,height);
    		container.add(c);
    	}
    
    	//
    	// TODO: Add any appropriate code in the following Event Handling Methods
    	//
    	private void serveradd_actionPerformed(ActionEvent e)
    	{
    		System.out.println("\nserveradd_actionPerformed(ActionEvent e) called.");
    		// TODO: Add any handling code here
    
    	}
    
    	private void username_actionPerformed(ActionEvent e)
    	{
    		System.out.println("\nusername_actionPerformed(ActionEvent e) called.");
    		// TODO: Add any handling code here
    
    	}
    
    	private void password_actionPerformed(ActionEvent e)
    	{
    		System.out.println("\npassword_actionPerformed(ActionEvent e) called.");
    		// TODO: Add any handling code here
    
    	}
    
    	private void jList1_valueChanged(ListSelectionEvent e)
    	{
    		System.out.println("\njList1_valueChanged(ListSelectionEvent e) called.");
    		if(!e.getValueIsAdjusting())
    		{
    			Object o = jList1.getSelectedValue();
    			System.out.println(">>" + ((o==null)? "null" : o.toString()) + " is selected.");
    			// TODO: Add any handling code here for the particular object being selected
    			
    		}
    	}
    
    	private void Connect_actionPerformed(ActionEvent e)
    	{
    		System.out.println("\nConnect_actionPerformed(ActionEvent e) called.");
    		// TODO: Add any handling code here
    
    	}
    
    	private void disconnect_actionPerformed(ActionEvent e)
    	{
    		System.out.println("\ndisconnect_actionPerformed(ActionEvent e) called.");
    		// TODO: Add any handling code here
    
    	}
    
    	//
    	// TODO: Add any method code to meet your needs in the following area
    	//
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
    
    //============================= Testing ================================//
    //=                                                                    =//
    //= The following main method is just for testing this class built.=//
    //= After testing,you may simply delete it.                            =//
    //======================================================================//
    	public static void main(String[] args)
    	{
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		JDialog.setDefaultLookAndFeelDecorated(true);
    		try
    		{
    			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    		}
    		catch (Exception ex)
    		{
    			System.out.println("Failed loading L&F: ");
    			System.out.println(ex);
    		}
    		new FTPCLIENT();
    	}
    //= End of Testing =
    
    
    }
    


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Make an instance of your FTPConnection class in the gui.

    Have an onClick event which passes the values of the textboxes into your ftp methods.
    onClick()
    {
        ftp.login(usernamebox.getText(),passbox.getText());
    }
    
    or something like that.


Advertisement