-1

I have a JSP page which contains a link to upload a file. Initially the file transfer was happening using FTP. The server seems to transfer the file from client using the FTP only. I require to change the code to support SFTP now. Like the server initiated an FTP session on the client machine, is it possible for SFTP as well? here is the client code:

FTPClient client = new FTPClient();                        
FTPClient clientUser = new FTPClient(); //From ftp location (user saves file here)
try {                           //Location where user chooses file from
    eas_user_import_ip_address_val  = resovleJNDIResource.resolveJNDIString(eas_user_import_ip_address_tx);
    eas_user_import_id_val = resovleJNDIResource.resolveJNDIString(eas_user_import_id_tx);
    eas_user_import_pwd_val = resovleJNDIResource.resolveJNDIString(eas_user_import_pwd_tx);
    eas_user_file_prefix_val= resovleJNDIResource.resolveJNDIString(eas_user_file_prefix_tx);
    eas_user_file_prefix_val= eas_user_file_prefix_val.trim();


clientUser.connect(eas_user_import_ip_address_val);
int replyUser = clientUser.getReplyCode();

if(!FTPReply.isPositiveCompletion(replyUser)) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error connecting to:" + eas_user_import_ip_address_val + " reply code:" + replyUser);
    clientUser.disconnect();
    return false;
}

boolean loginUser = clientUser.login(eas_user_import_id_val, eas_user_import_pwd_val);

if (!loginUser) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error logging in to:" + eas_user_import_id_val);
    return false;
}

//Location where file gets copied to. 
eas_import_ip_address_val        = resovleJNDIResource.resolveJNDIString(eas_import_ip_address_tx);
eas_import_id_val                                          = resovleJNDIResource.resolveJNDIString(eas_import_id_tx);
eas_import_pwd_val                                     = resovleJNDIResource.resolveJNDIString(eas_import_pwd_tx);

eas_part_file_prefix_val              = resovleJNDIResource.resolveJNDIString(eas_part_file_prefix_tx);
eas_p2p_file_prefix_val               = resovleJNDIResource.resolveJNDIString(eas_p2p_file_prefix_tx);

client.connect(eas_import_ip_address_val);
int reply = client.getReplyCode();

if(!FTPReply.isPositiveCompletion(reply)) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error connecting to:" + eas_import_ip_address_val + " reply code:" + reply);
    client.disconnect();
    return false;
}

boolean login = client.login(eas_import_id_val, eas_import_pwd_val);

if (!login) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error loging in to:" + eas_import_id_val);
    return false;
}
//Loged in to From and To locations. Now transfer file
clientUser.setFileType(FTP.ASCII_FILE_TYPE);
clientUser.enterLocalActiveMode(); //from example

String originalFileName = fileName;

fileName = eas_user_file_prefix_val+fileName;

InputStream ip = clientUser.retrieveFileStream(fileName);



String issueIdStr = StringHelper.prepad(issueId + "", 10, '0');

String headerRecord  = "HEADER                   " + adjPlatformCd + issueIdStr + batchId + " Original file : " + fileName;
String trailerRecord = "TRAILER                  " + adjPlatformCd + issueIdStr + batchId + " Server file   : " + eas_file_prefix_val + dt_tm_siz + ".csv";

client.setFileType(FTP.ASCII_FILE_TYPE);
//First store file as ".tmp". First header then data followed by trailer
boolean retValue = client.storeFile(eas_file_prefix_val + dt_tm_siz + ".tmp", new ByteArrayInputStream(headerRecord.getBytes()));
if (!retValue) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error creating:" + eas_file_prefix_val + dt_tm_siz + ".tmp");
    return false;
}

retValue = client.appendFile(eas_file_prefix_val + dt_tm_siz + ".tmp", ip);
if (!retValue) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error append 1:" + eas_file_prefix_val + dt_tm_siz + ".tmp");
    return false;
}
ip.close();

