This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Regarding FTP Client
Posted by Varun Gupta on September 05, 2001 at 2:00 AM
Hi Srinivas Parvath , This is a nice code you have written and very helpful too but i have a problem. How can we specify that how many directories are avaliable on the FTP Server and how can we choose in which directory tp upload our files. Moreover javadocs for this package are not avaliable. Where can i get them. Thanks Varun... > import java.io.*; > import sun.net.ftp.*; > import sun.net.*; > public class FTP extends FtpClient > { > private String serverName; > private String userName; > private String userPassword; > > public FTP(String serverName, String userName, String userPassword) > { > this.serverName = serverName; > this.userName = userName; > this.userPassword = userPassword; > } > > /** > * Copies a file to the remote host. > * @param localFile local file including the path > * @param remoteFile remote filename > * @param ASCIIMode if set to true the transfer localInputStream done in ascii mode else binary mode. > * @return > */ > public boolean putFile(String localFile, String remoteFile,boolean ASCIIMode) > { > try > { > // Open a connection to the server and login > openServer(serverName); > login(userName, userPassword); > > // Set the transfer mode > if (ASCIIMode) > { > ascii(); > } > else > { > binary(); > } > // Get a handle to the remote file > TelnetOutputStream remoteOutputStream=(TelnetOutputStream)put(remoteFile); > // Get a handle to the local file > File localInputFile= new File(localFile); > FileInputStream localInputStream= new FileInputStream(localInputFile); > > // Read the local file and write it remotely > int charRead; > int totalBytes=0; > byte[] bytes = new byte[1024]; > while((charRead=localInputStream.read(bytes))!=-1) > { > totalBytes +=charRead; > remoteOutputStream.write(bytes,0,charRead); > } > > // Close everything and clean up > localInputStream.close(); > remoteOutputStream.close(); > closeServer(); > return (true); > } > catch (IOException e) > { > return (false); > } > } > }
Replies:
|