Java and FTP help!

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

  1. macrumors 6502

    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!

                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:*******@; type=i");
                    URLConnection conn = site.openConnection();
                    File output = new File("");
                    Writer saveSite = new BufferedWriter(new FileWriter(output+input));
                    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


    Staff Member

    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. macrumors 6502

    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...

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


    hey joe -- you forgot to X out your username and password in your second example.
  5. macrumors 6502

    Fixed thanks. just changed it :)

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


    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


  8. macrumors 6502

    Is there somewhere that will tell me how to use this? Install it...
  9. macrumors 603


    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. macrumors 6502

    i have put the .jar file into my classpath and i still cannot get the thing to compile with this line....

  11. macrumors 603


    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. macrumors 6502

    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


    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. macrumors 6502

    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


    Use JPasswordField...
  16. macrumors 6502

    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