【问题标题】:How to download file via SftpRemoteFileTemplate from endpoint如何通过 SftpRemoteFileTemplate 从端点下载文件
【发布时间】:2020-05-28 16:32:33
【问题描述】:

我有如下文件控制器;

@RestController
@RequestMapping("/files")
@RequiredArgsConstructor
public class FileController {

     private final SftpRemoteFileTemplate sftpRemoteFileTemplate;

    @PostMapping("/generate/{exportId}")
    public void generate(@PathVariable("exportId") String exportId) {
        File csvFile = createCsvFile(System.getProperty(JAVA_IO_TMPDIR));
        CsvUtils.write(csvFile, prepareExports(exportId));
        Message<File> message = MessageBuilder.withPayload(file).build();
        String path = remoteFileTemplate.send(message, FileExistsMode.REPLACE);
        //store path to download after etc...
    }


    @GetMapping("/download/{filePath}")
    public ResponseEntity<Resource> download(@PathVariable("filePath") String filePath) {
        //I didn't imagine how can i use it to download response?
        sftpRemoteFileTemplate.get(filePath, stream -> {

        });
    }    
}

我可以使用 ; 将文件上传到我的 SFTP 服务器remoteFileTemplate.send(message, FileExistsMode.REPLACE)

但我无法想象如何通过 sftpRemoteFileTemplate.get 方法下载它。

它需要InputStreamCallback,我如何使用它来做出下载响应?

【问题讨论】:

标签: spring-boot spring-mvc spring-integration spring-integration-sftp


【解决方案1】:

只需创建一个FileOutputStream 并将输入流中的数据复制到它。例如

FileCopyUtils.copy(stream, new FileOutputStream(new File("local/path"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2020-03-15
    相关资源
    最近更新 更多