java jcraft 隐藏目录_java实操之使用jcraft进行sftp上传下载文件

public classSftpUploadTest {private ChannelSftp sftp = null;private Session sshSession = null;privateString username;privateString password;privateString host;private intport;public SftpUploadTest(String username, String password, String host, intport) {this.username =username;this.password =password;this.host =host;this.port =port;

}/*** 连接sftp服务器

*

*@returnChannelSftp sftp连接实例*/

publicChannelSftp connect() {

info("-->sftp连接开始>>>>>> " + host + ":" + port + " >>>username=" +username);

JSch jsch= newJSch();try{

jsch.getSession(username, host, port);

sshSession=jsch.getSession(username, host, port);

sshSession.setPassword(password);

Properties properties= newProperties();

properties.put("StrictHostKeyChecking", "no");

sshSession.setConfig(properties);

sshSession.connect();

Channel channel= sshSession.openChannel("sftp");

channel.connect();

sftp=(ChannelSftp) channel;

info(" ftp Connected to " + host + ":" +port);

}catch(JSchException e) {throw new RuntimeException("sftp连接失败", e);

}returnsftp;

}/*** 下载单个文件,如果指定文件名,则下载到文件名否则保持原有文件名

*

*@paramremoteFilePath 远程文件路径 /tmp/xxx.txt || xxx.txt.zip

*@paramlocalFilePath 本地文件路径 如 D:\\xxx.txt

*@return下载的文件*/

publicFile downloadFile(String remoteFilePath, String localFilePath) {

info(">>>>>>>>>downloadFile--ftp下载文件" + remoteFilePath + "开始>>>>>>>>>>>>>");

connect();

String remoteFileName= "";//远端目录确定以 / 作为目录格式

String rFileSeparator = "/";int rDirNameSepIndex = remoteFilePath.lastIndexOf(rFileSeparator) + 1;

String rDir= remoteFilePath.substring(0, rDirNameSepIndex);

remoteFileName=remoteFilePath.substring(rDirNameSepIndex);if(localFilePath.endsWith(File.separator)) {

localFilePath= localFilePath + (localFilePath.endsWith(File.separator) ? remoteFileName : "/" +remoteFileName);

}

File file= null;

OutputStream output= null;try{

file= newFile(localFilePath);if(file.exists()) {

file.delete();

}

file.createNewFile();

sftp.cd(rDir);

output= newFileOutputStream(file);

sftp.get(remoteFileName, output);

info("===DownloadFile:" + remoteFileName + " success from sftp.");

}catch(SftpException e) {

error("ftp下载文件失败", e);

}catch(FileNotFoundException e) {

error("本地目录异常,请检查" +file.getPath(), e);

}catch(IOException e) {

error("创建本地文件失败" +file.getPath(), e);

}finally{if (output != null) {try{

output.close();

}catch(IOException e) {

e.printStackTrace();

}

}

disconnect();

}

info(">>>>>>>>>downloadFile--ftp下载文件结束>>>>>>>>>>>>>");returnfile;

}/*** 上传单个文件,如果指正下载文件名则使用,否则保留原有文件名

*

*@paramremoteFilePath 远程文件路径 /tmp/xxx.txt ||xxx.txt.zip

*@paramuploadFilePath 要上传的文件 如:D:\\test\\xxx.txt*/

public voiduploadFile(String remoteFilePath, String uploadFilePath) {

info(" begin uploadFile from:" + uploadFilePath +

", to: " +remoteFilePath);

FileInputStream in= null;

connect();

String remoteFileName= "";

String remoteDir=remoteFilePath;

String localFileName= "";//远端目录确定以 / 作为目录格式

String rFileSeparator = "/";if(remoteFilePath.endsWith(rFileSeparator)) {

localFileName= uploadFilePath.substring(uploadFilePath.lastIndexOf(File.separator) + 1);

remoteFileName=localFileName;

}else{int fileNameDirSep = remoteFilePath.lastIndexOf(rFileSeparator) + 1;

remoteDir= remoteFilePath.substring(0, fileNameDirSep);

remoteFileName=remoteFilePath.substring(fileNameDirSep);

}try{

sftp.cd(remoteDir);

}catch(SftpException e) {try{

sftp.mkdir(remoteDir);

sftp.cd(remoteDir);

}catch(SftpException e1) {

error("ftp创建文件路径失败,路径为" +remoteDir);throw new RuntimeException("ftp创建文件路径失败" +remoteDir);

}

}

File file= newFile(uploadFilePath);try{

in= newFileInputStream(file);

sftp.put(in, remoteFileName);

}catch(FileNotFoundException e) {

error("文件不存在-->" +uploadFilePath);

}catch(SftpException e) {

error("sftp异常-->", e);

}finally{if (in != null) {try{

in.close();

}catch(IOException e) {

info("Close stream error." +e.getMessage());

}

}

disconnect();

}

info(">>>>>>>>>uploadFile--ftp上传文件结束>>>>>>>>>>>>>");

}/*** 关闭连接*/

public voiddisconnect() {if (this.sftp != null) {if (this.sftp.isConnected()) {this.sftp.disconnect();this.sftp = null;

info("sftp 连接已关闭!");

}

}if (this.sshSession != null) {if (this.sshSession.isConnected()) {this.sshSession.disconnect();this.sshSession = null;

info("sshSession 连接已关闭!");

}

}

}private voidinfo(String msg) {

System.out.println("info: " +msg);

}private voiderror(String msg) {

error(msg,null);

}private voiderror(String msg, Throwable e) {

System.out.println("error: " +msg);if(e != null) {

e.printStackTrace();

}

}public static void main(String[] args) throwsException {

SftpUploadTest uploadTest= new SftpUploadTest("root", "123", "10.40.10.17", 52766);

uploadTest.uploadFile("/tmp/" + "readme-" + System.currentTimeMillis() + ".txt", "C:\\Users\\xx\\Desktop\\sample\\readme.txt");

uploadTest.downloadFile("/tmp/readme.txt", "C:\\Users\\xx\\Desktop\\sample\\readme-" + System.currentTimeMillis() + ".txt");

}

}

