Sftp实现服务器之间文件传输java代码实现
使用sftp在服务器之间传输时要先确定服务器之间的sftp链接权限是开通的,使用如下方法如果出现密码输入提示就说明要连接的服务器之间是联通的。
1.定义一个sftp工具类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SftpTools {
/**
* 链接sftp
*
* @param host
* 主机
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @return
*/
public ChannelSftp connect(String host, int port, String username,
String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
System.out.println("Session创建成功");
sshSession.setPassword(password);
System.out.println("密码输入成功");
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
System.out.println("链接参数设置成功");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session已连接");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("连接到主机" + host + ".");
} catch (Exception e) {
e.printStackTrace();
}
return sftp;
}
/**
* 文件重命名
*
* @param directory 目录
* @param oldname 旧文件名
* @param newname 新文件名
* @param sftp
*/
public 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 void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
public void uploada(String directory,String sftpFileName,InputStream input, ChannelSftp sftp) throws SftpException{
try {
sftp.cd(directory);
} catch (Exception e) {
// TODO: handle exception
}
sftp.put(input, sftpFileName);
}
/**
* 文件下载
*
* @param directory 目录
* @param downloadFile 要下载文件名
* @param saveFile 保持的文件名
* @param sftp
*/
public void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件删除
*
* @param directory 目录
* @param deleteFile 要删除的文件名
* @param sftp
*/
public 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();
}
}
/**
* 列出目录下的文件
*
* @param directory 目录
* @param sftp
* @return
* @throws SftpException
*/
public Vector listFiles(String directory, ChannelSftp sftp)
throws SftpException {
return sftp.ls(directory);
}
//批量删除文件
public void delete(String directory, String[] fileNames, ChannelSftp aa) {
for (String fileName : fileNames) {
this.delete(directory, fileName, aa);
}
}
/**
* 创建目录文件夹
* @param directory 要创建文件夹的位置路径
* @param fileName 要创建文件夹的名称
* @param sftp sftp连接
*/
//创建目录文件
public void mkdir(String directory,String fileName,ChannelSftp sftp){
try {
sftp.cd(directory);
sftp.mkdir(fileName);
System.out.println("文件夹创建成功");
} catch (Exception e) {
System.out.println("文件夹创建失败");
e.printStackTrace();
}
}
}
2.使用工具类实现一个文件下载功能
public static void downLoadPic( String path,String downLoadFileName,String newFileName){
SftpTools sftpTools = new SftpTools();
//测试环境地址
String host="10.1.1.105";
int port=22;
String username="weblogic";
String password="1232343";
String directory="/weblogic/wls1036_x64/user_projects/domains/sasdomain/app/zyx_server/images/showpic/";
//建立sftp连接
ChannelSftp content= sftpTools.connect(host, port, username, password);
String saveFile = newFileName;
//下载文件
sftpTools.download(directory, downLoadFileName, saveFile, content);
}