package sftp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
/**
* 将目标路径化为数组,循环判断是否能进入目录;
* 1、能进入:进入目录,继续循环
* 2、不能进入:创建目录,继续循环
*
* @author Administrator
*
*/
public class SftpUpload {
Channel channel = null;
Session session = null;
public static void main(String[] args) {
SftpUpload sft = new SftpUpload();
Date date = new Date();
//源文件路径
String src = "C:\\Users\\Administrator\\Desktop\\看--不--见--我.txt";
//目标路径
String dst = "G:\\110-ftp\\110";
//根路径
String root_dst ="G:\\110-ftp";
sft.getChannel("127.0.0.1", 22, "root", "root", src, dst,root_dst, date);
}
/**
* 进入指定目录上传文件
* @param ip ip地址
* @param port 端口
* @param userName 用户名
* @param passWord 密码
* @param src 源文件
* @param dst 目标文件
* @param root_dst 根路径
* @param date 当前日期
*/
@SuppressWarnings({ "rawtypes", "unused" })
public void getChannel(String ip,int port,String userName,String passWord,String src,String dst,String root_dst,Date date){
// 创建JSch对象
JSch jsch = new JSch();
// 根据用户名,主机ip,端口获取一个Session对象
try {
session = jsch.getSession(userName, ip, port);
if (passWord != null) {
// 设置密码
session.setPassword(passWord);
}
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
// 通过Session建立链接
session.connect();
// 打开SFTP通道
channel = session.openChannel("sftp");
// 建立SFTP通道的连接
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
}
ChannelSftp chsft = (ChannelSftp) channel;
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String str = df.format(date);
String dst_old = dst;
int count = getCount(dst)-1;
System.out.println(count+"--级目录--");
String src_sr = src.substring(src.lastIndexOf("\\")+1);
//自动补全目录
switch(count) {
case 0:
dst+= "\\"+"upload"+"\\"+"LoanApply"+"\\"+str+"\\"+"授信编号"+"\\"+src_sr;
break;
case 1:
dst+= "\\"+"LoanApply"+"\\"+str+"\\"+"授信编号"+"\\"+src_sr;
break;
case 2:
dst+= "\\"+str+"\\"+"授信编号"+"\\"+src_sr;
break;
case 3:
dst+= "\\"+"授信编号"+"\\"+src_sr;
break;
case 4:
dst+="\\"+src_sr;
break;
case 5:
break;
}
System.out.println(dst);
//dst去除根路径
String cut_dst = dst.substring(root_dst.length()+1);
System.out.println(cut_dst);
String dst_s = cut_dst;
if (dst.indexOf("\\") != -1) {
dst_s = cut_dst.replaceAll("\\\\", "/");
}
String[] paths = dst_s.split("/");
//是否是目录或文件
boolean isFile = true;
for (int i = 0; i < paths.length-1; i++) {
String dir = paths[i];
System.out.println(dir);
try {
chsft.cd(dir);
isFile = true;
} catch (SftpException e) {
isFile = false;
}
if(isFile == false) {
try {
chsft.mkdir(dir);
chsft.cd(dir);
} catch (SftpException e) {
e.printStackTrace();
}
}
}
try {
//上传目标文件(覆盖式)
chsft.put(src, src_sr,ChannelSftp.OVERWRITE);
System.out.println("执行完毕");
} catch (SftpException e) {
e.printStackTrace();
}
//关闭连接
chsft.quit();
try {
closeChannel();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取\的数量判断几级目录
* @param dst
* @return
*/
public int getCount(String dst) {
int count = 0;
int leng = dst.length();
String dst_d = dst.replaceAll("\\\\", "");
count = leng-dst_d.length();
return count;
}
/**
* 关闭连接
* @throws Exception
*/
public void closeChannel() throws Exception {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
此方法不是很成熟、完善。
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
/**
* 将目标路径化为数组,循环判断是否能进入目录;
* 1、能进入:进入目录,继续循环
* 2、不能进入:创建目录,继续循环
*
* @author Administrator
*
*/
public class SftpUpload {
Channel channel = null;
Session session = null;
public static void main(String[] args) {
SftpUpload sft = new SftpUpload();
Date date = new Date();
//源文件路径
String src = "C:\\Users\\Administrator\\Desktop\\看--不--见--我.txt";
//目标路径
String dst = "G:\\110-ftp\\110";
//根路径
String root_dst ="G:\\110-ftp";
sft.getChannel("127.0.0.1", 22, "root", "root", src, dst,root_dst, date);
}
/**
* 进入指定目录上传文件
* @param ip ip地址
* @param port 端口
* @param userName 用户名
* @param passWord 密码
* @param src 源文件
* @param dst 目标文件
* @param root_dst 根路径
* @param date 当前日期
*/
@SuppressWarnings({ "rawtypes", "unused" })
public void getChannel(String ip,int port,String userName,String passWord,String src,String dst,String root_dst,Date date){
// 创建JSch对象
JSch jsch = new JSch();
// 根据用户名,主机ip,端口获取一个Session对象
try {
session = jsch.getSession(userName, ip, port);
if (passWord != null) {
// 设置密码
session.setPassword(passWord);
}
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
// 通过Session建立链接
session.connect();
// 打开SFTP通道
channel = session.openChannel("sftp");
// 建立SFTP通道的连接
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
}
ChannelSftp chsft = (ChannelSftp) channel;
DateFormat df = new SimpleDateFormat("yyyyMMdd");
String str = df.format(date);
String dst_old = dst;
int count = getCount(dst)-1;
System.out.println(count+"--级目录--");
String src_sr = src.substring(src.lastIndexOf("\\")+1);
//自动补全目录
switch(count) {
case 0:
dst+= "\\"+"upload"+"\\"+"LoanApply"+"\\"+str+"\\"+"授信编号"+"\\"+src_sr;
break;
case 1:
dst+= "\\"+"LoanApply"+"\\"+str+"\\"+"授信编号"+"\\"+src_sr;
break;
case 2:
dst+= "\\"+str+"\\"+"授信编号"+"\\"+src_sr;
break;
case 3:
dst+= "\\"+"授信编号"+"\\"+src_sr;
break;
case 4:
dst+="\\"+src_sr;
break;
case 5:
break;
}
System.out.println(dst);
//dst去除根路径
String cut_dst = dst.substring(root_dst.length()+1);
System.out.println(cut_dst);
String dst_s = cut_dst;
if (dst.indexOf("\\") != -1) {
dst_s = cut_dst.replaceAll("\\\\", "/");
}
String[] paths = dst_s.split("/");
//是否是目录或文件
boolean isFile = true;
for (int i = 0; i < paths.length-1; i++) {
String dir = paths[i];
System.out.println(dir);
try {
chsft.cd(dir);
isFile = true;
} catch (SftpException e) {
isFile = false;
}
if(isFile == false) {
try {
chsft.mkdir(dir);
chsft.cd(dir);
} catch (SftpException e) {
e.printStackTrace();
}
}
}
try {
//上传目标文件(覆盖式)
chsft.put(src, src_sr,ChannelSftp.OVERWRITE);
System.out.println("执行完毕");
} catch (SftpException e) {
e.printStackTrace();
}
//关闭连接
chsft.quit();
try {
closeChannel();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取\的数量判断几级目录
* @param dst
* @return
*/
public int getCount(String dst) {
int count = 0;
int leng = dst.length();
String dst_d = dst.replaceAll("\\\\", "");
count = leng-dst_d.length();
return count;
}
/**
* 关闭连接
* @throws Exception
*/
public void closeChannel() throws Exception {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
此方法不是很成熟、完善。