我有如下所示的文件控制器;
@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(消息,FileExistsMode.REPLACE)
但是我无法想象如何通过sftpRemoteFileTemplate.get方法下载它。
它需要InputStreamCallback,如何使用它做出下载响应?
强制创建一个FileOutputStream
并将数据从输入流复制到它。例如
FileCopyUtils.copy(stream, new FileOutputStream(new File("local/path"))
;