package com.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SFTPUtils {
private static ChannelSftp sftp;
private static SFTPUtils instance = null;
private SFTPUtils() {
}
//获取sftp连接实例
public static SFTPUtils getInstance(String host,int port,String username,String password) {
if (instance == null) {
if (instance == null) {
instance = new SFTPUtils();
sftp = instance.connect(host, port, username, password); //获取连接
}
}
return instance;
}
/**
* 连接sftp服务器
*
* @param host 主机
* @param port 端口
* @param username 用户名
* @param password 密码
* @return
*
* 过程就是通过Ip、Port、Username、Password获取一个Session,
* 然后通过Session打开SFTP通道(获得SFTP Channel对象),再在建立通道
* (Channel)连接,最后我们就是通过这个Channel对象来调用SFTP的各种操作方法.
* 总是要记得,我们操作完SFTP需要手动断开Channel连接与Session连接。
*/
public static 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);
sshSession.setPassword(password);
//配置文件操作类(.properties文件)此类继承自hashtable
Properties sshConfig = new Properties();
//StrictHostKeyChecking=no 最不安全的级别,当然也没有那么多烦人的提示了,相对安全的内网测试时建议使用。
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.err.println("SFTP Session connected.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.err.println("Connected to " + host);
} catch (Exception e) {
e.printStackTrace();
}
return sftp;
}
/**
* 上传文件
*
* @param directory 上传的目录
* @param uploadFile 要上传的文件
*/
public boolean upload(String directory, String uploadFile) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
FileInputStream fileInputStream = new FileInputStream(file);
sftp.put(fileInputStream, file.getName());
fileInputStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("文件上传失败");
return false;
}
}
/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param sftp
*/
public void upload(String directory, String uploadFile, ChannelSftp sftp,InputStream inputStream) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
sftp.put(inputStream, file.getName());
} catch (Exception e) {
System.err.println("上传失败");
e.printStackTrace();
}
}
public void upload(String directory, String uploadFile,InputStream inputStream) {
try {
sftp.cd(directory);
File file = new File(uploadFile);
sftp.put(inputStream, file.getName());
} catch (Exception e) {
System.err.println("上传失败===========================!!!");
e.printStackTrace();
}
}
public void disconnect() {
try {
sftp.getSession().disconnect();
} catch (JSchException e) {
e.printStackTrace();
}
sftp.quit();
sftp.disconnect();
}
//删除指定路径的文件
public static boolean deleteFile(File dirFile) {
// 如果dir对应的文件不存在,则退出
if (!dirFile.exists()) {
return false;
}
if (dirFile.isFile()) {
return dirFile.delete();
} else {
for (File file : dirFile.listFiles()) {
deleteFile(file);
}
}
return dirFile.delete();
}
/**
* 列出目录下的文件
*
* @param directory 要列出的目录
* @throws SftpException
*/
public Vector<ChannelSftp.LsEntry> listFiles(String directory) throws SftpException {
return sftp.ls(directory);
}
/**
* 获取流文件
* @param localFilepath
* @param fileName
* @return
*/
public InputStream getInputStream(String localFilepath ,String fileName){
InputStream inputStream = null;
try {
inputStream = sftp.get(localFilepath + "/" + fileName);
} catch (Exception e) {
System.err.println("获取文件数据流失败!");
e.printStackTrace();
}
return inputStream;
}
public static void main(String[] args) {
File file = new File("F:\\data\\cc.jpg");
try {
FileInputStream inputStream = new FileInputStream(file);
SFTPUtils sftpUtils = SFTPUtils.getInstance("上传地址ip",
22, "用户名",
"密码");
sftpUtils.upload("上传的目录, "上传的文件", inputStream);
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
将指定文件上传到指定sftp地址工具
猜你喜欢
转载自blog.csdn.net/qq_37356556/article/details/104640638
今日推荐
周排行