如何使用java代码将本地文件上传到SFTP服务器

第一步:pom.xml文件中导入相关依赖

<!--    SFTP上传    -->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.45</version>
        </dependency>

第二步:编写工具类

import com.jcraft.jsch.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
 
/**
 * sftp工具类,包含以下功能:
 * 获取sftp链接
 * 关闭sftp链接
 * 下载文件
 * 上传文件
 * 删除文件
 * 查找文件
 * 更多功能自行拓展
 */
public class SftpUtil {
 
    /**
     * 获取一个sftp链接
     * @param host sftp服务器ip
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @return 返回ChannelSftp
     * @throws Exception 获取通道时的异常
     */
    public static ChannelSftp getSftpChannel(String host, Integer port, String username, String password) throws Exception{
        Session session;
        Channel channel = null;
        JSch jSch = new JSch();
        try {
            session = jSch.getSession(username, host, port);
            session.setPassword(password);
 
            // 配置链接的属性
            Properties properties = new Properties();
            properties.setProperty("StrictHostKeyChecking","no");
            session.setConfig(properties);
 
            // 进行sftp链接
            session.connect();
 
            // 获取通信通道
            channel = session.openChannel("sftp");
            channel.connect();
        } catch (JSchException e) {
            e.printStackTrace();
            throw e;
        }
        return (ChannelSftp)channel;
    }
 
    /**
     * 上传文件
     * @param channelSftp sftp通道
     * @param localFile 本地文件(文件地址+文件名+文件后缀)
     * @param remoteFile 远程文件(文件地址+文件名+文件后缀)
     */
    public static void upload(ChannelSftp channelSftp, String localFile, String remoteFile) throws Exception{
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(localFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        
        channelSftp.put(inputStream, remoteFile);
        
        inputStream.close();
        
    }
 
    /**
     * 从sftp服务器上下载文件
     * @param channelSftp sftp通道
     * @param remoteFile 远程文件
     * @param localFile 本地文件
     */
    public static void download(ChannelSftp channelSftp, String remoteFile, String localFile) throws Exception{
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(localFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 
        
        channelSftp.get(remoteFile, outputStream);
        
        outputStream.close();
    }
 
    /**
     * 删除文件
     * @param channelSftp sftp通道
     * @param remoteFile 远程文件
     */
    public static void deleteFile(ChannelSftp channelSftp, String remoteFile) throws Exception{
        try {
            channelSftp.rm(remoteFile);
        } catch (SftpException e) {
            e.printStackTrace();
            throw e;
        }
    }
    
    /**
     * 1.根据指定目录获取文件夹下的文件列表
     * 2.根据文件绝对地址获取单个文件对象列表
     * @param channelSftp
     * @param directory
     * @return
     */
    @SuppressWarnings("unchecked")
    public static List<Object> viewDirectory(ChannelSftp channelSftp,String directory){
		List<Object> list = new ArrayList<Object>();
    	try {
			list = channelSftp.ls(directory);
		} catch (SftpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
    }
    
    /**
     * 关闭sftp链接
     * @param channelSftp sftp通道
     * @throws Exception 关闭session的异常
     */
    public static void closeSession(ChannelSftp channelSftp) throws Exception{
        if(channelSftp == null){
            return;
        }
 
        Session session = null;
        try {
            session = channelSftp.getSession();
        } catch (JSchException e) {
            e.printStackTrace();
            throw e;
        }finally {
            if(session != null){
                session.disconnect();
            }
        }
 
    }
 
}

注意事项:

使用时要先获取sftp连接,然后使用对应的文件操作方法,操作完成后及时关闭sftp连接

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码搬运工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值