public class Loginsftpbypubkey {
private static final Logger Logger=loggerfactory.getlogger (Authsftpbypubkey.class);
/**session timeout time 1 minutes **/
private static final int session_time_out=60000;//60s
/**
*
* Method Description: Upload local file to SFTP server specified directory <br>
*
* @param privkeypath Local private key file absolute path eg:. /.. /id_rsa
* @param remoteuser target SFTP Server user
* @param Port Destination host address
* @param host Destination port
* @return Channelsftp
* @throws Exception Exception
*/
public static channelsftp Getchannel (String privkeypath,string remoteuser,int port,string host) throws Serviceexception {
Jsch Jsch = new Jsch ();
Key file Absolute Path
Channel Channel=null;
try {
Jsch.addidentity (Privkeypath);
User name
Session session = Jsch.getsession (RemoteUser, host, Port);
Properties Config = new properties ();
Config.put ("stricthostkeychecking", "no");
Session.setconfig (config);
Session.settimeout (session_time_out);
Session.connect ();
Channel = Session.openchannel ("sftp");
Channel.connect ();
} catch (Jschexception e) {
throw new Serviceexception (Exceptioncode.other_exception,string.format ("Get SFTP Connection exception:%s", e.tostring ()));
}
Return (CHANNELSFTP) channel;
}
/**
*
* Method Description: Upload files to the remote SFTP server <br>
*
* @param sftp SFTP
* @param remoteuploaddir remote upload directory eg: ~/upload/
* @param fileabspath The file to be uploaded local absolute path eg:/weblogic/test.txt
* @throws serviceexception Serviceexception
*/
public static void upload (Channelsftp sftp,string remoteuploaddir,string fileabspath) throws serviceexception{
Check param
if (Stringutils.isempty (Fileabspath) | | Stringutils.isempty (Remoteuploaddir)) {
throw new Serviceexception (exceptioncode.illegal_argument, "Upload file absolute path or remote directory cannot be empty");
}
File Uploadfile=new file (Fileabspath);
if (!uploadfile.isfile ()) {
throw new Serviceexception (Exceptioncode.other_exception, String.Format ("File path: [%s] is not a file", Fileabspath));
}
Upload
try {
SFTP.CD (Remoteuploaddir);
Sftp.put (New FileInputStream (UploadFile), Uploadfile.getname ());
} catch (Exception e) {
throw new Serviceexception (Exceptioncode.other_exception, String.Format ("Upload file to SFTP Server exception:%s", e.tostring ()));
}
}
/**
*
* Method Description: Download the specified file to the local directory from the specified directory on the SFTP server <br>
*
* @param sftp SFTP
* @param localPath Local Path eg:/weblogic/test/
* @param remoteabspath remote to download the absolute path of the file Eg:.../.../test.txt
* @param localfilename Local file name
* @throws serviceexception Serviceexception
*/
public static void Download (channelsftp sftp,string localpath,string remoteabspath,string localfilename) throws serviceexception{
Check param
if (Stringutils.isempty (LocalPath)
|| Stringutils.isempty (Remoteabspath)
|| Stringutils.isempty (LocalFilename)) {
throw new Serviceexception (exceptioncode.illegal_argument, "must argument cannot be null");
}
File Localdir=new file (LocalPath);
if (!localdir.isdirectory ()) {
throw new Serviceexception (Exceptioncode.other_exception,string.format ("Path: [%s] is not a directory", LocalPath));
}
try {
@SuppressWarnings ("Unchecked")
Vector<lsentry> vector= (vector<lsentry>) sftp.ls (Remoteabspath);
if (vector==null| | Vector.size ()!=1) {
Target File not exist!
throw new Serviceexception (Exceptioncode.other_exception,string.format ("file to download: [%s] does not exist on the SFTP server", LocalFilename));
}
Delete local Duplicate files
String Fullfilepath=localpath.endswith (file.separator)? LocalPath: (Localpath+file.separator) +localfilename;
File Oldfile=new file (Fullfilepath);
if (oldfile.exists ()) {
Oldfile.delete ();
}
Sftp.get (Remoteabspath, Fullfilepath);
} catch (Sftpexception e) {
throw new Serviceexception (Exceptioncode.other_exception, String.Format ("Download file exception from SFTP server:%s", e.tostring ()));
}
}
public static void CloseSession (Channelsftp sftp) {
if (sftp!=null) {
try {
Sftp.getsession (). disconnect ();
} catch (Jschexception e) {
Logger.error ("Off SFTP session exception", E);
}
}
}
SFTP Password-Free login