昨天發的一篇隨筆,介紹的是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();
}
}
}