1. 注意问题

uri的格式: sftp://zhangsan:123456@10.10.10.10:22

dir问题 : 判断有没有 没有创建 然后进入 类推

config问题: StrictHostKeyChecking=no

进度问题 : 需要变量保存

2. 代码

    private void transfer_sftp(String attrPath, Element groupNode, long size) throws JSchException, 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<files.size(); k++){
JSch jsch = new JSch();
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 = new Properties();
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, new SftpProgressMonitor() { long n = 0L;
long _max = 0L; public long getN() {
return n;
} public void setN(long n) {
this.n = n;
} @Override
public void init(int arg0, String arg1, String arg2, long max) {
this._max = max;
logger.info("SFTP Transferring begin....._max="+_max);
} @Override
public void end() {
logger.info("SFTP Transferring end....");
} @Override
public boolean count(long count) {
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(); }
}

sftp java 上传的更多相关文章

  1. SFTP文件上传与下载

    SFTP是对ftp进行加密的一种文件协议. 首先是先引入:jsch-0.1.42-sources.jar与jsch-0.1.42.jar包 SFTP文件上传与下载. import java.io.Fi ...

  2. edtftpj让Java上传FTP文件支持断点续传

    在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一 ...

  3. Java上传文件FTP服务器代码

    1. 在实际的应用重,通常是通过程序来进行文件的上传. 2. 实现java上传文件到ftp服务器中 新建maven项目 添加依赖 <dependency> <groupId>c ...

  4. java上传excel文件及解析

      java上传excel文件及解析 CreateTime--2018年3月5日16:25:14 Author:Marydon 一.准备工作 1.1 文件上传插件:swfupload: 1.2 文件上 ...

  5. java 上传文件到 ftp 服务器

    1.  java 上传文件到 ftp 服务器 package com.taotao.common.utils; import java.io.File; import java.io.FileInpu ...

  6. Java 上传文件到 SFTP 抛异常 java.lang.NoClassDefFoundError: Could not initialize class sun.security.ec.SunEC 的解决办法

    最近从 Op 那里报来一个问题,说是SFTP上传文件不成功.拿到的 Exception 如下: Caused by: java.lang.NoClassDefFoundError: Could not ...

  7. java 通过sftp服务器上传下载删除文件

    最近做了一个sftp服务器文件下载的功能,mark一下: 首先是一个SftpClientUtil 类,封装了对sftp服务器文件上传.下载.删除的方法 import java.io.File; imp ...

  8. java实现sftp客户端上传文件夹的功能

    使用的jar: <dependencies> <dependency> <groupId>jsch</groupId> <artifactId&g ...

  9. java实现sftp客户端上传文件以及文件夹的功能

    1.依赖的jar文件 jsch-0.1.53.jar 2.登录方式有密码登录,和密匙登录 代码: 主函数: import java.util.Properties; import com.cloudp ...

随机推荐

  1. 两种 js下载文件的步骤

    ----------------------------------引用地址链接------------------------------------------------- http://www ...

  2. thinkphp3.2.3----图片上传并生成缩率图

    public function uploadify(){ if(!IS_POST){ $this->error('非法!'); } $upload = $this->_upload(); ...

  3. iOS 循环轮播框架思路

    使用3个imageview实现无线轮播的大致原理 将3个imageview添加到scrollview上面,scrollview的contensize是3个imageview的宽度,设置scrollvi ...

  4. (STM32F4) Timer 基本操作

    Timer (計時器) 就是慢慢數時間,在timer內部有一個計數器. 而計數器會數到Register的value當數值數到設定值Timer就會發起IRQ 而程式就會轉跳到中斷向量裡頭去執行想要做的事 ...

  5. IntelliJ IDEA 版本控制(svn、git) 修改文件后,所属目录的颜色也变化

    IntelliJ IDEA 的版本控制默认文件修改了,所属目录的颜色是不会变化,这很不方便.如: 修改方法如下: File --> settings --> version control ...

  6. maven 打包 源代码

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-sourc ...

  7. 基础篇:6.5)形位公差-公差带 Tolerance Zone

    本章目的:了解14个形位公差的公差带形状,其从属关系. 1.定义 公差带-实际被测要素允许变动的区域. 它体现了对被测要素的设计要求,也是加工和检验的根据. 2.公差带四大特征-形状.大小.方向.位置 ...

  8. UnxUtils让windows下的dos命令变为linux下的命令

    一.UnxUtils UnxUtils是一个可以支持在Windows下使用linux命令的工具,用习惯了linux之后,感觉Windows的dos命令实在是太难用了,发现了这个工具,非常的小,装了它之 ...

  9. selenium+Python(处理html5的视频播放)

    Webdriver支持在指定的浏览器测试HTML5,另外可以用JavaScript来测试这些功能,这样就可以在任何浏览器上测试HTML5 多数浏览器使用控件来播放视频,但是不同浏览器需要使用不同的插件 ...

  10. 【ExtJS】关于Component生命周期

    很久以前就学习过extjs的组件生命周期,很久之后,再回头看一看,又增加好多新的认识. extjs组件生命周期大体分为3个阶段:初始化.渲染.销毁. 第一阶段:初始化 初始化工作开始于组件的诞生,所有 ...