Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-19-2009, 04:19 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
Default connect to ftp
Hi.
I could really need some help.
I´m 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 i´m 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:
--------------------ConfigurationProject 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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-19-2009, 04:31 PM
Senior Member
 
Join Date: Dec 2008
Posts: 164
Rep Power: 2
dswastik is on a distinguished road
Default
What is the code in this line?

GUI2.java:270
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-19-2009, 04:40 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
us and pass value have u tried to print before sending to ftp?

Please paste the code too for clear picture.
__________________
Ramya
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-19-2009, 04:46 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
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 == nullJOptionPane.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) {
    }*/
    
}

Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-19-2009, 05:02 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
RamyaSivakanth is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-19-2009, 05:08 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
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 == nullJOptionPane.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) {
    }
    
     
    }

Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-19-2009, 06:21 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-19-2009, 08:17 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
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:
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-20-2009, 12:00 AM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-20-2009, 11:25 AM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
Default
I solved it. i removed "ftp://" from the hostname and it worked.
Thanks for all help.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 10-20-2009, 12:18 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
Default
You would have fixed it earlier if you had not been swallowing up those exceptions.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 10-20-2009, 08:04 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 10-20-2009, 08:11 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
Default
Open the API specs for JList and see if the argument the method is expecting is what you are passing.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 10-20-2009, 08:12 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
Default
it is. and it worked before in earlier programs.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 10-20-2009, 08:23 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
Default
Post the exact message you are getting and the line reported.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 10-20-2009, 08:46 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
Default
cannot find symbol method setListData(java.lang.String)

PHP Code:
{
            
String[] names client.listNames();
    for (
String name names) {
        list.
setListData(name); 
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 10-20-2009, 08:47 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
Default
forgot a } in that code though
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 10-20-2009, 08:55 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
Default
Read my reply #13 above again and then bang your head against a desk once.
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 10-20-2009, 09:06 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
anki.gulsvart is on a distinguished road
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< list.getModel().getSize(); i++) {
        
System.out.println(list.getModel().getElementAt(i)); 
but for list.setListData nothing comes up.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 10-20-2009, 09:14 PM
Senior Member
 
Join Date: Aug 2009
Posts: 1,760
Rep Power: 2
r035198x is on a distinguished road
Default
You are no longer making much sense.
The method takes an array or a vector. You passed it a String.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to connect with my GUI doha786 New To Java 2 04-01-2009 05:59 PM
Connect DB2 with struts anilonwebs Web Frameworks 0 11-22-2008 08:24 AM
JMF: Cannot connect to device jonsamwell New To Java 1 08-26-2008 04:23 AM
how connect herfnai Database 1 08-18-2008 07:34 AM
connect two applet dg_iiita Java Applets 0 02-15-2008 02:59 PM


All times are GMT +2. The time now is 06:27 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org