sftp文件上传下载


前言

在日常的工作中我们经常遇见使用sftp协议的方式进行文件的上传与下载,为了是我们的工作起来更加的方便,这里使用jsch包,进行上传下载,大大提供我们的工作效率,可以说是nice。
当然,sftp 和 ftp 有什么样的区别呢,记住一点即可。sftp是加密传输,ftp不是,就类似于https和https之前的关系一样。


一、直接上干货

1.maven

<!-- sftp -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.54</version>
</dependency>

二、使用步骤

1.引入库

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.SftpException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Vector;
import lombok.extern.slf4j.Slf4j;

2.读入数据

@Slf4j
public class SftpUtils {
    /**
     * @param filePath 上传文件路径
     * @param ftpPath  上传到的sftp服务器目录
     * @param username 用户名
     * @param password 密码
     * @param host     ip
     * @param port     端口
     */
    public static void uploadFile(String filePath, String ftpPath, String username, String password, String host, Integer port) {
        FileInputStream input;
        ChannelSftp sftp;
        try {
            JSch jsch = new JSch();
            //获取ssh - session
            com.jcraft.jsch.Session sshSession = jsch.getSession(username, host, port);
            //添加配置密码
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            //关闭主机密钥检查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            //开启session连接
            sshSession.connect();
            //获取sftp通道-并连接
            sftp = (ChannelSftp) sshSession.openChannel("sftp");
            sftp.connect();
            //判断目录是否存在
            try {
                //ls()获取指定目录下的文件列表
                Vector dir = sftp.ls(ftpPath);
            } catch (SftpException e) {
                sftp.mkdir(ftpPath);
            }
            sftp.cd(ftpPath);
            String filename = filePath.substring(filePath.lastIndexOf(File.separator) + 1); //附件名字
            input = new FileInputStream(new File(filePath));
            sftp.put(input, filename);
            input.close();
            sftp.disconnect();
            sshSession.disconnect();
            log.info("==============你的上传成功了===============");
        } catch (Exception e) {
            log.error("==============你的上传成功了===============",e);
        }
    }

    /**
     * @param directory    SFTP服务器的文件路径
     * @param downloadFile SFTP服务器上的文件名
     * @param saveFile     保存到本地路径
     * @param username     用户
     * @param password     密码
     * @param host         ip
     * @param port         端口
     */
    public static void downloadFile(String directory, String downloadFile, String saveFile, String username, String password, String host, Integer port) {
        ChannelSftp sftp;
        try {
            JSch jsch = new JSch();
            //获取ssh - session
            com.jcraft.jsch.Session sshSession = jsch.getSession(username, host, port);
            //设置密码
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            //关闭主机密钥检查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            //开启session连接
            sshSession.connect();
            //获取sftp通道-并连接
            sftp = (ChannelSftp) sshSession.openChannel("sftp");
            sftp.connect();
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            FileOutputStream output = new FileOutputStream(new File(saveFile));
            sftp.get(downloadFile, output);
            output.close();
            sftp.disconnect();
            sshSession.disconnect();
            log.info("================你的下载成功了!==================");
        } catch (Exception e) {
            log.error("================你的文件下载异常了!================", e);
        }
    }

}

该处使用工具类的方式调用即可,可进行自定义骚加改造。


总结

轮子必须有,将轮子进行到底

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架,而SFTP(SSH File Transfer Protocol)是一种安全的文件输协议。结合Spring Boot和SFTP可以实现文件的上下载功能。 要使用Spring Boot进行SFTP文件的上下载,可以按照以下步骤进行操作: 1. 添加依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-sftp</artifactId> </dependency> ``` 2. 配置SFTP连接信息:在`application.properties`或`application.yml`文件中配置SFTP连接信息,包括主机名、端口号、用户名、密码等。 3. 创建SFTP下载的服务类:可以创建一个服务类,使用Spring Integration提供的`SftpOutboundGateway`和`SftpInboundFileSynchronizer`来实现文件的上下载。 4. 实现文件功能:在服务类中,使用`SftpOutboundGateway`来实现文件的上。可以通过调用`put`方法将本地文件SFTP服务器上。 5. 实现文件下载功能:在服务类中,使用`SftpInboundFileSynchronizer`来实现文件下载。可以通过调用`synchronizeToLocalDirectory`方法将SFTP服务器上的文件下载到本地目录。 6. 创建Controller:创建一个Controller类,定义上下载文件的接口。在接口中调用服务类的方法来实现文件的上下载。 以上是使用Spring Boot和SFTP实现文件下载的基本步骤。具体的代码实现可以根据实际需求进行调整和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值