I put together an ftpput task some weeks ago using NetComponents from OROInc.  I
have attached it for your use.

Use it like this:

  <ftpput localname="filename" remotename="filename" server="ftp.server"
userid="me" password="mypass" binary="yes|no"/>

"binary" is the only optional parameter and defaults to yes.  I know the
clear-text password is a huge security hole, but this was a quick job - and I
only use restricted accounts in the context anyway.  If anyone can suggest a
simple, secure way to do this, I'm all ears.

This task currently only does single files - it does not support copydir-like
behavior.

BTW - Apache guys - I'm donating this to the project if you want it.  If it ends
up in the library, I don't have to maintain my diffs.  :-)  Here are the lines
to add to the build.xml:

    In target check_for_optional_packages:
    <available property="ftp.present" classname="com.oroinc.net.ftp.FTPClient"
/>

    In target compile:
    <exclude name="**/FTP*.java" unless="ftp.present" />


Roger Vaughn


Walker Joe wrote:

> Hi,
>
> At a rough guess this must have been asked 100 times already, but there is
> not FAQ that I can see that answers it.
>
> Are there any plans for <ftp srcdir="..." destdir="..." />
> I guess even a <copydir method="ftp" ... /> could be possible.
> ?
>
> I've not seen anything and I guess that java.net.URL is not up to the job so
> it would mean using some other FTP classes, which maybe why it's not been
> done already.
>
> I'm tempted to write this but it must have been debated before?
>
> I'm also tempted to write something to call tomcat's jspc process. Anyone
> tried this?
>
> Joe.
>
> Legal Disclaimer:-
>
> Please be aware that messages sent over
> the Internet may not be secure and should
> not be seen as forming a legally binding
> contract unless otherwise stated.
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs.optional;

import org.apache.tools.ant.*;
import java.io.*;
import java.net.*;
import com.oroinc.net.ftp.*;

/**
 * Sends an individual file to an FTP server.
 *
 * @author [EMAIL PROTECTED]
 */

public class FTPput
    extends Task
{
    private String localName;
    private String remoteName;
    private String server;
    private String userid;
    private String password;
    private boolean binary = true;

    public void setLocalname(String name)
    {
        this.localName = name;
    }

    public void setRemotename(String name)
    {
        this.remoteName = name;
    }

    public void setServer(String server)
    {
        this.server = server;
    }

    public void setUserid(String userid)
    {
        this.userid = userid;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public void setBinary(String binary)
    {
        this.binary = project.toBoolean(binary);
    }
    
    public void execute()
        throws BuildException
    {
        if (localName == null)
        {
            throw new BuildException("localName attribute must be set!");
        }
        if (remoteName == null)
        {
            throw new BuildException("remoteName attribute must be set!");
        }
        if (server == null)
        {
            throw new BuildException("server attribute must be set!");
        }
        if (userid == null)
        {
            throw new BuildException("userid attribute must be set!");
        }
        if (password == null)
        {
            throw new BuildException("password attribute must be set!");
        }

        FTPClient ftp = null;
        InputStream instream = null;

        try
        {
            log("Opening FTP connection to " + server, Project.MSG_VERBOSE);

            ftp = new FTPClient();
            
            ftp.connect(server);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))
            {
                throw new BuildException("FTP connection failed: " + 
ftp.getReplyString());
            }

            log("connected", Project.MSG_VERBOSE);
            log("logging in to FTP server", Project.MSG_VERBOSE);

            if (!ftp.login(userid, password))
            {
                throw new BuildException("Could not login to FTP server");
            }

            log("login succeeded", Project.MSG_VERBOSE);
            
            if (binary)
            {
                ftp.setFileType(FTP.IMAGE_FILE_TYPE);
            }

            log("transferring file", Project.MSG_VERBOSE);
            
            File localFile = project.resolveFile(localName);
            instream = new BufferedInputStream(new FileInputStream(localFile));

            ftp.storeFile(remoteName, instream);

            log("File " + localFile.getAbsolutePath() + " copied to " + server);
        }
        catch(IOException ex)
        {
            throw new BuildException("error during FTP transfer: " + ex);
        }
        finally
        {
            /*
            if (ftp != null && ftp.isConnected())
            {
                try
                {
                    // this hangs - I don't know why.
                    ftp.disconnect();
                }
                catch(IOException ex)
                {
                    // ignore it
                }
            }
            */
            
            if (instream != null)
            {
                try
                {
                    instream.close();
                }
                catch(IOException ex)
                {
                    // ignore it
                }
            }
        }
    }
}

Reply via email to