1、添加maven依赖
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-sftp</artifactId>
<version>${spring-integration.version}</version>
</dependency>
2、sftp配置
sftp:
host:
port:
user:
password:
package com.xzp.config;
import cn.hutool.core.util.ObjectUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.Map;
@ConfigurationProperties(prefix = "sftp")
@Data
public class SftpProperties {
private String host;
private Integer port;
private String user;
private String password;
private String privateKey;
private String privateKeyPassphrase;
private Boolean allowUnknownKeys = true;
private String knownHosts;
private Boolean sharedSession = true;
private String remoteDirectory = "";
private String temporaryFileSuffix = ".tmp";
private Boolean useTemporaryFileName = true;
private Boolean autoCreateDirectory = true;
}
package com.xzp.config;
import com.jcraft.jsch.ChannelSftp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.file.remote.session.CachingSessionFactory;
import org.springframework.integration.sftp.session.DefaultSftpSessionFactory;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import javax.annotation.Resource;
@ConditionalOnProperty(
prefix = "sftp",
value = {"host","port","user"}
)
@EnableConfigurationProperties(SftpProperties.class)
@Configuration
public class SftpConfiguration {
@Resource
private SftpProperties sftpProperties;
@Bean
public DefaultSftpSessionFactory sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(sftpProperties.getSharedSession());
factory.setHost(sftpProperties.getHost());
factory.setPort(sftpProperties.getPort());
factory.setUser(sftpProperties.getUser());
if(StringUtils.isNotBlank(sftpProperties.getPrivateKey())){
factory.setPrivateKey(new ClassPathResource(sftpProperties.getPrivateKey()));
factory.setPrivateKeyPassphrase(sftpProperties.getPrivateKeyPassphrase());
}else{
factory.setPassword(sftpProperties.getPassword());
}
if(StringUtils.isNotBlank(sftpProperties.getKnownHosts())){
factory.setKnownHostsResource(new ClassPathResource(sftpProperties.getKnownHosts()));
}else{
factory.setAllowUnknownKeys(sftpProperties.getAllowUnknownKeys());
}
return factory;
}
@Bean
public CachingSessionFactory<ChannelSftp.LsEntry> cachingSessionFactory(){
CachingSessionFactory<ChannelSftp.LsEntry> cachingSessionFactory = new CachingSessionFactory<>(sftpSessionFactory());
cachingSessionFactory.setPoolSize(10);
cachingSessionFactory.setSessionWaitTimeout(10000);
return cachingSessionFactory;
}
@Bean
public SftpRemoteFileTemplate sftpRemoteFileTemplate() {
SftpRemoteFileTemplate sftpRemoteFileTemplate = new SftpRemoteFileTemplate(cachingSessionFactory());
sftpRemoteFileTemplate.setRemoteDirectoryExpression(new LiteralExpression(sftpProperties.getRemoteDirectory()));
sftpRemoteFileTemplate.setTemporaryFileSuffix(sftpProperties.getTemporaryFileSuffix());
sftpRemoteFileTemplate.setUseTemporaryFileName(sftpProperties.getUseTemporaryFileName());
sftpRemoteFileTemplate.setAutoCreateDirectory(sftpProperties.getAutoCreateDirectory());
return sftpRemoteFileTemplate;
}
}
3、封装方法
package com.xzp.service;
import cn.hutool.core.lang.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.sftp.session.SftpRemoteFileTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.Objects;
@Slf4j
@Component
public class SftpService {
@Autowired(required = false)
private SftpRemoteFileTemplate sftpRemoteFileTemplate;
/**
* 上传文件
*
* @param localFilePath 本地文件路径
* @param remoteFileDir 远程文件目录
* @return true-成功,false-失败,null-没有开启sftp
*/
public Boolean upload(String localFilePath, String remoteFileDir) {
if(Objects.isNull(sftpRemoteFileTemplate)){
return null;
}
Assert.notBlank(localFilePath, "本地文件路径不能为空");
Assert.notBlank(remoteFileDir, "远程文件目录不能为空");
log.info("上传文件到sftp, localFilePath:[{}], remoteFileDir:[{}]", localFilePath, remoteFileDir);
remoteFileDir = remoteFileDir.endsWith(File.separator) ? remoteFileDir : remoteFileDir + File.separator;
try {
Message<File> message = MessageBuilder.withPayload(new File(localFilePath)).build();
String result = sftpRemoteFileTemplate.send(message, remoteFileDir);
log.info("上传文件到sftp, 响应结果:[{}]", result);
return true;
} catch (Exception e) {
log.error("上传文件到sftp异常, localFilePath:[{}], remoteFileDir:[{}], 异常信息:[{}]", localFilePath, remoteFileDir, e.getMessage(), e);
return false;
}
}
}