import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
public class SandvineFTP {
private Logger logger = Logger.getLogger(SandvineFTP.class);
private static SandvineFTP sFTP;
FTPClient client = new FTPClient();
// make singleton
private SandvineFTP(){
}
public static SandvineFTP getInstance(){
if(sFTP==null)
sFTP = new SandvineFTP();
return sFTP;
}
// make connection with the server...
public boolean getConnection(String serverName, String uName, String pwd){
boolean result=false;
try {
client.connect(serverName);
int code = client.getReplyCode();
if(FTPReply.isPositiveCompletion(code)){
logger.info("successfully connected to remote server");
System.out.println("Connected to Server ... Code ::: "+ code);
}
result = client.login(uName,pwd);
}
catch(Exception e){
logger.error("Error ::: " + e.getMessage(),e);
e.printStackTrace();
}
return result;
}
// close connection
public void closeConnection(){
try {
client.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("Disconnect Error ::: " + e.getMessage(),e);
e.printStackTrace();
}
}
// list all files
public List listAllFiles(){
try {
FTPFile[] ftpFile = client.listFiles();
List fileNames = new ArrayList();
System.out.println(fileNames.size());
for(FTPFile file : ftpFile){
fileNames.add(file.getName());
}
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("List Error ::: " + e.getMessage(),e);
e.printStackTrace();
}
return null;
}
// change the working directory
public boolean changeWorkingDirectory(String dirName){
boolean result = false;
try {
result = client.changeWorkingDirectory(dirName);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("Change Directory Error ::: " + e.getMessage(),e);
e.printStackTrace();
}
return result;
}
// delete the file
public String getCurrentWorkingDirectory(){
try {
return client.printWorkingDirectory();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
// put the file
public boolean putFile(String completePath){
boolean result = false;
try {
client.setFileType(FTP.ASCII_FILE_TYPE);
File f = new File(completePath);
if(f.exists()){
result = client.storeFile(f.getName(), new FileInputStream(f));
}
else
{
logger.info("File does not exist");
System.out.println("file does not exist");
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("File Transfer Error ::: " + e.getMessage(),e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("File Transfer Error ::: " + e.getMessage(),e);
}
return result;
}
// receive the file
public InputStream getFile(String fileName){
InputStream iStream=null;
try {
client.setFileType(FTP.ASCII_FILE_TYPE);
iStream = client.retrieveFileStream(fileName);
// iStream.close();
System.out.println("File Transfer Successfull");
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("File Transfer Error ::: " + e.getMessage(),e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("File Transfer Error ::: " + e.getMessage(),e);
}
return iStream;
}
public boolean deleteFile(String fileName){
boolean result=false;
try {
result = client.deleteFile(fileName);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("File Transfer Error ::: " + e.getMessage(),e);
e.printStackTrace();
}
return result;
}
}
0 comments:
Post a Comment