I'm stumped here. I want to be able to run this class from a jsp page
that will pull a file from one server and ftp it to another server. I
can get the following code to work fine when I simply want to read a
file from the server where this java code is located and upload it to
a new server. For some reason though I can not get this code to work
when I try to pull a file from another server in the FtpInputStream
object and then pass it in a method to the method that takes that
FtpInputStream object and iterates over it writing it to the new
server. The browser just seems to hand when I try this. Possibly I
can't pass an FtpInputStream the way I am trying it? I'd really love
some help as I think this must be something simple that I'm missing.
If I create stand alone code to read from a server and write to a
local file or if I read from the local file and upload to a remote
server I can write the code to get it to work fine. It's just
tweaking this code so that it will read from the one server and write
to the other. If the following code is unclear or not formatted
correctly please let me know. The problems seem to be where the ***
comments are. I would really love to get this working.
Thanks lots.
RIck

package insidectm;

import sun.net.ftp.*;
import sun.net.*;
import java.io.*;
import java.util.*;

public class FTPapp extends FtpClient
{
        private String serverName;
        private String userName;
        private String userPassword;
        private String files;
        private String fromServerName;
        private String fromUserName;
        private String fromUserPassword;

        public FTPapp (String serverName, String userName, String userPassword, String 
fromServerName, String fromUserName, String fromUserPassword )
        {
                this.serverName = serverName;
                this.userName = userName;
                this.userPassword = userPassword;
                this.fromServerName = fromServerName;
                this.fromUserName = fromUserName;
                this.fromUserPassword = fromUserPassword;
        }
        public boolean doUpload( String files )
        {
                this.files = files;
                boolean statusFlag = false;
                String token = null;
                boolean ASCIIMode = true;
                String remoteFile = null;
                String fileToCopyPath = null;

                try
                {

                        openServer(serverName);
                        login(userName, userPassword);

                        StringTokenizer st = new StringTokenizer( files, "\n", false );
                        while( st.hasMoreTokens() )
                        {
                                token = st.nextToken();
                                System.out.print("token = "+token+"\n");
                                //find out if token is gif or jpg
                                if ( ( token.indexOf( ".jpg", token.length() - 4 ) != 
-1 ) ||
                                        ( token.indexOf( ".gif", token.length() - 4 
)!= -1 ) ||
                                        ( token.indexOf( ".jpeg", token.length() - 5 ) 
!= -1 ) )
                                {
                                        ASCIIMode = false;
                                }

                                remoteFile = token.trim();
                                fileToCopyPath = token.trim();

                                // Set the transfer mode
                                if (ASCIIMode)
                                        ascii();
                                else
                                        binary();

                                // Get a handle to the remote file
                                TelnetOutputStream os=(TelnetOutputStream) 
put(remoteFile);

                                // Get a handle to the file on the server you are 
copying from
                                FtpInputStream is = getFtpInputStream( fileToCopyPath, 
fromServerName, fromUserName, fromUserPassword, ASCIIMode );
/**** CAN I DO THE ABOVE METHOD TO GET THE FtpInputSteam?? Currently when I try the 
browser just seems to hang indefinately ***/
                                int c;
                                int total_bytes=0;
                                byte[] bytes = new byte[1024];

                                while((c=is.read(bytes)) !=-1)
                                {
                                        total_bytes +=c;
                                        os.write(bytes,0,c);
                                }

                                statusFlag = true;
                                is.close();
                                os.close();
                        }//end while loop of uploading files

                        closeServer();
                }
                catch (java.io.IOException e)
                {
                        System.out.println (e.toString()+"\n");
                        statusFlag = false;
                }
                return statusFlag;
        }


        public FtpInputStream getFtpInputStream( String fileToCopyPath, String 
fromServerName, String fromUserName, String fromUserPassword, boolean ASCIIMode )
        {
                FtpInputStream is = null;
                try
                {
                        openServer( fromServerName);
                        login( fromUserName, fromUserPassword);
                        if (ASCIIMode)
                                ascii();
                        else
                                binary();

                        // Get a handle to the file copying from
/*** seems to hang here indefinately ????? *******/
                        is =(FtpInputStream)get( fileToCopyPath );
                        closeServer();

                }
                catch (java.io.IOException e)
                {
                        System.out.println (e.toString()+"\n");

                }
                return is;
        }
}

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to