Commit 795ae350 authored by spawar's avatar spawar

D15IKAT004 Added source for upload file using SFTP


git-svn-id: http://15.206.35.175/svn/proteus/business-java/trunk@99398 ce508802-f39f-4f6c-b175-0d175dae99d5
parent 3268646a
...@@ -38,8 +38,11 @@ public class DDUpdStockSchedule implements Schedule ...@@ -38,8 +38,11 @@ public class DDUpdStockSchedule implements Schedule
private String hostName = ""; private String hostName = "";
private String username = ""; private String username = "";
private String password = ""; private String password = "";
private String remoteFilePath = "uat/Stock.csv"; //Changed by Samadhan D15IKAT004 on 10/12/2015 for SFTP And configurable remote path
//private String remoteFilePath = "uat/Stock.csv";
private String remoteFilePath = "";
private String csvFileNameUpd = ""; private String csvFileNameUpd = "";
private String uploadProtocol = "";
private FileSystemOptions opts = null; private FileSystemOptions opts = null;
public String schedulePriority( String wrkflwPriority )throws Exception public String schedulePriority( String wrkflwPriority )throws Exception
...@@ -56,6 +59,7 @@ public class DDUpdStockSchedule implements Schedule ...@@ -56,6 +59,7 @@ public class DDUpdStockSchedule implements Schedule
Connection conn = null; Connection conn = null;
ConnDriver connDriver = null; ConnDriver connDriver = null;
String tranDate="",fileName = "Stock.csv"; String tranDate="",fileName = "Stock.csv";
String remoteFileName = "Stock.csv";
try try
{ {
setDDSalesConfig(); setDDSalesConfig();
...@@ -67,7 +71,7 @@ public class DDUpdStockSchedule implements Schedule ...@@ -67,7 +71,7 @@ public class DDUpdStockSchedule implements Schedule
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat()); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(genericUtility.getApplDateFormat());
tranDate = simpleDateFormat.format(currentDate.getTime()); tranDate = simpleDateFormat.format(currentDate.getTime());
System.out.println("csvFileNameUpd12---->>["+csvFileNameUpd+"]"); System.out.println("csvFileNameUpd12---->>["+csvFileNameUpd+"]");
File file = new File(csvFileNameUpd); File file = new File(csvFileNameUpd);
System.out.println("file.separatorChar---->>["+file.separatorChar+"]"); System.out.println("file.separatorChar---->>["+file.separatorChar+"]");
file.mkdirs(); file.mkdirs();
...@@ -77,30 +81,49 @@ public class DDUpdStockSchedule implements Schedule ...@@ -77,30 +81,49 @@ public class DDUpdStockSchedule implements Schedule
{ {
fileName = file.separator+fileName; fileName = file.separator+fileName;
} }
generateCsvFile (csvFileNameUpd +fileName,conn); generateCsvFile (csvFileNameUpd +fileName,conn);
System.out.println("CSV file successfully created....... "); System.out.println("CSV file successfully created....... ");
//Changed By Samadhan D15IKAT004 on 10/12/2015 for SFTP potocol Start
final String vtest = csvFileNameUpd+fileName; //final String vtest = csvFileNameUpd+fileName;
final String localFile = csvFileNameUpd+fileName;
char ch1 = remoteFilePath.charAt(remoteFilePath.length() -1 );
System.out.println("Ch @@@@---->>["+ch1+"]");
if(ch1 != file.separatorChar)
{
remoteFileName = file.separator+remoteFileName;
}
final String remoteFile = remoteFilePath+remoteFileName;
//Changed By Samadhan D15IKAT004 on 10/12/2015 for SFTP potocol End
//putFile(username, hostName, password, remoteFilePath, csvFileNameUpd+fileName); //putFile(username, hostName, password, remoteFilePath, csvFileNameUpd+fileName);
Thread t = new Thread() Thread t = new Thread()
{ {
public void run() public void run()
{ {
System.out.println("Thread started........"); System.out.println("Thread started........");
putFile(username, hostName, password, remoteFilePath,vtest); //Changed By Samadhan D15IKAT004 on 10/12/2015 for SFTP potocol Start
System.out.println("Thread commited........"); //putFile(username, hostName, password, remoteFilePath, vtest);
} if("SFTP".equalsIgnoreCase(uploadProtocol))
{
}; putFileSFTP(username, hostName, password, remoteFilePath,localFile,22);
}
t.start(); else
{
System.out.println("End of program........"); putFileFTP(username, hostName, password, remoteFile,localFile,21);
}
//Changed By Samadhan D15IKAT004 on 10/12/2015 for SFTP potocol End
System.out.println("The file is uploaded successfully......."); System.out.println("Thread commited........");
}
};
t.start();
System.out.println("End of program........");
//System.out.println("The file is uploaded successfully.......");
} }
catch(Exception e) catch(Exception e)
{ {
...@@ -203,16 +226,19 @@ public class DDUpdStockSchedule implements Schedule ...@@ -203,16 +226,19 @@ public class DDUpdStockSchedule implements Schedule
} }
} }
*/ */
private void putFile(String username, String host, String password, String remotefile, String localfile) private void putFileFTP(String username, String host, String password, String remotefile, String localfile,int port)
{ {
FTPClient ftpClient = new FTPClient(); FTPClient ftpClient = new FTPClient();
System.out.println("preparing the host information for ftp.");
try try
{ {
System.out.println("Remote File: "+remotefile); System.out.println("Remote File: "+remotefile);
System.out.println("Local File: "+localfile); System.out.println("Local File: "+localfile);
ftpClient.connect(host, 21); ftpClient.connect(host, port);
showServerReply(ftpClient); showServerReply(ftpClient);
int replyCode = ftpClient.getReplyCode(); int replyCode = ftpClient.getReplyCode();
//Changed by Samadhan for getting file upload status using DDSalesConfig.xml
boolean uploadStatus= false;
if (!FTPReply.isPositiveCompletion(replyCode)) if (!FTPReply.isPositiveCompletion(replyCode))
{ {
System.out.println("Operation failed. Server reply code: " + replyCode); System.out.println("Operation failed. Server reply code: " + replyCode);
...@@ -233,7 +259,9 @@ public class DDUpdStockSchedule implements Schedule ...@@ -233,7 +259,9 @@ public class DDUpdStockSchedule implements Schedule
File firstLocalFile = new File(localfile); File firstLocalFile = new File(localfile);
InputStream inputStream = new FileInputStream(firstLocalFile); InputStream inputStream = new FileInputStream(firstLocalFile);
ftpClient.storeFile(remotefile, inputStream); //Changed by Samadhan for getting file upload status using DDSalesConfig.xml
//ftpClient.storeFile(remotefile, inputStream);
uploadStatus = ftpClient.storeFile(remotefile, inputStream);
inputStream.close(); inputStream.close();
if (ftpClient.isConnected()) if (ftpClient.isConnected())
...@@ -241,7 +269,18 @@ public class DDUpdStockSchedule implements Schedule ...@@ -241,7 +269,18 @@ public class DDUpdStockSchedule implements Schedule
ftpClient.logout(); ftpClient.logout();
ftpClient.disconnect(); ftpClient.disconnect();
} }
System.out.println("File Uploaded successfully"); //Changed by Samadhan for getting file upload status using DDSalesConfig.xml Start
//System.out.println("File Uploaded successfully");
System.out.println("File Uploaded Status:"+uploadStatus);
if(uploadStatus)
{
System.out.println("File Uploaded successfully");
}
else
{
System.out.println("File can't Uploaded successfully");
}
//Changed by Samadhan for getting file upload status using DDSalesConfig.xml End
} }
} }
catch (IOException ex) catch (IOException ex)
...@@ -299,7 +338,11 @@ public class DDUpdStockSchedule implements Schedule ...@@ -299,7 +338,11 @@ public class DDUpdStockSchedule implements Schedule
this.csvFileNameUpd = genericUtility.getColumnValue("STOCK_TEMP_UPD_PATH",dom); this.csvFileNameUpd = genericUtility.getColumnValue("STOCK_TEMP_UPD_PATH",dom);
this.hostName = genericUtility.getColumnValue("SFTP_HOST",dom); this.hostName = genericUtility.getColumnValue("SFTP_HOST",dom);
this.username = genericUtility.getColumnValue("SFTP_USER",dom); this.username = genericUtility.getColumnValue("SFTP_USER",dom);
this.password = genericUtility.getColumnValue("SFTP_PWD",dom); this.password = genericUtility.getColumnValue("SFTP_PWD",dom);
//Changed by Samadhan for set remote path using DDSalesConfig.xml Start
this.remoteFilePath = genericUtility.getColumnValue("SFTP_REMOTE_DIR",dom);
this.uploadProtocol = genericUtility.getColumnValue("FILE_UPLOAD_PROTOCOL",dom);
//Changed by Samadhan for set remote path using DDSalesConfig.xml End
} }
} }
catch(Exception e) catch(Exception e)
...@@ -307,6 +350,48 @@ public class DDUpdStockSchedule implements Schedule ...@@ -307,6 +350,48 @@ public class DDUpdStockSchedule implements Schedule
e.printStackTrace(); e.printStackTrace();
} }
} }
//Changed by Samadhan D15IKAT004 on 10/12/2015 for SFTP File upload Start
private void putFileSFTP(String username, String host, String password, String remoteDestPath, String localFile,int port)
{
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
System.out.println("preparing the host information for sftp.");
try
{
System.out.println("user name: ["+username+"]password["+password+"]host["+host+"]Port["+port+"]");
System.out.println("Localfile:"+localFile);
System.out.println("RemotePath:"+remoteDestPath);
JSch jsch = new JSch();
session = jsch.getSession(username, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("Host connected.");
channel = session.openChannel("sftp");
channel.connect();
System.out.println("sftp channel opened and connected.");
channelSftp = (ChannelSftp) channel;
channelSftp.cd(remoteDestPath);
File f = new File(localFile);
channelSftp.put(new FileInputStream(f), f.getName());
System.out.println("File Uploaded successfully");
} catch (Exception ex)
{
System.out.println("Exception found while Uploading a file.");
ex.printStackTrace();
}
finally
{
channelSftp.exit();
System.out.println("sftp Channel exited.");
channel.disconnect();
System.out.println("Channel disconnected.");
session.disconnect();
System.out.println("Host Session disconnected.");
}
}
//Changed by Samadhan D15IKAT004 on 10/12/2015 for SFTP File upload End
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment