Uploading & Verifying

Discussion in 'Java' started by H3li0, Nov 5, 2003.

  1. H3li0

    H3li0 Guest

    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
    H3li0, Nov 5, 2003
    #1
    1. Advertising

  2. H3li0

    Guest

    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.
    , Apr 22, 2004
    #2
    1. Advertising

  3. On Thu, 22 Apr 2004 20:01:04 GMT, wrote:

    > 2. Write this code


    <snip 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. :)

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Apr 22, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. julian

    Verifying a URL

    julian, Jul 17, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    396
    Mario Vargas
    Jul 17, 2003
  2. John

    Verifying users

    John, Oct 3, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    431
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 4, 2004
  3. Tim

    Verifying web.config

    Tim, Apr 4, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    411
  4. Robert Scheer

    Verifying the user is connected

    Robert Scheer, Sep 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    446
  5. Dung Ping
    Replies:
    10
    Views:
    974
    Chaddy2222
    Jan 16, 2006
Loading...

Share This Page