跨服务器上传下载文件(jdk1.8)
1、创建连接
/**
* 连接远程服务器
*
* @param type
* @return sftp
*/
public static ChannelSftp sftpConnect(Session sshSession) throws Exception {
ChannelSftp sftp = null;
Channel channel = null;
String host = "x.x.x.x";
try {
// 创建连接
channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("连接到主机" + host + "");
} catch (JSchException e) {
System.out.println("未连接到主机" + host + "");
sshSession.disconnect();
throw e;
}
return sftp;
}
2、使用服务器用户名密码登录
/**
* 链接sftp host 主机 port 端口 username 用户名 password 密码
*
* @return
*/
public static Session connect() throws Exception {
Session sshSession = null;
String host = "x.x.x.x";
int port = 22;
String user = "user";
String pwd = "********";
try {
JSch jsch = new JSch();
sshSession = jsch.getSession(user, host, port);
System.out.println("Session创建成功");
sshSession.setPassword(pwd);
sshSession.setTimeout(30000);
System.out.println("密码输入成功");
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
System.out.println("链接参数设置成功");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session已连接");
} catch (JSchException e) {
System.out.println("sshsession 连接失败");
e.printStackTrace();
throw e;
}
return sshSession;
}
3、文件相关操作
/**
* 文件下载
*
* @param directory
* 目录
* @param downloadFile
* 要下载文件路径
* @param saveFile
* 保存到本地的文件路径
* @param sftp
*/
public static void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
buildPath(saveFile);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 构建路径
*
* @param filePath
*/
public static void buildPath(String filePath) {
int index = filePath.lastIndexOf("/");
String path = filePath.substring(0, index);
File directoryPath = new File(path);
if (!directoryPath.exists()) {
buildPath(path);
directoryPath.mkdirs();
}
return;
}
/**
* 文件重命名
*
* @param directory
* 目录
* @param oldname
* 旧文件名
* @param newname
* 新文件名
* @param sftp
*/
public static void renameFile(String directory, String oldname, String newname, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rename(oldname, newname);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
*
* @param directory
* 目录
* @param uploadFile
* 要上传的文件名
* @param sftp
*/
public static void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
try {
sftp.cd(directory);
} catch (Exception e) {
sftp.mkdir(directory);
sftp.cd(directory);
}
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
System.out.println("上传成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件删除
*
* @param directory
* 目录
* @param deleteFile
* 要删除的文件名
* @param sftp
*/
public static void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
System.out.println("删除成功");
} catch (Exception e) {
System.out.println("删除失败");
e.printStackTrace();
}
}