retValue = client.appendFile(eas_file_prefix_val + dt_tm_siz + ".tmp", new ByteArrayInputStream(trailerRecord.getBytes()));
if (!retValue) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error append 2:" + eas_file_prefix_val + dt_tm_siz + ".tmp");
    return false;
}

boolean commandOK=clientUser.completePendingCommand(); // this command lets next few ftp commands proces successfully

// place user file in PROCESSED folder. Append issue id, batch #, date and time if file length is < 230
String renamedUserFileName = eas_user_file_prefix_val+ "PROCESSED\\" + originalFileName.substring(0, originalFileName.lastIndexOf(".csv")) + "_" + issueId + "_" + batchId.trim() + dt_tm_siz + ".csv";

String someSiteCommand = "RNFR " + fileName; //rename from

reply  = clientUser.sendCommand(someSiteCommand);
someSiteCommand = "RNTO " + renamedUserFileName; //rename to

reply = clientUser.sendCommand(someSiteCommand);

if(!FTPReply.isPositiveCompletion(reply)) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error renaming:" + fileName + " reply code:" + reply);
    return false;
}

someSiteCommand = "RNFR " + eas_file_prefix_val + dt_tm_siz + ".tmp"; //rename from

reply  = client.sendCommand(someSiteCommand);
someSiteCommand = "RNTO " + eas_file_prefix_val + dt_tm_siz + ".csv"; //rename to

reply = client.sendCommand(someSiteCommand);
if(!FTPReply.isPositiveCompletion(reply)) {
    ENTADJRuntime.getInstance().logError("FTPService", "Error renaming:" + eas_file_prefix_val + dt_tm_siz + ".tmp" + " reply code:" + reply);
    return false;
}
client.logout();
clientUser.logout();
learner420
  • 190
  • 1
  • 6
  • 21

1 Answers1

1

Like the server initiated an FTP session on the client machine, is it possible for SFTP as well? here is the client code:

SFTP is a completely different protocol than FTP and you probably cannot reuse any FTP specific code. It might be that you've meant FTPS instead which is FTP extended with TLS. If this is supported depends on the setup of the server, that is it is not enough to only change the client code. As long as the server supports it you can use it with Java, see for example Secure FTP with org.apache.commons.net.ftp.FTPClient

Community
  • 1
  • 1
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • No, I meant SFTP (FTP over SSH). Actually the http server where I am hosting my application, doesn't support SFTP. But there is another SFTP server where I want to get this file uploaded to. What kind of setup do I need to do on the server for this purpose. – learner420 Nov 02 '15 at 21:10
  • 1
    @learner420: a HTTP server does not support SSH since HTTP and SSH are different protocols. For SFTP/SSH you need a SSH server installed and setup. What options you have for this depend on your (unknown) server OS. Maybe just search for SSH/SFTP server for your platform. – Steffen Ullrich Nov 02 '15 at 21:14
  • hmm, I have a SFTP server, where I want to upload this file and it's different that HTTP server. I am using HTTP server to host that jsp, from where I want to fetch all the details of the file and then initiate a SFTP transfer to that (SFTP) server. – learner420 Nov 02 '15 at 21:20
  • @learner420: for SFTP you need an SFTP client. An FTP client which you use will not work since these are completely different protocols. – Steffen Ullrich Nov 02 '15 at 21:21
  • oh, so this SFTP client must be installed on the client machine (where the user chooses the file on the webpage). Is it possible to control this SFTP client from the server code? Does the prior code (FTP code) was working all because Window already have FTP client inbuilt? – learner420 Nov 02 '15 at 21:27
  • @learner420: your prior code is using an FTP library and it worked because there was an FTP server on the system. To connect to a SFTP server you need some library in the client application which support SFTP, see https://www.google.com/search?q=sftp+java+library – Steffen Ullrich Nov 02 '15 at 21:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94028/discussion-between-learner420-and-steffen-ullrich). – learner420 Nov 02 '15 at 21:37