PDA

View Full Version : Java and FTP help!




jtalerico
Feb 4, 2006, 10:53 AM
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:*******@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!



robbieduncan
Feb 4, 2006, 11:43 AM
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.

jtalerico
Feb 4, 2006, 03:31 PM
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")) {
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();

zimv20
Feb 4, 2006, 05:37 PM
hey joe -- you forgot to X out your username and password in your second example.

jtalerico
Feb 4, 2006, 05:48 PM
Fixed thanks. just changed it :)

Damn what a day... And thoughts on what is wrong?

zimv20
Feb 4, 2006, 05:53 PM
thoughts on what is wrong?
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 (http://www.javaworld.com/javaworld/jw-04-2003/jw-0404-ftp_p.html) promising page.

jeremy.king
Feb 4, 2006, 06:58 PM
I'd suggest using Apache Commons Net (http://jakarta.apache.org/commons/net/)

Then give a looksy at this source (http://www.koders.com/java/fidCE3F66D3DD48269FC816633C19932FDB8077357F.aspx) for an example..

jtalerico
Feb 4, 2006, 07:15 PM
Is there somewhere that will tell me how to use this? Install it...

jeremy.king
Feb 4, 2006, 07:29 PM
Is there somewhere that will tell me how to use this? Install it...

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

jtalerico
Feb 4, 2006, 08:32 PM
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.*;

jeremy.king
Feb 4, 2006, 08:37 PM
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.*;

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?

jtalerico
Feb 4, 2006, 08:58 PM
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! :)

jeremy.king
Feb 5, 2006, 09:55 AM
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)?

jtalerico
Feb 5, 2006, 04:08 PM
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!

jeremy.king
Feb 5, 2006, 06:31 PM
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!

Use JPasswordField (http://java.sun.com/docs/books/tutorial/uiswing/components/passwordfield.html)...

jtalerico
Feb 5, 2006, 07:05 PM
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....