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. CF765F Souvenirs 离线+线段树+主席树

    $ \color{#0066ff}{ 题目描述 }$ A县旁,连绵着一条长度为 n 的山脉,这条山脉由 n 座山峰组成,第 i 座山 峰的高度为 ai.作为著名的旅游县城,每天来到山脉游玩的旅客络绎不 ...

  2. 编程开发之--Oracle数据库--存储过程和存储函数(1)

    1.存储过程和存储函数 描述:指存储在数据库中供所有用户程序调用的子程序叫做存储过程.存储函数 区别:存储函数可以通过return子句返回一个函数的值 (1)存储过程 语法:create [or re ...

  3. ubuntu 16.04 安装opencv 2.4.13

    ubuntu 16.04 安装opencv 2.4.13 https://blog.csdn.net/u011557212/article/details/54706966?utm_source=it ...

  4. php fputcsv 读取不到中文文件、数据

    string  setlocale(constant,location) constant 必需.规定应该设置什么地区信息. 可用的常量: LC_ALL - 包括下面的所有选项 LC_COLLATE ...

  5. python学习,day1作业:设计一个三级菜单

    设计一个三级菜单,要求能够下钻,能够推出,比较笨的方法 # coding=utf-8 # Author: RyAn Bi data = { 'beijing':{ 'changping':{ 'wud ...

  6. node爬取页面元素

    /** * Created by on 2018/12/25. */const http = require("https");const fs = require('fs'); ...

  7. 通过shell处理多行数据

    ### 源文件 cat > tmpb <<'EOF' dbname:db_a,start_time::: query_end_time:::,query_total_time:,da ...

  8. 问题诊断神器arthas

    https://github.com/alibaba/arthas 镜像地址 https://gitee.com/arthas/arthas OGNL https://commons.apache.o ...

  9. MySQL多表更新

    多表更新:参照另外的表来更新本表的内容 table_reference {[inner | cross] join | {left | right} [outer] join}  内连接.左外连接.右 ...

  10. 013-PaymentUtils工具类模板

    package ${enclosing_package}; import java.io.UnsupportedEncodingException; import java.security.Mess ...