我使用spring将文件上传到远程服务器。我可以在远程服务器上传同名的本地文件。现在,我想将具有不同名称(例如test_20180601.txt)的本地文件(example-test.txt)上传到远程服务器。我现在使用下面的代码。如何修改此代码以更改文件名?
@Configuration
@EnableConfigurationProperties(GcaSftpConfig.class)
@ConditionalOnProperty(prefix = "sftp.gca", name = "active", matchIfMissing = true)
public class GcaSftpUploadProcess
{
@Autowired
private GcaSftpConfig config;
@Bean(name = "gcaUploadSftpSessionFactory")
public SessionFactory<LsEntry> sftpSessionFactory()
{
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(config.getUpload().getHost());
factory.setPort(config.getUpload().getPort());
factory.setUser(config.getUpload().getUser());
factory.setPassword(config.getUpload().getPassword());
factory.setTimeout(config.getUpload().getTimeout());
factory.setAllowUnknownKeys(true);
return factory;
}
@Bean(name = "gcaUploadSftpRemoteFileTemplate")
public SftpRemoteFileTemplate sftpRemoteFileTemplate() throws Exception
{
SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sftpSessionFactory());
template.setRemoteDirectoryExpression(new LiteralExpression(config.getUpload().getRemoteDirectory()));
template.afterPropertiesSet();
return template;
}
public void upload(String localFileNameWithPath)
{
File file = new File(localFileNameWithPath);
Message<File> message = MessageBuilder.withPayload(file).build();
String send = sftpRemoteFileTemplate().send(message, FileExistsMode.REPLACE);
}
}
答案 0 :(得分:2)
RemoteFileTemplate
有此选项:
/**
* Set the file name generator used to generate the remote filename to be used when transferring
* files to the remote system. Default {@link DefaultFileNameGenerator}.
* @param fileNameGenerator the file name generator.
*/
public void setFileNameGenerator(FileNameGenerator fileNameGenerator) {