2015-11-02 71 views
-1

我有一個包含上傳文件鏈接的JSP頁面。最初,文件傳輸是使用FTP進行的。服務器似乎只使用FTP從客戶端傳輸文件。我需要現在更改代碼以支持SFTP。與服務器在客戶端計算機上啓動FTP會話一樣,是否也可以使用SFTP?這裏是客戶端代碼:JSP文件上傳。從FTP到SFTP的轉換

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(); 

回答

1

啓動一樣在客戶機上的FTP會話的服務器,有可能是SFTP呢?這裏是客戶端代碼:

SFTP是一個完全不同的協議FTP,你可能不能再使用任何FTP特定的代碼。這可能是因爲你的意思是FTPS,而FTP是通過TLS擴展的。如果支持這個取決於服務器的設置,那僅僅改變客戶端代碼是不夠的。只要服務器支持它,您可以使用它與Java,請參閱例如Secure FTP with org.apache.commons.net.ftp.FTPClient

+0

不,我的意思是SFTP(通過SSH的FTP)。實際上我託管我的應用程序的http服務器不支持SFTP。但還有另一個SFTP服務器,我想要將這個文件上傳到。爲此我需要在服務器上做什麼樣的設置。 – learner420

+1

@ learner420:HTTP服務器不支持SSH,因爲HTTP和SSH是不同的協議。對於SFTP/SSH,您需要安裝和設置SSH服務器。你有什麼選擇取決於你的(未知)服務器操作系統。也許只是爲您的平臺搜索SSH/SFTP服務器。 –

+0

嗯,我有一個SFTP服務器,我想上傳這個文件,它不同於HTTP服務器。我正在使用HTTP服務器來託管該jsp,從中我想要獲取該文件的所有詳細信息,然後啓動到該(SFTP)服務器的SFTP傳輸。 – learner420