1、属性文件
#SFTP IP
ftpHost=127.0.0.1
#SFTP username
ftpUserName=root
#SFTP password
ftpPassword=123456
#SFTP port
ftpPort=22
2、类文件
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class FTPUtils {
private final static Log logger = LogFactory.getLog(FTPUtils.class);
private static String ftpPath = "sftp.properties";
private static String ftpHost;
private static String ftpUserName;
private static String ftpPassword;
private static Integer ftpPort;
static {
// 类初始化后加载配置文件
InputStream in = FTPUtils.class.getClassLoader().getResourceAsStream(ftpPath);
Properties props = new Properties();
try {
props.load(in);
} catch (Exception e) {
e.printStackTrace();
}
ftpHost = props.getProperty("ftpHost");
ftpUserName = props.getProperty("ftpUserName");
ftpPassword = props.getProperty("ftpPassword");
ftpPort = Integer.parseInt(props.getProperty("ftpPort"));
}
/*
* 从SFTP服务器下载文件
*
* @param ftpPath SFTP服务器中文件所在路径
*
* @param localPath 下载到本地的位置
*
* @param fileName 文件名称
*/
public static boolean downloadSftpFile(String ftpPath, String localPath, String fileName) throws JSchException {
List<Object> list_obj=getConnect();
Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
String ftpFilePath = ftpPath + "/" + fileName;
String localFilePath = localPath + File.separatorChar + fileName;
try {
chSftp.get(ftpFilePath, localFilePath);
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("download error.");
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
return false;
}
/**
* 获取sftp文件流
* @param ftpPath
* @param fileName
* @return
* @throws JSchException
* @throws IOException
*/
public static InputStream downloadSftpFile(String ftpPath, String fileName) throws JSchException, IOException {
InputStream is = null;
List<Object> list_obj=getConnect();
// Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
String ftpFilePath = ftpPath + "/" + fileName;
try {
is = chSftp.get(ftpFilePath);
} catch (Exception e) {
e.printStackTrace();
logger.info("download error.");
if(is!=null){
is.close();
}
} finally {
// chSftp.quit();
// channel.disconnect();
// session.disconnect();
}
return is;
}
/**
* @param ftpPath
* 上传到SFTP服务器上文件的路径
* @param localPath
* 本地的位置
* @param fileName
* 文件名称 上传文件到ftp指定目录
* @throws JSchException
*/
public static boolean upDataFile(String ftpPath, String localPath, String fileName) throws JSchException {
List<Object> list_obj=getConnect();
Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
String ftpFilePath = ftpPath + "/" + fileName;
String localFilePath = localPath + File.separatorChar + fileName;
try {
chSftp.put(localFilePath, ftpFilePath);
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("up error.");
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
return false;
}
/**
* 上传到SFTP服务器上文件的路径
* @param ftpPath
* @param fileName
* @param is
* @return
* @throws JSchException
*/
public static boolean uploadFile(String ftpPath, String fileName,InputStream is) throws JSchException {
List<Object> list_obj=getConnect();
Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
String ftpFilePath = ftpPath + "/" + fileName;
try {
chSftp.put(is, ftpFilePath);;
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("up error.");
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
return false;
}
/**
* @param ftpPath
* SFTP服务器上文件的目录 拿到指定目录下的所有文件名
* @throws JSchException
*/
@SuppressWarnings("unchecked")
public static List<String> getFileName(String ftpPath) throws JSchException {
List<Object> list_obj=getConnect();
Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
Vector<ChannelSftp.LsEntry> v = chSftp.ls(ftpPath);
List<String> list = new ArrayList<>();
for (ChannelSftp.LsEntry obj : v) {
String fileName = obj.getFilename();
list.add(fileName);
}
return list;
} catch (Exception e) {
e.printStackTrace();
logger.info("up error.");
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
return null;
}
/**
* 删除指定路径文件
*
* @param path
* @return
* @throws JSchException
*/
public static boolean removeFile(String path) throws JSchException {
List<Object> list_obj=getConnect();
Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
chSftp.rm(path);
;
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("ls error.");
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
return false;
}
/**
* 创建目录
*
* @param path
* @return
* @throws JSchException
*/
public static boolean mkDir(String path) throws JSchException {
List<Object> list_obj=getConnect();
Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
chSftp.mkdir(path);
;
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("ls error.");
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
return false;
}
/**
* 删除目录
*
* @param path
* @return
* @throws JSchException
*/
public static boolean rmDir(String path) throws JSchException {
List<Object> list_obj=getConnect();
Session session = (Session)list_obj.get(0);
Channel channel =(Channel)list_obj.get(1);
channel.connect();
ChannelSftp chSftp = (ChannelSftp) channel;
try {
chSftp.rmdir(path);
;
return true;
} catch (Exception e) {
e.printStackTrace();
logger.info("ls error.");
} finally {
chSftp.quit();
channel.disconnect();
session.disconnect();
}
return false;
}
/**
* 获取信道
*
* @param session
* @return
* @throws JSchException
*/
private static List<Object> getConnect() throws JSchException {
List<Object> list=new ArrayList<>();
Session session = null;
JSch jsch = new JSch();
session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
session.setPassword(ftpPassword);
session.setTimeout(100000);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
list.add(session);
list.add(channel);
return list;
}
}
3、依赖
<!-- sftp-jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>