昨天发的一篇随笔,介绍的是Java实现FTP跨服务器文件操作,今天我们介绍下Java实现SFTP跨服务器的文件操作,包括文件上传下载和删除等。Java要实现SFTP跨服务器文件操作需要导入jar包:jsch-0.1.24.jar
具体实现代码如下:
package myTools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
/**
* SFTP 文件上传工具类
* 所需jar包(jsch-0.1.24.jar)
*
*/
public class SftpUtils {
public ChannelSftp sftp;
public Session sshSession;
/**
* 连接sftp服务器
*
* @param host
* 主机
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @return
*/
public void connect(String host, int port, String username,String password) {
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
}
}
/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
*/
public void upload(String directory, String uploadFile) {
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());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param uploadFile 要上传的文件
* @param fileName 上传后的文件名
*/
public void uploadFile( String uploadFile,String fileName) {
try {
File file = new File(uploadFile);
sftp.put(new FileInputStream(file), fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
*/
public void download(String directory, String downloadFile,
String saveFile) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param downloadFile 要下载的文件
* @param saveFile 存在本地的路径
*/
public void download(String downloadFile,String saveFile) {
try {
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
*/
public void delete(String directory, String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 测试方法
* @param args
*/
public static void main(String[] args) {
String host1 = "192.168.0.122";
int port = 22;
String username = "aaa";
String password = "aa123";
String directory = "testFile";
String directory1 = "ftpFile/data/abc";
String directory2 = "E://abc/";
try {
SftpUtils sf = new SftpUtils();
//SftpUtils sf2 = new SftpUtils();
sf.connect(host1, port, username, password);
//sf2.connect(host2, port, username, password);
sf.sftp.cd(directory1);
//sf2.sftp.cd(directory);
//sf.download("道德经.docx", directory2+"temp.temp");
//sf2.uploadFile(directory2+"temp.temp","道德经.docx");
//sf.download("啊实打实.pdf", directory2+"temp.temp");
//sf2.uploadFile(directory2+"temp.temp","啊实打实.pdf");
sf.download("123.docx", directory2+"123.docx");
//sf2.download("道德经.docx", directory2+"道德经.docx");
sf.sftp.disconnect();
sf.sshSession.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}