我试图将文件上传到动态目录到SFTP。当我上传一些文件时,第一个文件总是上传到最后一个目录。然后,其余文件将被上传到正确的目录。当我进行调试模式时,我看到每个第一个文件都将被上载到临时目录,该目录是spring已经设置的代码。我不知道如何将此临时目录的值设置为正确的值。请帮我解决问题。或者,也许您还有其他方法可以上传并创建适当的动态目录。请告诉我。这是代码:
private String sftpRemoteDirectory = "documents/"
@MessagingGateway
public interface UploadGateway {
@Gateway(requestChannel = "toSftpChannel")
void upload(File file, @Header("dirName") String dirName);
}
@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
public MessageHandler handler() {
SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd");
String newDynamicDirectory = "E" + formatter.format(new Date())+String.format("%04d",Integer.parseInt("0001") + 1);
handler.setRemoteDirectoryExpression(new LiteralExpression(sftpRemoteDirectory + newDynamicDirectory));
handler.setFileNameGenerator(message -> {
String dirName = (String) message.getHeaders().get("dirName");
handler.setRemoteDirectoryExpression(new LiteralExpression(sftpRemoteDirectory + dirName));
handler.setAutoCreateDirectory(true);
if (message.getPayload() instanceof File) {
return (((File) message.getPayload()).getName());
} else {
throw new IllegalArgumentException("File expected as payload!");
}
});
return handler;
}
您正在使用LiteralExpression
,仅被评估一次,您需要一个在运行时评估的表达式。
handler.setRemoteDirectoryExpressionString("'" + sftpRemoteDirectory/ + "'" + headers['dirName']);