从数据库发送SFTP文件而不实际创建文件

huangapple go评论108阅读模式
英文:

Send SFTP file from database without actually creating a file

问题

我想从数据库中发送一些数据。目前我们的SFTP设置如下(使用apache commons vsf2库)。

  1. try (StandardFileSystemManager manager = new StandardFileSystemManager()) {
  2. File file = generateFileFromDatabase();
  3. manager.init();
  4. FileSystemOptions opts = new FileSystemOptions();
  5. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
  6. opts, "no");
  7. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
  8. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
  9. // Create local file object
  10. FileObject localFile = manager.resolveFile(file.getAbsolutePath());
  11. // Create remote file object
  12. FileObject remoteFile = manager.resolveFile(sftpUri, opts);
  13. // Copy local file to the SFTP server
  14. remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
  15. } catch (Exception ex) {
  16. ex.printStackTrace();
  17. }

然而,generateFileFromDatabase() 函数通过调用 new 关键字来工作,就像这样:

  1. return new File();

我不想这样,因为每次调用 generateFileFromDatabase() 都会在文件系统中保存一个新文件。有没有一种方法可以生成 File(),而不将其保存到文件目录中?

英文:

I want to send some data from the database. Currently our SFTP is set up like so (using the apache commons vsf2 library).

  1. try(StandardFileSystemManager manager = new StandardFileSystemManager())
  2. File file = generateFileFromDatabase();
  3. manager.init();
  4. FileSystemOptions opts = new FileSystemOptions();
  5. SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
  6. opts, "no");
  7. SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
  8. SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
  9. // Create localfile object
  10. FileObject localFile = manager.resolveFile(file.getAbsolutePath());
  11. // Create remote file object
  12. FileObject remoteFile = manager.resolveFile(sftpUri, opts);
  13. // Copy local file to sftp server
  14. remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
  15. } catch (Exception ex) {
  16. ex.printStackTrace();
  17. }

However, generateFileFromDatabase() works by invoking the new keyword, as in

  1. return new File();

I don't want this, because this saves a new file in the file system every time generateFileFromDatabase() is called. Is there a way to generate a File() without saving it to the file directory?

答案1

得分: 0

只需使用以下代码:

  1. OutputStream os = remoteFile.getContent().getOutputStream();

然后像处理本地文件一样,在循环中写入数据。

对于读取缓慢/大文件且连接时间长的情况,可能需要预先生成数据块并以多次追加的方式写入。

英文:

Just use

  1. OutputStream os = remoteFile.getContent().getOutputStream();

and then write In a loop to it, just like you would with a local file.

This might be a problem for slow reading/large transfers with long open connections. In that case you might need to pre-produce chunks and write them in multiple appends.

huangapple
  • 本文由 发表于 2020年8月4日 04:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63236874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定