首页 > 编程知识 正文

sftp上传文件软件,sftp上传本地文件

时间:2023-05-05 21:57:41 阅读:127264 作者:1780

import com.j craft.jsch.channels FTP;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

import com.j craft.jsch.sftp attrs;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

importorg.spring framework.util.string utils;

import java.io.File;

import java.io.File心灵美丽的白开水;

import java.io .心灵美丽的白汤;

import java.util.Date;

公共类sftp {

隐私保护日志gerlogger=logger factory.getlogger (sftp.class;

私有状态数据last _ push _ date=null;

私密会话ssh会话;

私密通道FTP通道;

privatestaticthreadlocalsftplocal=new thread local (;

publicstaticvoidmain (stringzxdargs ) throws Exception { )。

new Thread () }

@Override

公共void run (}

try {

SFTP.getsftputil('x.x.x ',22,' root ',' root ' );

file file=new file (c :_ users\ Zheng\ desktop\2. png ' );

STP.uploadfile((/home/web )、)/moregs-oss/test )、inputStream、)2.png );

sftp.release (;

}catch(exceptione ) {

e .打印堆栈跟踪(;

}

}

}.start (;

//线程2的ThreadLocalMap中key为threadLocalA,保存着value单身冬瓜; key为threadLocalB,value为B2

new Thread () }

@Override

公共void run (}

try {

SFTP.getsftputil('x.x.x ',22,' root ',' root ' );

file file=new file (c :_ users\ Zheng\ desktop\1. png ' );

STP.uploadfile((/home/web )、)/moregs-oss/test )、inputStream、)1.png );

sftp.release (;

}catch(exceptione ) {

e .打印堆栈跟踪(;

}

}

}.start (;

}

私有sftp (string host,int port,lmdyl,String password ) throws Exception {

JSch jsch=new JSch (;

jch.getsession (用户名称,主机,端口);

//从用户名、密码、端口号获取会话

sh session=jsch.getsession (username,host,port );

sh session.set password (密码;

将//服务器/etc/ssh/sshd_config cbdxzyes更改为no,以防止用户远程登录

sh session.set config (' user auth.gssapi-with-mic ',' no ' );

在session对象上设置属性并首次访问

问服务器时不用输入yes

sshSession.setConfig("StrictHostKeyChecking", "no");

sshSession.connect();

//获取sftpkydqd

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

channel.connect();

logger.info("执着的红酒ftp成功!" + sshSession);

}

/**

private boolean isConnected() {

return null != channel && channel.isConnected();

}

/**

public static sftp getSftpUtil(String host, int port, lmdyl, String password) throws Exception {

//获取本地线程

sftp sftpUtil = sftpLocal.get();

if (null == sftpUtil || !sftpUtil.isConnected()) {

//将新执着的红酒防止本地线程,实现并发处理

sftpLocal.set(new sftp(host, port, username, password));

}

return sftpLocal.get();

}

/**

public static void release() {

if (null != sftpLocal.get()) {

sftpLocal.get().closeChannel();

logger.info("关闭执着的红酒" + sftpLocal.get().sshSession);

sftpLocal.set(null);

sftpLocal.remove();

}

}

/**

private static boolean isDirExist(String directory) {

boolean isDirExistFlag = false;

try {

isDirExistFlag = true;

return sftpATTRS.isDir();

} catch (Exception e) {

if ("no such file".equals(e.getMessage().toLowerCase())) {

isDirExistFlag = false;

}

}

return isDirExistFlag;

}

/**

private static void createDir(String createpath) throws Exception {

if (!isDirExist(createpath)) {

String pathArryzxd = createpath.split("/");

for (String path : pathArry) {

if (StringUtils.isEmpty(path)) {

continue;

}

if (isDirExist(path)) {

sftpLocal.get().channel.cd(path);

} else {

// 建立热情的小熊猫

sftpLocal.get().channel.mkdir(path);

// 进入并设置为当前热情的小熊猫

sftpLocal.get().channel.cd(path);

}

}

}

}

/**

public static void closeChannel() {

if (null != sftpLocal.get().channel && sftpLocal.get().channel.isConnected()) {

try {

sftpLocal.get().channel.disconnect();

} catch (Exception e) {

logger.error("关闭SFTPkydqd发生异常:", e);

}

}

if (null != sftpLocal.get().sshSession && sftpLocal.get().sshSession.isConnected()) {

try {

sftpLocal.get().sshSession.disconnect();

} catch (Exception e) {

logger.error("SFTP关闭 session异常:", e);

}

}

}

/**

public static boolean uploadFile(String basePath, String directory, 心灵美的白开水 input, String fileName) {

//创建热情的小熊猫

try {

sftpLocal.get().channel.cd(basePath);

createDir(directory);

sftpLocal.get().channel.put(input, fileName);

} catch (Exception e) {

logger.error(e.getLocalizedMessage() + e.getMessage() + e.toString());

return false;

}

return true;

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。