Java and FTP help!

Discussion in 'Mac Programming' started by jtalerico, Feb 4, 2006.

  1. macrumors 6502

    Joined:
    Nov 23, 2005
    #1
    Hey guys I am trying to get my Java code to connect via FTP to my site, and upload a file. Not sure how this will work. This is what i have so far!

    Code:
                String wsave = JOptionPane.showInputDialog(null, "File Saved! Would you like to upload it?");
                if(wsave.equals("yes") || wsave.equals("y")) {
                    URL site = new URL("ftp://MYUSER:*******@ftp://69.68.181.132/Sites/txt/; type=i");
                    URLConnection conn = site.openConnection();
                    File output = new File("ftp://69.68.181.132/Sites/txt/");
                    Writer saveSite = new BufferedWriter(new FileWriter(output+input));
                    saveSite.write(text);
                    save.close();
                    conn = null;
                }
            }
    And i am catching the exception, so i am always getting File could not be written... Is there a way to "upload" or BufferedWriter the file on the server? Thanks!
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Opening a connection via the site.openConnection does not have any effect on other classes. So you cannot do that then expect that creating a File with the same address will work. It won't. You will need to do something directly with the URLConnection. What I don't know: I haven't read the documentation.
     
  3. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #3
    These are the errors i am getting now...


    2006-02-04 16:13:23.048 java[4201] CFLog (0): CFMessagePort: bootstrap_register(): failed 1103 (0x44f), port = 0x11f03, name = 'java.ServiceProvider'
    See /usr/include/servers/bootstrap_defs.h for the error codes.
    2006-02-04 16:13:23.051 java[4201] CFLog (99): CFMessagePortCreateLocal(): failed to name Mach port (java.ServiceProvider)

    Here is the new code...

    Code:
                if(wsave.equals("yes") || wsave.equals("y")) {
                    System.out.println("end");
                    URL site = new URL("ftp://xxxxx:xxxxxx@ftp://192.168.0.22/Sites/txt/; type=i");
                    URLConnection conn = site.openConnection();
                    File output = new File(input);
                    BufferedOutputStream os = new BufferedOutputStream(conn.getOutputStream());
                    System.out.println("end");
                    FileOutputStream in = new FileOutputStream(input);
                    os.close();
                   
     
  4. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #4
    hey joe -- you forgot to X out your username and password in your second example.
     
  5. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #5
    Fixed thanks. just changed it :)

    Damn what a day... And thoughts on what is wrong?
     
  6. macrumors 601

    zimv20

    Joined:
    Jul 18, 2002
    Location:
    chicago
    #6
    nah, i haven't done java in 6 years and even then i never messed with connecting to an ftp server. however, a quick google search for java and ftp found this promising page.
     
  7. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
  8. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #8
    Is there somewhere that will tell me how to use this? Install it...
     
  9. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #9
    Well, first put the commons-net-1.4.1.jar file somewhere in the classpath. Then look at the Javadoc or the source link I provided...
     
  10. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #10
    i have put the .jar file into my classpath and i still cannot get the thing to compile with this line....

    import org.apache.commons.net.*;
     
  11. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #11
    Then it's not in the classpath. How are you trying to compile?

    Also, did you extract the .jar file from the .zip file you downloaded from Apache?
     
  12. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #12
    yes, i extracted the jar file from the tar.gz...

    I put the file into the /System/Lib.../Framework/JavaVM..../1.4.2/Classes/

    That should be the classpath...

    BTW Thanks for the help! :)
     
  13. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #13
    I would NOT suggest putting jar files there, instead learn to use the -classpath flag of the javac command. If you are using an IDE, they usually have a similar way to setup the classpath. For example, in Eclipse you can add .jar files as Libraries to a project's build path which for compilation purposes will include the .jar when you try to compile.

    So again, how are you trying to compile this class(es)?
     
  14. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #14
    Okay, i got all that worked out!! Thanks guys! Once again you guys are the greatest!!

    But one more question!!!

    Say i have a password field, and they are entering it in a JOptionPane.. I would like it to be **** instead of the plane text.. How do I do so? Thanks again guys!
     
  15. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #15
    Use JPasswordField...
     
  16. thread starter macrumors 6502

    Joined:
    Nov 23, 2005
    #16
    Yup, already got it.. I was hoping there was some sort of field you could put for Joptionpane because i am not that conserend with security....
     

Share This Page