Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: connect to ftp

  1. #1
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default connect to ftp

    Hi.
    I could really need some help.
    Im trying to connect and log in to an ftp-server but it doesnt work.
    My code for connect is:
    PHP Code:
    try{
    client.connect("ftp://83.209.73.115/");
        JOptionPane.showMessageDialog(panel,"Connected" );
     } catch (IOException ex) {
        }
    But the message never appears. So i dont know if im connected or not.
    And my code for login is:

    PHP Code:
        
            try{ 
             
            String us; 
        us=username.getText(); 
        String pass; 
        pass=passw.getText(); 
    boolean login = client.login("us", "pass"); 
    
        if (login) { 
            JOptionPane.showMessageDialog(panel,"Login success"); 
    } catch (IOException ex) { 
        }
    But when i press the button for login i get alot of exceptions.
    PHP Code:
    --------------------Configuration: Project - JDK version 1.6.0_14 <Default> - <Default>--------------------
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:471)
        at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:534)
        at org.apache.commons.net.ftp.FTP.user(FTP.java:683)
        at org.apache.commons.net.ftp.FTPClient.login(FTPClient.java:628)
        at GUI2.login(GUI2.java:270)
        at GUI2$buttonListener.actionPerformed(GUI2.java:75)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6263)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
        at java.awt.Component.processEvent(Component.java:6028)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4630)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
        at java.awt.Container.dispatchEventImpl(Container.java:2085)
        at java.awt.Window.dispatchEventImpl(Window.java:2475)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    First i thought client is NULL, but its not.
    Can someone help me?

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    What is the code in this line?

    GUI2.java:270

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    us and pass value have u tried to print before sending to ftp?

    Please paste the code too for clear picture.
    Ramya:cool:

  4. #4
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Here is all my code. Lots of trash though.
    The code on that line is: boolean login = client.login("secret", "secret");

    PHP Code:
    //import sun.net.ftp.FtpClient;
    import com.jscape.inet.ftp.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.security.*;
    import java.io.IOException;  
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import org.apache.commons.net.tftp.TFTP;
    import org.apache.commons.net.ftp.FTPClient;
    
    
    public class GUI2 extends JFrame
    {
    	private Container cont;
    	private JPanel panel;
    	private JPanel panel2; 
    	private JTextField file;
    	private JTextField username;
    	private JTextField passw;
    	private JButton connect;
    	private	JButton login;
    	public JOptionPane hej=new JOptionPane();
    	private FTPClient client;
    	
    	remove2 rev=new remove2();
    	  public static int BUFFER_SIZE = 10240;
    	
    	  private FTPClient m_client;
    	
    	/*  private String host = "";
    	
    	  private String user = "";
    	
    	  private String password = "";
    	
    	  private String sDir = "";
    */
    	
    	
    	private class buttonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			String btnText=e.getActionCommand();
    
    			if(btnText.equals("Connect"))
    				{
    					connect();
    				/*	try {
    
    
    					
    				*/	
    				}
    				
    				if(btnText.equals("Log in"))
    				{
    					login();
    				}
    		}
    	}
    		
    	public GUI2()
    	{
    		client = new FTPClient();
    		file=new JTextField();
    		username=new JTextField();
    		passw=new JTextField();
    		buttonListener bl=new buttonListener();
    		cont=getContentPane();
    		setTitle("Project");
    		setSize(400,300);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		cont.setLayout(new GridLayout(1,1));
    		panel=new JPanel();
    		panel.setLayout(new GridLayout(4,3));
    		panel2=new JPanel();
    		panel2.setLayout(new GridLayout(2,2));
    		
    		JButton connect=new JButton();
    		JButton login=new JButton();
    		JLabel empty1=new JLabel("");
    		JLabel empty2=new JLabel("");
    		JLabel empty3=new JLabel("");
    		JLabel empty4=new JLabel("");
    		JLabel empty5=new JLabel("");
    		JLabel empty6=new JLabel("");
    		JLabel empty7=new JLabel("");
    		JLabel empty8=new JLabel("");
    		JLabel empty9=new JLabel("");
    		JLabel empty10=new JLabel("");
    		JLabel empty11=new JLabel("");
    		JLabel empty12=new JLabel("");
    		JLabel empty13=new JLabel("");
    		JLabel empty14=new JLabel("");
    		JLabel empty15=new JLabel("");
    		JLabel empty16=new JLabel("");
    		JLabel empty17=new JLabel("");
    		JLabel empty18=new JLabel("");
    		JLabel empty19=new JLabel("");
    		JLabel empty20=new JLabel("");
    		JLabel empty21=new JLabel("");
    		JLabel empty22=new JLabel("");
    		JLabel empty23=new JLabel("");
    		
    		connect=new JButton("Connect");
    		cont.add(panel);
    		connect.addActionListener(new buttonListener());
    		login=new JButton("Log in");
    		login.addActionListener(new buttonListener());
    		panel.add(connect);
    		panel.add(empty1);
    		panel.add(empty2);
    		//panel.add(empty3);
    		panel.add(login);
    		panel.add(empty4);
    		panel.add(empty5);
    		//panel.add(empty6);
    		//panel.add(empty7);
    		panel.add(username);
    		panel.add(passw);
    		panel.add(empty8);
    		panel.add(empty9);
    		panel.add(empty10);
    		/*panel.add(empty11);
    		panel.add(empty12);
    		panel.add(empty13);
    		panel.add(empty14);
    		panel.add(empty15);
    		panel.add(empty16);
    	
    		
    		panel.add(file);
    		panel.add(empty17);
    		panel.add(empty18);
    		panel.add(empty19);
    		panel.add(empty20);
    		panel.add(empty21);
    		
    		panel.add(empty22);
    		panel.add(empty23);*/
    			
    		
    		
    		
    
    	}
    
    	void connect()
    	{
    		try{
    		// create Ftp instance with FTP hostname, username and password as arguments
    //Ftp ftp = new Ftp("ftp://83.209.73.115/","secret","secret");
    
    // establish connection
    //ftp.connect();
    /*}
            catch(Exception e) {
                e.printStackTrace();
            }
    */		
    	
    	//JOptionPane.showMessageDialog(panel, "hej");
    	
       // FileInputStream fis = null;
    
        client.connect("ftp://83.209.73.115/");
        JOptionPane.showMessageDialog(panel,"Connected to " );
          JOptionPane.showMessageDialog(panel,client.getReplyString());
    
          // After connection attempt, you should check the reply code to verify
          // success.
    /*      reply = client.getReplyCode();
    
          if(!FTPReply.isPositiveCompletion(reply)) {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
          }*/
    
        //client.login("secret", "secret");
    	JOptionPane.showMessageDialog(panel,"Logged in");
    	
        
       /* String filename = "C:\\Users\\Anna\\test.txt";
        fis = new FileInputStream(filename);
        client.storeFile(filename, fis);
        client.logout();
        fis.close();*/
    	/*try{
    	
        FTPClient client = new FTPClient();
    
        client.connect("ftp://83.209.73.115");
        boolean login = client.login("annam", "secret");
    
        if (login) {
          System.out.println("Login success...");
          boolean logout = client.logout();
          if (logout) {
            System.out.println("Logout from FTP server...");
          }
        } else {
          System.out.println("Login fail...");
        }
        client.disconnect();*/
        } catch (IOException ex) {
        }
        
       /* try
        {
        
        FTPClient client = new FTPClient();
        FileOutputStream fos = null;
    
        client.connect("ftp://83.209.73.115");
        client.login("secret", "secret");
    
    	String fileName;
        fileName=file.getText();
        fos = new FileOutputStream(fileName);
    
        client.retrieveFile("/" + fileName, fos);
        fos.close();
       */
        
       /* boolean deleted = client.deleteFile(fileName);
        
        
        FileInputStream fis = null;
        fis = new FileInputStream(fileName + ".xml");
        client.storeFile(fileName, fis);
        fis.close();
        */
      /*  client.disconnect();
    	
    	*/
    	}
    	
    	void login()
    	{
    		
             
            try{ 
             
            String us; 
        us=username.getText(); 
        String pass; 
        pass=passw.getText(); 
    
    if(client == null) JOptionPane.showMessageDialog(panel,"client is null, this is going to crash");
    else  JOptionPane.showMessageDialog(panel,"client is not null");
        boolean login = client.login("secret", "secret"); 
    JOptionPane.showMessageDialog(panel,us); 
        if (login) { 
            //panel.add(panel2); 
            //JOptionPane.showMessageDialog(panel,"Login success"); 
          /*boolean logout = client.logout(); 
          if (logout) { 
            System.out.println("Logout from FTP server..."); 
          }*/
        } else { 
         JOptionPane.showMessageDialog(panel,"Login fail"); 
        } 
         
         } catch (IOException ex) { 
        }  
    	/*	try{
    		
    		String us;
    	us=username.getText();
    	String pass;
    	pass=passw.getText();
    	boolean login = client.login(us, pass);
    
        if (login) {
        	//panel.add(panel2);
        	JOptionPane.showMessageDialog(panel,"Login success");
          /*boolean logout = client.logout();
          if (logout) {
            System.out.println("Logout from FTP server...");
          }
        } else {
         JOptionPane.showMessageDialog(panel,"Login fail");
        }
    	
    	 } catch (IOException ex) {
        }*/
    	}
    }

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Iam able to see lot of comments than the real code.Please send the correct code wit printstacktrace and properly put try-catch with exception block
    Ramya:cool:

  6. #6
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    PHP Code:
    import com.jscape.inet.ftp.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.security.*;
    import java.io.IOException;  
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    import org.apache.commons.net.tftp.TFTP;
    import org.apache.commons.net.ftp.FTPClient;
    
    
    public class GUI2 extends JFrame
    {
    	private Container cont;
    	private JPanel panel;
    	private JPanel panel2; 
    	private JTextField file;
    	private JTextField username;
    	private JTextField passw;
    	private JButton connect;
    	private	JButton login;
    	public JOptionPane hej=new JOptionPane();
    	private FTPClient client;
    	
    	remove2 rev=new remove2();
    	  public static int BUFFER_SIZE = 10240;
    	
    	  private FTPClient m_client;
    	
    	
    	
    	private class buttonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			String btnText=e.getActionCommand();
    
    			if(btnText.equals("Connect"))
    				{
    					connect();
    				
    				}
    				
    				if(btnText.equals("Log in"))
    				{
    					login();
    				}
    		}
    	}
    		
    	public GUI2()
    	{
    		client = new FTPClient();
    		file=new JTextField();
    		username=new JTextField();
    		passw=new JTextField();
    		buttonListener bl=new buttonListener();
    		cont=getContentPane();
    		setTitle("Project");
    		setSize(400,300);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		cont.setLayout(new GridLayout(1,1));
    		panel=new JPanel();
    		panel.setLayout(new GridLayout(4,3));
    		panel2=new JPanel();
    		panel2.setLayout(new GridLayout(2,2));
    		
    		JButton connect=new JButton();
    		JButton login=new JButton();
    		JLabel empty1=new JLabel("");
    		JLabel empty2=new JLabel("");
    		JLabel empty3=new JLabel("");
    		JLabel empty4=new JLabel("");
    		JLabel empty5=new JLabel("");
    		JLabel empty6=new JLabel("");
    		JLabel empty7=new JLabel("");
    		JLabel empty8=new JLabel("");
    		JLabel empty9=new JLabel("");
    		JLabel empty10=new JLabel("");
    		JLabel empty11=new JLabel("");
    		JLabel empty12=new JLabel("");
    		JLabel empty13=new JLabel("");
    		JLabel empty14=new JLabel("");
    		JLabel empty15=new JLabel("");
    		JLabel empty16=new JLabel("");
    		JLabel empty17=new JLabel("");
    		JLabel empty18=new JLabel("");
    		JLabel empty19=new JLabel("");
    		JLabel empty20=new JLabel("");
    		JLabel empty21=new JLabel("");
    		JLabel empty22=new JLabel("");
    		JLabel empty23=new JLabel("");
    		
    		connect=new JButton("Connect");
    		cont.add(panel);
    		connect.addActionListener(new buttonListener());
    		login=new JButton("Log in");
    		login.addActionListener(new buttonListener());
    		panel.add(connect);
    		panel.add(empty1);
    		panel.add(empty2);
    		//panel.add(empty3);
    		panel.add(login);
    		panel.add(empty4);
    		panel.add(empty5);
    		//panel.add(empty6);
    		//panel.add(empty7);
    		panel.add(username);
    		panel.add(passw);
    		panel.add(empty8);
    		panel.add(empty9);
    		panel.add(empty10);
    
    	}
    
    	void connect()
    	{
    		try{
    	
        client.connect("ftp://83.209.73.115");
    } catch (IOException ex) {
        }
        
    	}
    	
    	void login()
    	{
    		
             
            try{ 
             
            String us; 
        us=username.getText(); 
        String pass; 
        pass=passw.getText(); 
    
    if(client == null) JOptionPane.showMessageDialog(panel,"client is null, this is going to crash");
    else  JOptionPane.showMessageDialog(panel,"client is not null");
        boolean login = client.login("secret", "secret"); 
        	} catch (IOException ex) {
        }
    	
         
    	}
    }

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Don't put empty catch blocks. They eat all the exceptions. Put at least e.printStackTrace() in all your catch blocks.
    Then find out which line is reported for the exception and post that line from your code.

  8. #8
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    done. thanks.
    the line reported for the exceptions is: boolean login = client.login("secret", "secret");

    and now also for: client.connect("ftp...");

    I also get the error: java.net.UnknownHostException:

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You will need to fix all those exceptions in order then.
    First the host you are supplying is reported as unknown.
    Read the API specs for the classes you are using to find out what they do under what circumstances.

  10. #10
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    I solved it. i removed "ftp://" from the hostname and it worked.
    Thanks for all help.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You would have fixed it earlier if you had not been swallowing up those exceptions.

  12. #12
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Do you know how i can make the program download, change and upload files to the fpt-server automatically when a new file uploads to the server?

    And a more simple question. I want to use a JList and thought you put text with list.setListData(something); but the compiler says setListData doesnt excists. I have included everything.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Open the API specs for JList and see if the argument the method is expecting is what you are passing.

  14. #14
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    it is. and it worked before in earlier programs.

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Post the exact message you are getting and the line reported.

  16. #16
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    cannot find symbol method setListData(java.lang.String)

    PHP Code:
    {
        		String[] names = client.listNames();
        for (String name : names) {
        	list.setListData(name);

  17. #17
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    forgot a } in that code though

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read my reply #13 above again and then bang your head against a desk once.

  19. #19
    anki.gulsvart is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    i can make it work with System.out.println() but not list.setListData.

    PHP Code:
    String[] names = client.listNames();
        	list = new JList(names);
        	for(int i = 0; i < list.getModel().getSize(); i++) {
        	System.out.println(list.getModel().getElementAt(i));
    but for list.setListData nothing comes up.

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are no longer making much sense.
    The method takes an array or a vector. You passed it a String.

Page 1 of 2 12 LastLast

Similar Threads

  1. JMF: Cannot connect to device
    By jonsamwell in forum New To Java
    Replies: 2
    Last Post: 06-10-2011, 07:20 PM
  2. how to connect with my GUI
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 04-01-2009, 04:59 PM
  3. Connect DB2 with struts
    By anilonwebs in forum Web Frameworks
    Replies: 0
    Last Post: 11-22-2008, 07:24 AM
  4. how connect
    By herfnai in forum JDBC
    Replies: 1
    Last Post: 08-18-2008, 06:34 AM
  5. connect two applet
    By dg_iiita in forum Java Applets
    Replies: 0
    Last Post: 02-15-2008, 01:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •