java使用SSHd远程连接windows服务器上传本地文件夹到指定目录并保持文件夹的目录结构

//仅支持有后缀名的文件,可以保持目录结构,如果文件夹中有的文件没有后缀名,则不能保持目录结构!!!
package cn.sanjin.javawindows;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SftpFile {
private static Log log = LogFactory.getLog(SftpFile.class);
private static int sign1;
private static int sign2;
private static String cutMoveFilePath;
/**
@Description: 上传文件
* @param host Ip地址
* @param port 端口号
* @param user 用户名
* @param password 密码
* @param remoteFilePath 远程保存文件的共享目录
* @param localFilePath 本地文件的上传目录
* @param command 要执行的cmd命令
* @throws
/
public static void uploadFile(String host, Integer port, String user, String password,String remoteFilePath, String localFilePath,String command) {
try {
Session session = SftpFile.connect(“192.168.1.68”,22,“root”,“root”);
/

* 文件传输通道
/
Channel fileChannel = session.openChannel(“sftp”);
/
*
* 命令传输通道
/
Channel cmdChannel = session.openChannel(“exec”);
Channel cmdChannel2 = session.openChannel(“exec”);
/
*
* 设置连接超时
/
fileChannel.connect();
ChannelSftp sftp = (ChannelSftp) fileChannel;
/
*
* 上传文件
/
SftpFile.upload(remoteFilePath, localFilePath, sftp);
/
*
* 移动文件到指定目录
/
String command1 =command.substring(0,command.indexOf("&")).trim();
SftpFile.execCmd(cmdChannel, command1);
/
*
* 删除共享目录文件
*/
String command2 = command.substring(command.indexOf("&")+1).trim();
SftpFile.execDelCmd(cmdChannel2, command2);
if(sftp != null) {
sftp.disconnect();
}
if(session != null) {
session.disconnect();
}

	} ca
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值