com.jcraft.jsch.RequestShell.class com.jcraft.jsch.RequestAgentForwarding.class com.jcraft.jsch.Random.class com.jcraft.jsch.RequestSignal.class com.jcraft.jsch.Compression.class com.jcraft.jsch.SignatureDSA.class com.jcraft.jsch.Logger.class com.jcraft.jsch.RequestEnv.class com.jcraft.jsch.UserAuthPublicKey.class com.jcraft.jsch.RequestX11.class com.jcraft.jsch.RequestSftp.class com.jcraft.jsch.Session.class com.jcraft.jsch.SignatureRSA.class com.jcraft.jsch.JSchAuthCancelException.class com.jcraft.jsch.Cipher.class com.jcraft.jsch.UserAuth.class com.jcraft.jsch.ChannelX11.class com.jcraft.jsch.RequestPtyReq.class com.jcraft.jsch.jce.HMACMD596.class com.jcraft.jsch.jce.Random.class com.jcraft.jsch.jce.SignatureDSA.class com.jcraft.jsch.jce.TripleDESCBC.class com.jcraft.jsch.jce.MD5.class com.jcraft.jsch.jce.SignatureRSA.class com.jcraft.jsch.jce.AES128CBC.class com.jcraft.jsch.jce.AES192CBC.class com.jcraft.jsch.jce.AES256CBC.class com.jcraft.jsch.jce.HMACSHA196.class com.jcraft.jsch.jce.SHA1.class com.jcraft.jsch.jce.HMACSHA1.class com.jcraft.jsch.jce.KeyPairGenRSA.class com.jcraft.jsch.jce.HMACMD5.class com.jcraft.jsch.jce.BlowfishCBC.class com.jcraft.jsch.jce.KeyPairGenDSA.class com.jcraft.jsch.jce.DH.class com.jcraft.jsch.DHGEX.class com.jcraft.jsch.jcraft.HMACMD596.class com.jcraft.jsch.jcraft.Compression.class com.jcraft.jsch.jcraft.HMACSHA196.class com.jcraft.jsch.jcraft.HMACSHA1.class com.jcraft.jsch.jcraft.HMACMD5.class com.jcraft.jsch.jcraft.HMAC.class com.jcraft.jsch.HostKeyRepository.class com.jcraft.jsch.JSch.class com.jcraft.jsch.MAC.class com.jcraft.jsch.Request.class com.jcraft.jsch.ChannelAgentForwarding.class com.jcraft.jsch.UserAuthGSSAPIWithMIC.class com.jcraft.jsch.Channel.class com.jcraft.jsch.ForwardedTCPIPDaemon.class com.jcraft.jsch.SftpProgressMonitor.class com.jcraft.jsch.UserAuthKeyboardInteractive.class com.jcraft.jsch.ChannelExec.class com.jcraft.jsch.SocketFactory.class com.jcraft.jsch.ProxySOCKS5.class com.jcraft.jsch.Buffer.class com.jcraft.jsch.ProxyHTTP.class com.jcraft.jsch.GSSContext.class com.jcraft.jsch.IO.class com.jcraft.jsch.Identity.class com.jcraft.jsch.JSchException.class com.jcraft.jsch.CipherNone.class com.jcraft.jsch.SftpATTRS.class com.jcraft.jsch.KeyPairRSA.class com.jcraft.jsch.Packet.class com.jcraft.jsch.RequestExec.class com.jcraft.jsch.KeyPairGenRSA.class com.jcraft.jsch.ChannelShell.class com.jcraft.jsch.IdentityFile.class com.jcraft.jsch.RequestSubsystem.class com.jcraft.jsch.Proxy.class com.jcraft.jsch.KnownHosts.class com.jcraft.jsch.RequestWindowChange.class com.jcraft.jsch.ChannelSubsystem.class com.jcraft.jsch.HASH.class com.jcraft.jsch.KeyPairDSA.class com.jcraft.jsch.ChannelSftp.class com.jcraft.jsch.HostKey.class com.jcraft.jsch.KeyPairGenDSA.class com.jcraft.jsch.UserAuthNone.class com.jcraft.jsch.UserInfo.class com.jcraft.jsch.ServerSocketFactory.class com.jcraft.jsch.KeyPair.class com.jcraft.jsch.JSchPartialAuthException.class com.jcraft.jsch.DH.class com.jcraft.jsch.KeyExchange.class com.jcraft.jsch.ChannelDirectTCPIP.class com.jcraft.jsch.Util.class com.jcraft.jsch.SftpException.class com.jcraft.jsch.DHG1.class com.jcraft.jsch.ChannelForwardedTCPIP.class com.jcraft.jsch.ChannelSession.class com.jcraft.jsch.PortWatcher.class com.jcraft.jsch.ProxySOCKS4.class com.jcraft.jsch.UserAuthPassword.class com.jcraft.jsch.UIKeyboardInteractive.class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值