簡體   English   中英

無法使用 spring 集成將文件上傳到 sftp 服務器

[英]Not able to upload file to sftp server using spring integration

嗨,我在嘗試使用 spring 集成將 csv 文件上傳到 SFTP 服務器時遇到以下異常,無法弄清楚發生了什么錯誤,但在服務器上我可以看到類似10132_1234.csv.writing 的文件,但寫入操作永遠不會完成,而通過 winscp 我可以毫無問題地復制,請幫助解決此問題,

 Caused by: org.springframework.messaging.MessagingException: Failed
   to write to 'file-location/10132_1234.csv.writing' while uploading
   the file; nested exception is
   org.springframework.core.NestedIOException: failed to write file;
   nested exception is 3: Forbidden     at
   org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:560)
    at
   org.springframework.integration.file.remote.RemoteFileTemplate.doSend(RemoteFileTemplate.java:337)
    ... 88 common frames omitted`enter code here`

   Caused by: org.springframework.core.NestedIOException: failed to
   write file; nested exception is 3: Forbidden     at
   org.springframework.integration.sftp.session.SftpSession.write(SftpSession.java:177)
    at
   org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.write(CachingSessionFactory.java:235)
    at
   org.springframework.integration.file.remote.RemoteFileTemplate.doSend(RemoteFileTemplate.java:568)
    at
   org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:557)
    ... 89 common frames omitted

   Caused by: com.jcraft.jsch.SftpException: Forbidden  at
   com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
    at com.jcraft.jsch.ChannelSftp._put(ChannelSftp.java:594)   at
   com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:540)    at
   com.jcraft.jsch.ChannelSftp.put(ChannelSftp.java:492)    at
   org.springframework.integration.sftp.session.SftpSession.write(SftpSession.java:174)
    ... 92 common frames omitted

這是我的上傳代碼:

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(sftpHost);
    factory.setPort(sftpPort);
    factory.setUser(sftpUser);
    if (sftpPrivateKey != null) {
        factory.setPrivateKey(sftpPrivateKey);
        factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
    } else {
        factory.setPassword(sftpPasword);
    }
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<>(factory);
}

@Bean
@ServiceActivator(inputChannel = "toSftpChannel")
public MessageHandler handler() {
    logger.info("Entering into MessageHandler for uploading the file to SFTP server");
    SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());
    handler.setRemoteDirectoryExpression(new LiteralExpression(sftpRemoteDirectory));
    handler.setFileNameGenerator(message -> {
        if (message.getPayload() instanceof File) {
            return ((File) message.getPayload()).getName();
        } else {
            throw new IllegalArgumentException("File expected as payload.");
        }
    });
    return handler;
}

@MessagingGateway
public interface UploadGateway {
    @Gateway(requestChannel = "toSftpChannel")
    void upload(File file);

}

只需檢查您是否提供了有效的文件路徑作為目標 它應該是寫入上游文件內容的確切文件路徑

springframework.integration.sftp.session.SftpSession.write(InputStream inputStream, String destination)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM