Uploading & Verifying

H

H3li0

Hello,

Does anyone know how to upload files to a server via ftp *and* veryfying
that the file has been successfully uploaded?

How to do that with Java?

Any help/sample code would be appreciated.


Thanks in Advance.

Java Newbie
 
M

mromarkhan

Peace be unto you.

1.
Download
Commons Net from
http://jakarta.apache.org/site/binindex.cgi

2. Write this code
import org.apache.commons.net.ftp.FTPClient;
import java.io.*;
import org.apache.commons.net.ftp.FTPReply;
public class FTPThis
{
public static void main(String [] args)
{
FTPClient ftp = new FTPClient();
try
{
String server = "members.google.com";
String username="mromarkhan";
String password="x9s23msd0m3";
int reply;
ftp.connect(server);
ftp.login(username, password);
System.out.println("Connected to " + server + ".");
System.out.print(ftp.getReplyString());

// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();

if(!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
System.out.println("PWD: " + ftp.printWorkingDirectory());
FileInputStream inputstream = new FileInputStream("c:/windows/desktop/quran.html");
if(ftp.storeFile("quran.html",inputstream))
{
System.out.println("Was uploaded");
}
else
{
System.out.println("Not uploaded");
}
}
catch(IOException e)
{
if(ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch(IOException f)
{
// do nothing
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
System.exit(1);
}
}
}

3.
Realize that
"The convention for all the FTP command methods
in FTPClient is such that they
either return a boolean value or
some other value. The boolean methods
return true on a successful completion
reply from the FTP server and false on
a reply resulting in an error condition or failure."
- http://jakarta.apache.org/commons/net/apidocs/org/apache/commons/net/ftp/FTPClient.html

4. Run Show
javac -classpath "C:\downloads\commons-net-1.1.0\commons-net-1.1.0\commons-net-1.1.0.jar;." FTPThis.java
java -classpath "C:\downloads\commons-net-1.1.0\commons-net-1.1.0\commons-net-1.1.0.jar;." FTPThis

5. Produces
Connected to members.google.com.
230-User mromarkhan has group access to: httpd
230 OK. Current restricted directory is /
PWD: /
Was uploaded

Have a good day.
 
A

Andrew Thompson

2. Write this code

System.out.println("Connected to " + server + ".");
System.out.print(ftp.getReplyString());

// After connection attempt, you should check the reply code to verify
// success.

Hi! I've noticed a few of your
code samples and had a play with
one.

[ They are fun, BTW :) ]

Could I just ask two (small) things
of you though?

1) Set the default 'wrap width' of
your newsreader a few less characters
so you do not post code lines as long
as the second last one there.

It may well line-wrap on some news readers
and becomes not 'copy-paste-compile' any more.

That would be a pity.

[My apologies in advance if you specifically
counted the chars and determined they fit
through a standard reader, it seems 83 chars,
whereas I _think_ you can only depend on the
'70's - but I am a bit hazy on the actual number. ]

2) Wrap your code in something like..
<code>
.....
</code>

[ I am looking at playing with classes
that can pull code straight from the Google
usenet archives into my on-line compiler,
and those tags would help me identify the
code easily! ]

Oh, and ..peace be unto you, as well. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top