Jump to content

Help- Evaranna Ftps Protocol Use Chesara Java Lo


Recommended Posts

Posted

may be useful for some..............








import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPConnectionClosedException;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

public class SaigFileBuilder {

private String server = "host";
private String username = "user";
private String password = "pass";
private String remoteParentDir = "/prd/fa/out/FA003100/SAIG/";
private String filesep = System.getProperty("file.separator");
private String local = "c:" + filesep + "temp" + filesep + "saig_files";
private List<String> fileList = null;

private boolean error = false;
private String protocol = "TLS"; // SSL/TLS
private FTPSClient ftps = null;



public static void main(String[] args) {

SaigFileBuilder fb = new SaigFileBuilder();

try {
fb.getClientConnection();
fb.getFiles(false);
fb.logout();

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

System.exit(fb.error ? 1 : 0);
}



private void getClientConnection() throws NoSuchAlgorithmException {

this.ftps = new FTPSClient(this.protocol);
this.ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

this.ftps = getConnection(server);
this.ftps = getLogin(username, password);
}

private FTPSClient getConnection(String server) {

try
{
int reply;

// Connect to the FTP server...
this.ftps.connect(server);
System.out.println("Connected to " + server + ".");

// Get connect reply code...
reply = this.ftps.getReplyCode();
System.out.println("String reply: " + this.ftps.getReplyString());

// Verify successful connection reply code was returned...
if (!FTPReply.isPositiveCompletion(reply))
{
error = true;
System.err.println("FTP server refused connection.");
return null;
}

// FileZilla client issues following, so I will too..
this.ftps.sendCommand("OPTS UTF8 ON");
this.ftps.execPBSZ(0);
this.ftps.execPROT("P");
this.ftps.enterLocalPassiveMode();
}
catch (IOException e)
{
System.err.println("Could not connect to server.");
e.printStackTrace();
}
return this.ftps;
}


private FTPSClient getLogin(String username, String password) {

try
{
this.ftps.setBufferSize(1000);

if (!this.ftps.login(username, password))
{
System.out.println("Login failed");
this.ftps.logout();
return null;
}

System.out.println("Remote system is " + this.ftps.getSystemType());

}
catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}

return this.ftps;
}



private void getFiles(boolean binaryTransfer) {
try
{
if (binaryTransfer) {
this.ftps.setFileType(FTP.BINARY_FILE_TYPE);
}

OutputStream output = null;

this.ftps.changeWorkingDirectory(this.remoteParentDir);
System.out.println("pwd: " + this.ftps.printWorkingDirectory());

this.fileList = new ArrayList<String>();
scanDirTree(this.remoteParentDir);

System.out.println("Finished scanning...");
for (String file : this.fileList) {
System.out.println(file);
}

String dirHold = " ";
String winDir = " ";
String winFile = " ";

for (String ftpFilePath : this.fileList) {
// Going to copy the remote dirs and files to local, so change the paths to
// use local Windows file separator instead of remote Unix...
winDir = ftpFilePath.substring(0, ftpFilePath.lastIndexOf("/") + 1).replace("/", filesep);
winFile = ftpFilePath.substring(ftpFilePath.lastIndexOf("/") + 1, ftpFilePath.length());
System.out.println("winDir.: " + winDir);
System.out.println("winFile: " + winFile);

// If new dir found in list, then create it locally...
if (dirHold.equals(winDir) == false) {
System.out.println("mkdir: " + local + winDir);
new File(local + winDir).mkdirs();
dirHold = winDir;
}

// And write the file locally...
System.out.println("Attempting to write: " + this.local + winDir + winFile);
output = new FileOutputStream(local + winDir + winFile);
this.ftps.retrieveFile(ftpFilePath, output);

output.close();
output = null;
}

}
catch (FTPConnectionClosedException e)
{
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
}
catch (IOException e)
{
error = true;
e.printStackTrace();
}
}


private void scanDirTree(String directory) throws IOException {
// Create and return a list of all dirs and files on remote
// server below the parent dir that was passed to this method...
FTPFile[] files = this.ftps.listFiles(directory);

if (files.length == 0) {
System.out.println("no files in " + directory);
}

for (FTPFile file : files) {
String name = file.getName();


if (!".".equals(name) && !"..".equals(name)) {
if (file.isDirectory()) {
// Recursive call, go deeper...
scanDirTree(directory + name + "/");
} else {
fileList.add(directory + file.getName());
System.out.println("Added: " + directory + file.getName());
}
}
}
}



private void logout() {

if (this.ftps.isConnected()) {
try {
System.out.println("Logging out...");
this.ftps.logout();
System.out.println("Disconnecting...");
this.ftps.disconnect();
}
catch (IOException e) {
error = true;

}
}
}
}

Posted

GP Mr.Mirchi.. I might help someone

×
×
  • Create New...