我的application.properties
文件如下所示:
sftp.host=0.0.0.0
sftp.port=22
sftp.user=foo
sftp.password=pass
我的带有上传方法的上传类如下所示:
public class UpAndDownLoad {
@Value("${sftp.host}")
private String sftpHost;
@Value("${sftp.port}")
private int sftpPort;
@Value("${sftp.user}")
private String sftpUser;
@Value("${sftp.password}")
private String sftpPasword;
private DefaultSftpSessionFactory getSftpFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setAllowUnknownKeys(true);
factory.setUser(sftpUser);
factory.setPassword(sftpPasword);
return factory;
}
public void upload() throws IOException {
SftpSession session = getSftpFactory().getSession();
InputStream resourceAsStream = UpAndDownLoad.class.getClassLoader().getResourceAsStream("mytextfile.txt");
session.write(resourceAsStream, "upload/mynewfile" + LocalDateTime.now() + ".txt");
session.close();
}
每当我输入 sftp 主机、用户、密码的值时,上传方法都会正常工作。
private DefaultSftpSessionFactory getSftpFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost("0.0.0.0");
factory.setPort(22);
factory.setAllowUnknownKeys(true);
factory.setUser("foo");
factory.setPassword("passs");
return factory;
}
但是一旦我从应用程序属性中传递 vlaues,如第二个代码块所示,它就会失败:
java.lang.IllegalStateException:无法创建 SFTP session
最佳答案
只需修改您的 applicationContext.xml
通过此添加 <context:property-holder>
.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:application.properties"/>
</beans>
或者如果基于 Java 的配置,则如下:
@Configuration
@PropertySource("classpath:application.properties")
public class Config {
}
将类作为组件。
@Component
public class UpAndDownLoad {
@Value("${sftp.host}")
private String sftpHost;
@Value("${sftp.port}")
private int sftpPort;
@Value("${sftp.user}")
private String sftpUser;
@Value("${sftp.password}")
private String sftpPasword;
private DefaultSftpSessionFactory getSftpFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
factory.setHost(sftpHost);
factory.setPort(sftpPort);
factory.setAllowUnknownKeys(true);
factory.setUser(sftpUser);
factory.setPassword(sftpPasword);
return factory;
}
public void upload() throws IOException {
SftpSession session = getSftpFactory().getSession();
InputStream resourceAsStream = UpAndDownLoad.class.getClassLoader().getResourceAsStream("mytextfile.txt");
session.write(resourceAsStream, "upload/mynewfile" + LocalDateTime.now() + ".txt");
session.close();
}
}
关于java - 使用 application.properties 值创建 SFTP 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60751277/