下載sftp所需要用到的jar包:com.jcraft.jsch_0.1.31.jar
所用到的文件操作工具類代碼如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 Util {
/**
* 鏈接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);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
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();
}
}
/**
* 文件下載
*
* @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);
}
}
測試代碼:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.junit.Test;
import com.chigo.utils.SFTPUtil;
import com.jcraft.jsch.ChannelSftp;
public class Test01 {
//建立鏈接
SFTPUtil sf = new SFTPUtil();
String host = "192.168.130.33";
int port = 22;
String username = "root";
String password = "abc345";
ChannelSftp sftp=sf.connect(host, port, username, password);
@Test
public void testList() throws Exception{
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/product/Home_appliances/";
Vector v = sf.listFiles(directory, sftp);
for(int i = 0;i < sf.listFiles(directory, sftp).size();i++){
System.out.println(v.get(i));
}
List<String> list = new ArrayList<String>();
for(int i = 2;i < sf.listFiles(directory, sftp).size();i++){
list.add(v.get(i).toString().substring(55, v.get(i).toString().length()));
System.out.println(v.get(i).toString().substring(55, v.get(i).toString().length()));
}
System.out.println(list.toString());
}
@Test
public void testUpload(){
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/homeAnimation/";
sf.upload(directory, "C:/product01.JPG", sftp);
}
@Test
public void testRenameFile(){
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/homeAnimation/";
String oldname = "baobiao1.png";
String newname = "a.png";
sf.renameFile(directory, oldname, newname, sftp);
}
@Test
public void testDelFile(){
String directory = "/usr/local/apache-tomcat-7.0.62/webapps/images/homeAnimation/";
String deleteFile = "baobiao1.png";
sf.delete(directory, deleteFile, sftp);
}
}