java sftp.mkdir 报错_sftp java 上传

private void transfer_sftp(String attrPath, Element groupNode, long size) throwsJSchException, SftpException, UnsupportedEncodingException{

attrPath= attrPath.replaceAll("sftp://", "");

String[] tmps= attrPath.split(":");

String dstUserName= tmps[0];int port = 22;

String portstr= tmps[2].replaceAll("/", "");try{

port=Integer.parseInt(portstr);

}catch(NumberFormatException e) {

e.printStackTrace();

}

String[] tmp= tmps[1].split("@");

String dstPassword= tmp[0];

String sftpIP= tmp[1];

List> files = groupNode.selectNodes("File");for(int k=0; k

JSch jsch= newJSch();

Session session=jsch.getSession(dstUserName, sftpIP, port);

logger.info("JSch Session created: user:"+dstUserName + ",password="+dstPassword+",sftpIP="+sftpIP+",port="+port);

session.setPassword(dstPassword);

Properties sshConfig= newProperties();

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

session.setConfig(sshConfig);

session.connect();

logger.info("JSch Session connected > user:"+dstUserName);

ChannelSftp channelSftp= (ChannelSftp) session.openChannel("sftp");

channelSftp.connect();

logger.info("Connected to "+sftpIP);

Element file=(Element) files.get(k);

String srcFilePath= file.elementText("SrcFileName");

String dstFilePath= file.elementText("DstFileName");

logger.info("upload file on sftp protocol >> srcFilePath="+srcFilePath + ",dstFilePath="+dstFilePath);int index = dstFilePath.lastIndexOf("/");

String path= dstFilePath.substring(0, index + 1);

String filename= dstFilePath.substring(index + 1, dstFilePath.length());if (path.startsWith("/"))

path= path.substring(1);

logger.info("dest path:"+path);if (filename.startsWith("/"))

filename= filename.substring(1);

String[] pathArray= path.split("/");for(String pathStr : pathArray) {try{

logger.info("channelsftp dir ::"+pathStr);

Vector ls=channelSftp.ls(pathStr);if(ls != null){

channelSftp.cd(pathStr);

}

}catch(Exception e) {

logger.info("channelsftp catch mkdir ::"+pathStr);

channelSftp.mkdir(pathStr);

channelSftp.cd(pathStr);

e.printStackTrace();

}

}//String dir = new String(path.getBytes("gb2312"), "iso-8859-1");//String destName = new String(filename.getBytes("gb2312"), "iso-8859-1");//System.out.println("上传目录名名:::"+dir);//System.out.println("上传后文件名:::"+destName);//channelSftp.put(srcFilePath, dstFilePath, new SftpProgressMonitor() {

channelSftp.put(srcFilePath, filename, newSftpProgressMonitor() {long n = 0L;long _max = 0L;public longgetN() {returnn;

}public void setN(longn) {this.n =n;

}

@Overridepublic void init(int arg0, String arg1, String arg2, longmax) {this._max =max;

logger.info("SFTP Transferring begin....._max="+_max);

}

@Overridepublic voidend() {

logger.info("SFTP Transferring end....");

}

@Overridepublic boolean count(longcount) {

n+=count;//logger.info("SFTP Transferring count="+count+",n="+n);

if(_max!=0){

run_progress= (int) ((n*100)/_max);//logger.info("the progress of upload file through sftp :" + run_progress + "%");

}return true;

}

}, ChannelSftp.OVERWRITE);

channelSftp.quit();

session.disconnect();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值