The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

Advertisement

Advertisement

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:

Here is the Code

Posted by Srinivas Parvath on April 26, 2001 at 4:12 PM

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:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us