可以使用 InputStreamResource 代替临时文件将异步文件上传到 sftp 服务器
Can InputStreamResource be used instead of temporary file for async file upload to sftp server
我有一个 Spring Integration flow
将文件异步上传到 sftp 服务器,要上传的文件来自 http 端点。最初我遇到了与所讨论的相同的问题 很高兴它得到了解决。
在同一个 SO 线程中我发现了这条评论。
In enterprise environments, you often have files of sizes you cannot afford to buffer into memory like that. Sadly enough, InputStreamResource won't work either. Your best bet, as far as I could tell so far, is to copy contents to an own temp file (e.g. File#createTempFile) which you can clean up at the end of the processing thread.
目前我正在将文件输入流连接到 InputStreamResource
以解决这个问题,它工作完美。为什么评论者说 InputStreamResource 也不起作用,AFAIK InputStream
从不将数据存储在内存中
InputStreamResource
的 inputStream 是否在文件上传后自动关闭?
当我们说大文件时,我们在这里谈论的文件大小是多少。目前在我的案例中有 2-5 Mb 的文件上传到 SFTP
我真的需要关心将我的文件上传机制更改为类似于存储在临时文件夹中的机制吗?
代码示例:
@PostMapping("/upload")
public void sampleEndpoint(@NotEmpty @RequestParam MultipartFile file )
throws IOException {
Resource resource = new InputStreamResource(file.getInputStream());
sftpFileService.upload(resource);
}
SftpFileService异步上传方法:
@Async
public void upload(Resource resource){
try{
messagingGateway.upload(resource);
}catch(Exception e){
e.printStackTrace();
}
}
2-5 Mb
可能不是一个需要担心的尺寸。当文件的大小为 1-2Gb
时,可能会出现此问题。虽然当您的服务发生多个并发上传时,您可能会遇到一些内存不足的情况。
InputStreamResource
只是围绕 InputStream
和 Resource
API 的装饰器,用于访问底层委托流。目前尚不清楚它如何在异步环境中工作,因为 MultipartFile
在 HTTP 上传请求结束时被删除。
此外,您没有显示任何代码来更好地了解情况...
我有一个 Spring Integration flow
将文件异步上传到 sftp 服务器,要上传的文件来自 http 端点。最初我遇到了与所讨论的相同的问题
在同一个 SO 线程中我发现了这条评论。
In enterprise environments, you often have files of sizes you cannot afford to buffer into memory like that. Sadly enough, InputStreamResource won't work either. Your best bet, as far as I could tell so far, is to copy contents to an own temp file (e.g. File#createTempFile) which you can clean up at the end of the processing thread.
目前我正在将文件输入流连接到 InputStreamResource
以解决这个问题,它工作完美。为什么评论者说 InputStreamResource 也不起作用,AFAIK InputStream
从不将数据存储在内存中
InputStreamResource
的 inputStream 是否在文件上传后自动关闭?
当我们说大文件时,我们在这里谈论的文件大小是多少。目前在我的案例中有 2-5 Mb 的文件上传到 SFTP
我真的需要关心将我的文件上传机制更改为类似于存储在临时文件夹中的机制吗?
代码示例:
@PostMapping("/upload")
public void sampleEndpoint(@NotEmpty @RequestParam MultipartFile file )
throws IOException {
Resource resource = new InputStreamResource(file.getInputStream());
sftpFileService.upload(resource);
}
SftpFileService异步上传方法:
@Async
public void upload(Resource resource){
try{
messagingGateway.upload(resource);
}catch(Exception e){
e.printStackTrace();
}
}
2-5 Mb
可能不是一个需要担心的尺寸。当文件的大小为 1-2Gb
时,可能会出现此问题。虽然当您的服务发生多个并发上传时,您可能会遇到一些内存不足的情况。
InputStreamResource
只是围绕 InputStream
和 Resource
API 的装饰器,用于访问底层委托流。目前尚不清楚它如何在异步环境中工作,因为 MultipartFile
在 HTTP 上传请求结束时被删除。
此外,您没有显示任何代码来更好地了解情况...